-
Notifications
You must be signed in to change notification settings - Fork 255
feat(CLDSRV-884): Add OpenTelemetry tracing instrumentation #6140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
delthas
wants to merge
8
commits into
development/9.4
Choose a base branch
from
improvement/CLDSRV-884/otel-instrumentation
base: development/9.4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
40a3fd1
chore: prettier-format files touched by CLDSRV-884
delthas 60a9dbe
chore: add OpenTelemetry dependencies
delthas c83bfd9
feat: add trust-boundary host filter
delthas 0bd2f71
feat: add health-probe ignore filter
delthas 9b000da
feat: add OTEL bootstrap and manual span helper
delthas 700e93e
feat: flush OTEL on shutdown
delthas 82a77a4
feat: instrument all S3 API handlers with OTEL spans
delthas c25268d
chore: bump arsenal to 8.4.3
delthas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
delthas marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| 'use strict'; | ||
|
|
||
| // Probe + scrape paths that should never produce a span. Filtered at | ||
| // ingest (not at the trace backend) because probe rate × pod count × | ||
| // always-on sampling overwhelms the exporter and storage with traffic | ||
| // nobody queries. | ||
| const HEALTH_PATHS = new Set(['/live', '/ready', '/_/healthcheck', '/_/healthcheck/deep', '/metrics']); | ||
|
|
||
|
francoisferrand marked this conversation as resolved.
|
||
| function isHealthPath(url) { | ||
|
delthas marked this conversation as resolved.
|
||
| if (typeof url !== 'string' || url.length === 0) { | ||
| return false; | ||
| } | ||
| const qIdx = url.indexOf('?'); | ||
| const path = qIdx === -1 ? url : url.slice(0, qIdx); | ||
| return HEALTH_PATHS.has(path); | ||
| } | ||
|
|
||
| module.exports = { isHealthPath }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| 'use strict'; | ||
|
|
||
| const assert = require('assert'); | ||
| const cluster = require('cluster'); | ||
|
|
||
| const { loadTrustedHosts, makeRequestHook } = require('./trustedHosts'); | ||
| const { isHealthPath } = require('./healthPaths'); | ||
|
|
||
| let sdk = null; | ||
|
|
||
| function isEnabled() { | ||
|
delthas marked this conversation as resolved.
|
||
| return process.env.ENABLE_OTEL === 'true'; | ||
| } | ||
|
|
||
| function init() { | ||
| if (!isEnabled() || sdk) { | ||
| return; | ||
| } | ||
|
|
||
| const endpoint = process.env.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT; | ||
| assert(endpoint, 'ENABLE_OTEL=true but OTEL_EXPORTER_OTLP_TRACES_ENDPOINT is unset'); | ||
|
|
||
| // Cluster primary doesn't serve HTTP; workers re-exec the entry and init() themselves. | ||
| const { config } = require('../Config'); | ||
| if (config.isCluster && cluster.isPrimary) { | ||
| return; | ||
| } | ||
|
|
||
| const { diag, DiagConsoleLogger, DiagLogLevel } = require('@opentelemetry/api'); | ||
| const { NodeSDK } = require('@opentelemetry/sdk-node'); | ||
| const { resourceFromAttributes } = require('@opentelemetry/resources'); | ||
| const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-http'); | ||
| const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http'); | ||
| const { IORedisInstrumentation } = require('@opentelemetry/instrumentation-ioredis'); | ||
| const { MongoDBInstrumentation } = require('@opentelemetry/instrumentation-mongodb'); | ||
| const { ParentBasedSampler, TraceIdRatioBasedSampler } = require('@opentelemetry/sdk-trace-base'); | ||
| const { version } = require('../../package.json'); | ||
|
|
||
| diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.WARN); | ||
|
|
||
| let samplingRatio = 0.01; | ||
| if (process.env.OTEL_SAMPLING_RATIO !== undefined) { | ||
| const parsed = Number(process.env.OTEL_SAMPLING_RATIO); | ||
| assert( | ||
| Number.isFinite(parsed) && parsed >= 0 && parsed <= 1, | ||
| `OTEL_SAMPLING_RATIO must be a finite number in [0, 1], got: ${process.env.OTEL_SAMPLING_RATIO}`, | ||
| ); | ||
| samplingRatio = parsed; | ||
| } | ||
|
|
||
| const trustedHosts = loadTrustedHosts(); | ||
|
|
||
| const ignoreIncomingRequestHook = req => req.method === 'OPTIONS' || isHealthPath(req.url); | ||
|
|
||
| sdk = new NodeSDK({ | ||
| resource: resourceFromAttributes({ | ||
| 'service.name': process.env.OTEL_SERVICE_NAME || 'cloudserver', | ||
| 'service.version': process.env.OTEL_SERVICE_VERSION || version, | ||
| 'service.namespace': process.env.OTEL_SERVICE_NAMESPACE || 'scality', | ||
| }), | ||
| traceExporter: new OTLPTraceExporter({ url: endpoint }), | ||
| logRecordProcessors: [], | ||
| metricReaders: [], | ||
| spanLimits: { | ||
| attributeValueLengthLimit: 4096, | ||
| attributeCountLimit: 128, | ||
| eventCountLimit: 128, | ||
| linkCountLimit: 128, | ||
| }, | ||
| sampler: new ParentBasedSampler({ | ||
| root: new TraceIdRatioBasedSampler(samplingRatio), | ||
| }), | ||
| instrumentations: [ | ||
| new HttpInstrumentation({ | ||
| ignoreIncomingRequestHook, | ||
| requestHook: makeRequestHook(trustedHosts), | ||
| }), | ||
| new IORedisInstrumentation({ requireParentSpan: true }), | ||
| // Mask leaf values in db.statement so query shape is captured | ||
| // without user data (object keys, filter values) flowing to | ||
| // the trace backend. | ||
| new MongoDBInstrumentation({ enhancedDatabaseReporting: false }), | ||
| ], | ||
| }); | ||
|
|
||
| sdk.start(); | ||
| } | ||
|
|
||
| // Cap the flush window. The BatchSpanProcessor's default 30s export | ||
| // timeout would otherwise block process.exit, and Kubernetes' default | ||
| // terminationGracePeriodSeconds is also 30s — we'd get SIGKILL'd | ||
| // before the flush ever completed. | ||
| const SHUTDOWN_DEADLINE_MS = 5000; | ||
|
|
||
| async function close() { | ||
| // Capture + clear before awaiting so concurrent callers (SIGTERM | ||
| // during an uncaught-exception flow, for example) don't both call | ||
| // sdk.shutdown() — the SDK doesn't guarantee idempotent shutdown. | ||
| const local = sdk; | ||
| if (!local) { | ||
| return; | ||
| } | ||
| sdk = null; | ||
| // Attach the rejection handler before racing the timeout. If the | ||
| // timeout wins the race, sdk.shutdown() keeps running unattached; | ||
| // a late rejection on that orphaned promise would otherwise crash | ||
| // the process via Node's default unhandledRejection behavior. | ||
| const shutdown = (async () => { | ||
| try { | ||
| await local.shutdown(); | ||
| } catch (err) { | ||
| // Loggers may already be torn down at this point in shutdown; | ||
| // log to stderr directly. | ||
| // eslint-disable-next-line no-console | ||
| console.error('tracing close failed', err); | ||
| } | ||
| })(); | ||
| await Promise.race([ | ||
| shutdown, | ||
| // .unref() so the timer doesn't pin the event loop open | ||
| // when sdk.shutdown() resolves first. | ||
| new Promise(resolve => { | ||
| setTimeout(resolve, SHUTDOWN_DEADLINE_MS).unref(); | ||
| }), | ||
| ]); | ||
| require('./instrumentation').resetTracer(); | ||
| } | ||
|
|
||
| module.exports = { init, close, isEnabled }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.