-
Notifications
You must be signed in to change notification settings - Fork 0
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
chore: more advanced types for sync/async resource #2
Changes from 2 commits
799fdfb
b368d32
8f876a3
cc645c3
221ced8
f612922
8224e1f
644edc3
4f991d0
da0287c
27e6c2d
cb58e7a
2916ecf
d511a0e
b884eec
739462d
1f8c365
f402596
6412688
b6f0208
d68ff0f
ddd84ff
a3357d5
a576c3f
b2eedfb
2a8555f
fb06b5b
2ee9f1a
7ef38a1
d8bff27
47f4ce1
53f0654
03a8ee1
2052a24
5c7753f
e440d5e
c5294ba
40242ae
a7a6b7f
b88b95b
cb6555a
c06f31e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,15 +23,14 @@ import { | |
timeInputToHrTime, | ||
} from '@opentelemetry/core'; | ||
import { Resource } from '@opentelemetry/resources'; | ||
import { ReadableSpan } from './export/ReadableSpan'; | ||
import { Tracer } from './Tracer'; | ||
import { SpanProcessor } from './SpanProcessor'; | ||
import { Tracer } from './Tracer'; | ||
import { TraceParams } from './types'; | ||
|
||
/** | ||
* This class represents a span. | ||
*/ | ||
export class Span implements api.Span, ReadableSpan { | ||
export class Span implements api.Span { | ||
// Below properties are included to implement ReadableSpan for export | ||
// purposes but are not intended to be written-to directly. | ||
readonly spanContext: api.SpanContext; | ||
|
@@ -41,7 +40,7 @@ export class Span implements api.Span, ReadableSpan { | |
readonly links: api.Link[] = []; | ||
readonly events: api.TimedEvent[] = []; | ||
readonly startTime: api.HrTime; | ||
resource: Resource; | ||
private _resource: Resource | Promise<Resource>; | ||
readonly instrumentationLibrary: InstrumentationLibrary; | ||
name: string; | ||
status: api.Status = { | ||
|
@@ -70,12 +69,15 @@ export class Span implements api.Span, ReadableSpan { | |
this.kind = kind; | ||
this.links = links; | ||
this.startTime = timeInputToHrTime(startTime); | ||
this.resource = parentTracer.resource; | ||
this._resource = parentTracer.resource; | ||
this.instrumentationLibrary = parentTracer.instrumentationLibrary; | ||
this._logger = parentTracer.logger; | ||
this._traceParams = parentTracer.getActiveTraceParams(); | ||
this._spanProcessor = parentTracer.getActiveSpanProcessor(); | ||
this._spanProcessor.onStart(this); | ||
|
||
this._resolveResource().then(() => { | ||
this._spanProcessor.onStart(this); | ||
}); | ||
} | ||
|
||
context(): api.SpanContext { | ||
|
@@ -171,15 +173,9 @@ export class Span implements api.Span, ReadableSpan { | |
); | ||
} | ||
|
||
if (this.resource instanceof Promise) { | ||
this.resource.then(resource => { | ||
this.resource = resource; | ||
this._spanProcessor.onEnd(this); | ||
}); | ||
return; | ||
} | ||
|
||
this._spanProcessor.onEnd(this); | ||
this._resolveResource().then(() => { | ||
this._spanProcessor.onEnd(this); | ||
}); | ||
} | ||
|
||
isRecording(): boolean { | ||
|
@@ -194,6 +190,13 @@ export class Span implements api.Span, ReadableSpan { | |
return this._ended; | ||
} | ||
|
||
get resource(): Resource { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the promise is resolved (after _resolveResources completes), then this will act like a regular sync resource to satisfy the ReadableSpan interface. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Imagine the following situation
Another situation
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Checking for resource isn't even a part of the public API until we get a "Readable Span", which is not the same as a span. It might be easier if we just send a plain object instead of the actual Span with a getter?
No, the export will still have the correct resource as the span will not be sent to the exporter until the resource is resolved, and getters don't cache. Try it if you don't believe me.
Why? The span isn't actually exported until the resource resolves. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wrote you a message on gitter 4 days ago, please read :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. D: ok thanks Too many communication channels to keep track of |
||
if (this._resource instanceof Promise) { | ||
return Resource.createTelemetrySDKResource(); | ||
} | ||
return this._resource; | ||
} | ||
|
||
private _isSpanEnded(): boolean { | ||
if (this._ended) { | ||
this._logger.warn( | ||
|
@@ -204,4 +207,12 @@ export class Span implements api.Span, ReadableSpan { | |
} | ||
return this._ended; | ||
} | ||
|
||
private async _resolveResource() { | ||
try { | ||
this._resource = await this._resource; | ||
} catch (err) { | ||
this._logger.error(`Resource failed to resolve: ${err?.message}`); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why did you remove ReadableSpan ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can be added back.