-
Notifications
You must be signed in to change notification settings - Fork 355
OTel SDK #2498
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
Merged
Merged
OTel SDK #2498
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a28d754
Implement basic OTel SDK
149ace5
Add some doc comments explaining some possibly unclear bits of OTel
f85d3f8
Add types and API docs for OTel
ceb5f48
Add compatibility intro to docs/API.md
85f5194
Add integration test
02b475c
Use OTel API in integration test
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
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
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
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,67 @@ | ||
| 'use strict' | ||
|
|
||
| const { AsyncLocalStorage } = require('async_hooks') | ||
| const { trace, ROOT_CONTEXT } = require('@opentelemetry/api') | ||
|
|
||
| const SpanContext = require('./span_context') | ||
| const tracer = require('../../') | ||
|
|
||
| // Horrible hack to acquire the otherwise inaccessible SPAN_KEY so we can redirect it... | ||
| let SPAN_KEY | ||
| trace.getSpan({ | ||
| getValue (key) { | ||
| SPAN_KEY = key | ||
| } | ||
| }) | ||
|
|
||
| function wrappedGetValue (target) { | ||
| return (key) => { | ||
| if (key === SPAN_KEY) { | ||
| return { | ||
| spanContext () { | ||
| const activeSpan = tracer.scope().active() | ||
| const context = activeSpan && activeSpan.context() | ||
| return new SpanContext(context) | ||
| } | ||
| } | ||
| } | ||
| return target.getValue(key) | ||
| } | ||
| } | ||
|
|
||
| class ContextManager { | ||
| constructor () { | ||
| this._store = new AsyncLocalStorage() | ||
bengl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| active () { | ||
| const active = this._store.getStore() || ROOT_CONTEXT | ||
|
|
||
| return new Proxy(active, { | ||
| get (target, key) { | ||
| return key === 'getValue' ? wrappedGetValue(target) : target[key] | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| with (context, fn, thisArg, ...args) { | ||
| const span = trace.getSpan(context) | ||
| const ddScope = tracer.scope() | ||
| return ddScope.activate(span._ddSpan, () => { | ||
| const cb = thisArg == null ? fn : fn.bind(thisArg) | ||
| return this._store.run(context, cb, ...args) | ||
| }) | ||
| } | ||
|
|
||
| bind (context, target) { | ||
| const self = this | ||
| return function (...args) { | ||
| return self.with(context, target, this, args) | ||
| } | ||
| } | ||
|
|
||
| enable () {} | ||
| disable () {} | ||
Qard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| module.exports = ContextManager | ||
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,16 @@ | ||
| 'use strict' | ||
|
|
||
| class Sampler { | ||
| shouldSample (context, traceId, spanName, spanKind, attributes, links) { | ||
| // 0 = no, 1 = record, 2 = record and sample | ||
| // TODO: Make this actually do sampling... | ||
Qard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return { decision: 2 } | ||
| } | ||
|
|
||
| /** Returns the sampler name or short description with the configuration. */ | ||
| toString () { | ||
| return 'DatadogSampler' | ||
| } | ||
| } | ||
|
|
||
| module.exports = Sampler | ||
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,152 @@ | ||
| 'use strict' | ||
|
|
||
| const api = require('@opentelemetry/api') | ||
|
|
||
| const { | ||
| getTimeOrigin, | ||
| otperformance, | ||
| timeInputToHrTime | ||
| } = require('@opentelemetry/core') | ||
|
|
||
| const tracer = require('../../') | ||
| const DatadogSpan = require('../opentracing/span') | ||
| const { ERROR_MESSAGE, ERROR_TYPE, ERROR_STACK } = require('../constants') | ||
|
|
||
| const SpanContext = require('./span_context') | ||
|
|
||
| // The one built into OTel rounds so we lose sub-millisecond precision. | ||
| function hrTimeToMilliseconds (time) { | ||
| return time[0] * 1e3 + time[1] / 1e6 | ||
| } | ||
|
|
||
| class Span { | ||
| constructor ( | ||
| parentTracer, | ||
| context, | ||
| spanName, | ||
| spanContext, | ||
| kind, | ||
| links = [], | ||
| timeInput | ||
| ) { | ||
| const { _tracer } = tracer | ||
|
|
||
| const hrStartTime = timeInputToHrTime(timeInput || (otperformance.now() + getTimeOrigin())) | ||
| const startTime = hrTimeToMilliseconds(hrStartTime) | ||
|
|
||
| this._ddSpan = new DatadogSpan(_tracer, _tracer._processor, _tracer._prioritySampler, { | ||
| operationName: spanName, | ||
| context: spanContext._ddContext, | ||
| startTime, | ||
| hostname: _tracer._hostname, | ||
| tags: { | ||
| 'service.name': _tracer._service | ||
| } | ||
| }, _tracer._debug) | ||
|
|
||
| this._parentTracer = parentTracer | ||
| this._context = context | ||
|
|
||
| this._hasStatus = false | ||
|
|
||
| // NOTE: Need to grab the value before setting it on the span because the | ||
| // math for computing opentracing timestamps is apparently lossy... | ||
| this.startTime = hrStartTime | ||
| this.kind = kind | ||
| this.links = links | ||
| this._spanProcessor.onStart(this, context) | ||
| } | ||
|
|
||
| get parentSpanId () { | ||
| const { _parentId } = this._ddSpan.context() | ||
| return _parentId && _parentId.toString(16) | ||
| } | ||
|
|
||
| // Expected by OTel | ||
| get resource () { | ||
| return this._parentTracer.resource | ||
| } | ||
| get instrumentationLibrary () { | ||
| return this._parentTracer.instrumentationLibrary | ||
| } | ||
| get _spanProcessor () { | ||
| return this._parentTracer.getActiveSpanProcessor() | ||
| } | ||
|
|
||
| get name () { | ||
| return this._ddSpan.context()._name | ||
| } | ||
|
|
||
| spanContext () { | ||
| return new SpanContext(this._ddSpan.context()) | ||
| } | ||
|
|
||
| setAttribute (key, value) { | ||
| this._ddSpan.setTag(key, value) | ||
| return this | ||
| } | ||
|
|
||
| setAttributes (attributes) { | ||
| this._ddSpan.addTags(attributes) | ||
| return this | ||
| } | ||
|
|
||
| addEvent (name, attributesOrStartTime, startTime) { | ||
| api.diag.warn('Events not supported') | ||
| return this | ||
| } | ||
|
|
||
| setStatus ({ code, message }) { | ||
| if (!this.ended && !this._hasStatus && code) { | ||
| this._hasStatus = true | ||
| if (code === 2) { | ||
| this._ddSpan.addTags({ | ||
| [ERROR_MESSAGE]: message | ||
| }) | ||
| } | ||
| } | ||
| return this | ||
| } | ||
|
|
||
| updateName (name) { | ||
| if (!this.ended) { | ||
| this._ddSpan.setOperationName(name) | ||
| } | ||
| return this | ||
| } | ||
|
|
||
| end (timeInput) { | ||
| if (this.ended) { | ||
| api.diag.error('You can only call end() on a span once.') | ||
| return | ||
| } | ||
|
|
||
| const hrEndTime = timeInputToHrTime(timeInput || (otperformance.now() + getTimeOrigin())) | ||
| const endTime = hrTimeToMilliseconds(hrEndTime) | ||
|
|
||
| this._ddSpan.finish(endTime) | ||
| this._spanProcessor.onEnd(this) | ||
| } | ||
|
|
||
| isRecording () { | ||
| return this.ended === false | ||
| } | ||
|
|
||
| recordException (exception) { | ||
| this._ddSpan.addTags({ | ||
| [ERROR_TYPE]: exception.name, | ||
| [ERROR_MESSAGE]: exception.message, | ||
| [ERROR_STACK]: exception.stack | ||
| }) | ||
| } | ||
|
|
||
| get duration () { | ||
| return this._ddSpan._duration | ||
| } | ||
|
|
||
| get ended () { | ||
| return typeof this.duration !== 'undefined' | ||
| } | ||
| } | ||
|
|
||
| module.exports = Span |
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,44 @@ | ||
| 'use strict' | ||
|
|
||
| const api = require('@opentelemetry/api') | ||
| const { AUTO_KEEP } = require('../../../../ext/priority') | ||
| const DatadogSpanContext = require('../opentracing/span_context') | ||
| const id = require('../id') | ||
|
|
||
| function newContext () { | ||
| const spanId = id() | ||
| return new DatadogSpanContext({ | ||
| traceId: spanId, | ||
| spanId | ||
| }) | ||
| } | ||
|
|
||
| class SpanContext { | ||
| constructor (context) { | ||
| if (!(context instanceof DatadogSpanContext)) { | ||
| context = context | ||
| ? new DatadogSpanContext(context) | ||
| : newContext() | ||
| } | ||
| this._ddContext = context | ||
| } | ||
|
|
||
| get traceId () { | ||
| return this._ddContext._traceId.toString(16) | ||
| } | ||
|
|
||
| get spanId () { | ||
| return this._ddContext._spanId.toString(16) | ||
| } | ||
|
|
||
| get traceFlags () { | ||
| return this._ddContext._sampling.priority >= AUTO_KEEP ? 1 : 0 | ||
| } | ||
|
|
||
| get traceState () { | ||
| const ts = this._ddContext._tracestate | ||
| return api.createTraceState(ts ? ts.toString() : '') | ||
| } | ||
| } | ||
|
|
||
| module.exports = SpanContext |
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,50 @@ | ||
| 'use strict' | ||
|
|
||
| class NoopSpanProcessor { | ||
| forceFlush () { | ||
| return Promise.resolve() | ||
| } | ||
|
|
||
| onStart (span, context) { } | ||
| onEnd (span) { } | ||
|
|
||
| shutdown () { | ||
| return Promise.resolve() | ||
| } | ||
| } | ||
|
|
||
| class MultiSpanProcessor extends NoopSpanProcessor { | ||
| constructor (spanProcessors) { | ||
| super() | ||
| this._processors = spanProcessors | ||
| } | ||
|
|
||
| forceFlush () { | ||
| return Promise.all( | ||
| this._processors.map(p => p.forceFlush()) | ||
| ) | ||
| } | ||
|
|
||
| onStart (span, context) { | ||
| for (const processor of this._processors) { | ||
| processor.onStart(span, context) | ||
| } | ||
| } | ||
|
|
||
| onEnd (span) { | ||
| for (const processor of this._processors) { | ||
| processor.onEnd(span) | ||
| } | ||
| } | ||
|
|
||
| shutdown () { | ||
| return Promise.all( | ||
| this._processors.map(p => p.shutdown()) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| module.exports = { | ||
| MultiSpanProcessor, | ||
| NoopSpanProcessor | ||
| } |
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.