-
Notifications
You must be signed in to change notification settings - Fork 404
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added opentelemetry bridge instrumentation that adds a context …
…manager, and processor to handle synthesizing segments and time slice metrics.
- Loading branch information
Showing
21 changed files
with
1,191 additions
and
308 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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,52 @@ | ||
/* | ||
* Copyright 2024 New Relic Corporation. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict' | ||
|
||
/** | ||
* @see https://open-telemetry.github.io/opentelemetry-js/interfaces/_opentelemetry_api.ContextManager.html | ||
*/ | ||
class ContextManager { | ||
#ctxMgr | ||
|
||
constructor(agent) { | ||
this.#ctxMgr = agent.tracer._contextManager | ||
} | ||
|
||
active() { | ||
return this.#ctxMgr.getContext() | ||
} | ||
|
||
bind(context, target) { | ||
return boundContext.bind(this) | ||
|
||
function boundContext(...args) { | ||
return this.with(context, target, this, ...args) | ||
} | ||
} | ||
|
||
/** | ||
* Runs the callback within the provided context, optionally | ||
* bound with a provided `this`. | ||
* | ||
* @param context | ||
* @param callback | ||
* @param thisRef | ||
* @param args | ||
*/ | ||
with(context, callback, thisRef, ...args) { | ||
return this.#ctxMgr.runInContext(context, callback, thisRef, args) | ||
} | ||
|
||
enable() { | ||
return this | ||
} | ||
|
||
disable() { | ||
return this | ||
} | ||
} | ||
|
||
module.exports = ContextManager |
This file contains 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,25 @@ | ||
/* | ||
* Copyright 2025 New Relic Corporation. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict' | ||
const { diag, DiagLogLevel } = require('@opentelemetry/api') | ||
|
||
// Map New Relic log levels to OTel log levels | ||
const logLevels = { | ||
trace: 'VERBOSE', | ||
debug: 'DEBUG', | ||
info: 'INFO', | ||
warn: 'WARN', | ||
error: 'ERROR', | ||
fatal: 'ERROR' | ||
} | ||
|
||
module.exports = function createOtelLogger(logger, config) { | ||
// enable exporter logging | ||
// OTel API calls "verbose" what we call "trace". | ||
logger.verbose = logger.trace | ||
const logLevel = DiagLogLevel[logLevels[config.logging.level]] | ||
diag.setLogger(logger, logLevel) | ||
} |
This file contains 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
This file contains 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
This file contains 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,45 @@ | ||
/* | ||
* Copyright 2025 New Relic Corporation. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict' | ||
const { BasicTracerProvider } = require('@opentelemetry/sdk-trace-base') | ||
const { Resource } = require('@opentelemetry/resources') | ||
const { SEMRESATTRS_SERVICE_NAME } = require('@opentelemetry/semantic-conventions') | ||
const NrSpanProcessor = require('./span-processor') | ||
const ContextManager = require('./context-manager') | ||
const defaultLogger = require('../logger').child({ component: 'opentelemetry-bridge' }) | ||
const createOtelLogger = require('./logger') | ||
|
||
module.exports = function setupOtel(agent, logger = defaultLogger) { | ||
if (agent.config.feature_flag.opentelemetry_bridge !== true) { | ||
logger.warn( | ||
'`feature_flag.opentelemetry_bridge` is not enabled, skipping setup of opentelemetry-bridge' | ||
) | ||
return | ||
} | ||
|
||
createOtelLogger(logger, agent.config) | ||
|
||
const provider = new BasicTracerProvider({ | ||
spanProcessors: [new NrSpanProcessor(agent)], | ||
resource: new Resource({ | ||
[SEMRESATTRS_SERVICE_NAME]: agent.config.applications()[0] | ||
}), | ||
generalLimits: { | ||
attributeValueLengthLimit: 4095 | ||
} | ||
|
||
}) | ||
provider.register({ | ||
contextManager: new ContextManager(agent) | ||
// propagator: // todo: https://github.com/newrelic/node-newrelic/issues/2662 | ||
}) | ||
|
||
agent.metrics | ||
.getOrCreateMetric('Supportability/Nodejs/OpenTelemetryBridge/Setup') | ||
.incrementCallCount() | ||
|
||
return provider | ||
} |
This file contains 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,43 @@ | ||
/* | ||
* Copyright 2025 New Relic Corporation. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict' | ||
const SegmentSynthesizer = require('./segment-synthesis') | ||
const { otelSynthesis } = require('../symbols') | ||
const { hrTimeToMilliseconds } = require('@opentelemetry/core') | ||
|
||
module.exports = class NrSpanProcessor { | ||
constructor(agent) { | ||
this.agent = agent | ||
this.synthesizer = new SegmentSynthesizer(agent) | ||
this.tracer = agent.tracer | ||
} | ||
|
||
/** | ||
* Synthesize segment at start of span and assign to a symbol | ||
* that will be removed in Context after it is assigned to the context. | ||
* @param span | ||
*/ | ||
onStart(span) { | ||
span[otelSynthesis] = this.synthesizer.synthesize(span) | ||
} | ||
|
||
/** | ||
* Update the segment duration from span and reconcile | ||
* any attributes that were added after the start | ||
* @param span | ||
*/ | ||
onEnd(span) { | ||
const ctx = this.tracer.getContext() | ||
this.updateDuration(ctx._segment, span) | ||
// TODO: add attributes from span that did not exist at start | ||
} | ||
|
||
updateDuration(segment, span) { | ||
segment.end() | ||
const duration = hrTimeToMilliseconds(span.duration) | ||
segment.overwriteDurationInMillis(duration) | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.