Skip to content
This repository has been archived by the owner on Sep 10, 2024. It is now read-only.

Commit

Permalink
Record request body for mget and add (de)serialization spans
Browse files Browse the repository at this point in the history
  • Loading branch information
dgieselaar authored and rockdaboot committed Mar 31, 2022
1 parent 4c8ce86 commit 4c0efd9
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 19 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@
"deep-freeze-strict": "^1.1.1",
"deepmerge": "^4.2.2",
"del": "^5.1.0",
"elastic-apm-node": "^3.27.0",
"elastic-apm-node": "https://github.com/dgieselaar/apm-agent-nodejs.git#a212ba683632c6a0c2178436451a99ce6f19572a",
"execa": "^4.0.2",
"exit-hook": "^2.2.0",
"expiry-js": "0.1.7",
Expand Down Expand Up @@ -894,4 +894,4 @@
"yargs": "^15.4.1",
"zlib": "^1.0.5"
}
}
}
35 changes: 35 additions & 0 deletions src/core/server/elasticsearch/client/configure_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import { Client, HttpConnection } from '@elastic/elasticsearch';
import type { KibanaClient } from '@elastic/elasticsearch/lib/api/kibana';
import agent, { Span } from 'elastic-apm-node';
import LRUCache from 'lru-cache';
import { Logger } from '../../logging';
import { parseClientOptions, ElasticsearchClientConfig } from './client_config';
import { instrumentEsQueryAndDeprecationLogger } from './log_query_and_deprecation';
Expand All @@ -32,12 +34,45 @@ export const configureClient = (
const clientOptions = parseClientOptions(config, scoped);
const KibanaTransport = createTransport({ getExecutionContext });

const cache = new LRUCache<any, Span | undefined | null>({
max: 100,
});

const client = new Client({
...clientOptions,
Transport: KibanaTransport,
Connection: HttpConnection,
});

function startSpan(name: string) {
const span = agent.startSpan(name, 'db', 'elasticsearch', { exitSpan: true });
return span;
}

client.diagnostic.on('serialization', (err, result) => {
if (!err) {
cache.set(result?.meta.request.id, startSpan('serialization'));
}
});

client.diagnostic.on('request', (err, result) => {
cache.get(result?.meta.request.id)?.end();
if (!err) {
cache.set(result?.meta.request.id, startSpan('request'));
}
});

client.diagnostic.on('deserialization', (err, result) => {
cache.get(result?.requestId)?.end();
if (!err) {
cache.set(result?.requestId, startSpan('deserialization'));
}
});

client.diagnostic.on('response', (err, result) => {
cache.get(result?.meta.request.id)?.end();
});

instrumentEsQueryAndDeprecationLogger({ logger, client, type });

return client as KibanaClient;
Expand Down
44 changes: 27 additions & 17 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7539,6 +7539,15 @@ agentkeepalive@^4.1.3:
depd "^1.1.2"
humanize-ms "^1.2.1"

agentkeepalive@^4.2.1:
version "4.2.1"
resolved "https://registry.yarnpkg.com/agentkeepalive/-/agentkeepalive-4.2.1.tgz#a7975cbb9f83b367f06c90cc51ff28fe7d499717"
integrity sha512-Zn4cw2NEqd+9fiSVWMscnjyQ1a8Yfoc5oBajLeo5w+YBHgDUcEBY2hS4YpTz6iN5f/2zQiktcuM6tS8x1p9dpA==
dependencies:
debug "^4.1.0"
depd "^1.1.2"
humanize-ms "^1.2.1"

aggregate-error@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/aggregate-error/-/aggregate-error-3.0.1.tgz#db2fe7246e536f40d9b5442a39e117d7dd6a24e0"
Expand Down Expand Up @@ -12731,24 +12740,25 @@ ejs@^3.1.6:
dependencies:
jake "^10.6.1"

elastic-apm-http-client@^10.3.0:
version "10.3.0"
resolved "https://registry.yarnpkg.com/elastic-apm-http-client/-/elastic-apm-http-client-10.3.0.tgz#12b95dc190a755cd1a8ce2c296cd28ef50f16aa4"
integrity sha512-BAqB7k5JA/x09L8BVj04WRoknRptmW2rLAoHQVrPvPhUm/IgNz63wPfiBuhWVE//Hl7xEpURO5pMV6az0UArkA==
elastic-apm-http-client@11.0.1:
version "11.0.1"
resolved "https://registry.yarnpkg.com/elastic-apm-http-client/-/elastic-apm-http-client-11.0.1.tgz#15dbe99d56d62b3f732d1bd2b51bef6094b78801"
integrity sha512-5AOWlhs2WlZpI+DfgGqY/8Rk7KF8WeevaO8R961eBylavU6GWhLRNiJncohn5jsvrqhmeT19azBvy/oYRN7bJw==
dependencies:
agentkeepalive "^4.2.1"
breadth-filter "^2.0.0"
container-info "^1.0.1"
end-of-stream "^1.4.4"
fast-safe-stringify "^2.0.7"
fast-stream-to-buffer "^1.0.0"
object-filter-sequence "^1.0.0"
readable-stream "^3.4.0"
semver "^6.3.0"
stream-chopper "^3.0.1"

elastic-apm-node@^3.27.0:
version "3.27.0"
resolved "https://registry.yarnpkg.com/elastic-apm-node/-/elastic-apm-node-3.27.0.tgz#0de441e36519e7384e66f6d420ec9a00a2669a1a"
integrity sha512-x8rWjGBgwKYcYN9IQWUv3dzq88RxEVnVIC6KMXQpFkRBG6XtWG4FeMvLaDSeTdXVnDBKTDVGCF9np5HdGhYbBw==
"elastic-apm-node@https://github.com/dgieselaar/apm-agent-nodejs.git#a212ba683632c6a0c2178436451a99ce6f19572a":
version "3.31.0"
resolved "https://github.com/dgieselaar/apm-agent-nodejs.git#a212ba683632c6a0c2178436451a99ce6f19572a"
dependencies:
"@elastic/ecs-pino-format" "^1.2.0"
after-all-results "^2.0.0"
Expand All @@ -12757,15 +12767,14 @@ elastic-apm-node@^3.27.0:
basic-auth "^2.0.1"
cookie "^0.4.0"
core-util-is "^1.0.2"
elastic-apm-http-client "^10.3.0"
elastic-apm-http-client "11.0.1"
end-of-stream "^1.4.4"
error-callsites "^2.0.4"
error-stack-parser "^2.0.6"
escape-string-regexp "^4.0.0"
fast-safe-stringify "^2.0.7"
http-headers "^3.0.2"
is-native "^1.0.1"
load-source-map "^2.0.0"
lru-cache "^6.0.0"
measured-reporting "^1.51.1"
monitor-event-loop-delay "^1.0.0"
Expand All @@ -12779,6 +12788,7 @@ elastic-apm-node@^3.27.0:
semver "^6.3.0"
set-cookie-serde "^1.0.0"
shallow-clone-shim "^2.0.0"
source-map "^0.8.0-beta.0"
sql-summary "^1.0.1"
traceparent "^1.0.0"
traverse "^0.6.6"
Expand Down Expand Up @@ -18924,13 +18934,6 @@ load-json-file@^6.2.0:
strip-bom "^4.0.0"
type-fest "^0.6.0"

load-source-map@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/load-source-map/-/load-source-map-2.0.0.tgz#48f1c7002d7d9e20dd119da6e566104ec46a5683"
integrity sha512-QNZzJ2wMrTmCdeobMuMNEXHN1QGk8HG6louEkzD/zwQ7EU2RarrzlhQ4GnUYEFzLhK+Jq7IGyF/qy+XYBSO7AQ==
dependencies:
source-map "^0.7.3"

loader-runner@^2.4.0:
version "2.4.0"
resolved "https://registry.yarnpkg.com/loader-runner/-/loader-runner-2.4.0.tgz#ed47066bfe534d7e84c4c7b9998c2a75607d9357"
Expand Down Expand Up @@ -26146,6 +26149,13 @@ source-map@^0.7.2, source-map@^0.7.3, source-map@~0.7.2:
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.7.3.tgz#5302f8169031735226544092e64981f751750383"
integrity sha512-CkCj6giN3S+n9qrYiBTX5gystlENnRW5jZeNLHpe6aue+SrHcG5VYwujhW9s4dY31mEGsxBDrHR6oI69fTXsaQ==

source-map@^0.8.0-beta.0:
version "0.8.0-beta.0"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.8.0-beta.0.tgz#d4c1bb42c3f7ee925f005927ba10709e0d1d1f11"
integrity sha512-2ymg6oRBpebeZi9UUNsgQ89bhx01TcTkmNTGnNO88imTmbSgy4nfujrgVEFKWpMTEGA11EDkTt7mqObTPdigIA==
dependencies:
whatwg-url "^7.0.0"

sourcemap-codec@^1.4.1:
version "1.4.6"
resolved "https://registry.yarnpkg.com/sourcemap-codec/-/sourcemap-codec-1.4.6.tgz#e30a74f0402bad09807640d39e971090a08ce1e9"
Expand Down

0 comments on commit 4c0efd9

Please sign in to comment.