-
Notifications
You must be signed in to change notification settings - Fork 821
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
feat(shim-opentracing): update logging based on new spec #2282
Changes from 1 commit
f891a4d
dd9df52
4b1ad09
8cc0ddd
6bd55df
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 | ||
---|---|---|---|---|
|
@@ -15,13 +15,9 @@ | |||
*/ | ||||
|
||||
import * as api from '@opentelemetry/api'; | ||||
import { SpanAttributes, SpanAttributeValue, SpanStatusCode, TextMapPropagator } from '@opentelemetry/api'; | ||||
import * as opentracing from 'opentracing'; | ||||
import { | ||||
SpanAttributes, | ||||
SpanAttributeValue, | ||||
SpanStatusCode, | ||||
TextMapPropagator, | ||||
} from '@opentelemetry/api'; | ||||
import { SemanticAttributes } from "@opentelemetry/semantic-conventions"; | ||||
|
||||
function translateReferences(references: opentracing.Reference[]): api.Link[] { | ||||
const links: api.Link[] = []; | ||||
|
@@ -96,14 +92,14 @@ export class SpanContextShim extends opentracing.SpanContext { | |||
/** | ||||
* Returns the trace ID as a string. | ||||
*/ | ||||
override toTraceId(): string { | ||||
override toTraceId(): string { | ||||
return this._spanContext.traceId; | ||||
} | ||||
|
||||
/** | ||||
* Returns the span ID as a string. | ||||
*/ | ||||
override toSpanId(): string { | ||||
override toSpanId(): string { | ||||
return this._spanContext.spanId; | ||||
} | ||||
|
||||
|
@@ -281,19 +277,62 @@ export class SpanShim extends opentracing.Span { | |||
* @param payload an arbitrary object to be attached to the event. | ||||
*/ | ||||
override logEvent(eventName: string, payload?: SpanAttributes): void { | ||||
this._span.addEvent(eventName, payload); | ||||
this._logInternal(eventName, payload); | ||||
} | ||||
|
||||
/** | ||||
* Logs a set of key value pairs. Since OpenTelemetry only supports events, | ||||
* the KV pairs are used as attributes on an event named "log". | ||||
* the KV pairs are used as attributes on a Span event. | ||||
* @param keyValuePairs a set of key-value pairs to be used as event attributes | ||||
* @param timestamp optional timestamp for the event | ||||
*/ | ||||
override log(keyValuePairs: SpanAttributes, _timestamp?: number): this { | ||||
// @todo: Handle timestamp | ||||
this._span.addEvent('log', keyValuePairs); | ||||
override log(keyValuePairs: SpanAttributes, timestamp?: number): this { | ||||
const entries = Object.entries(keyValuePairs); | ||||
const eventEntry = entries.find(([key, _]) => key === 'event'); | ||||
const eventName = eventEntry?.[1] || 'log'; | ||||
const name = eventName.toString(); | ||||
|
||||
this._logInternal(name, keyValuePairs, timestamp); | ||||
return this; | ||||
} | ||||
|
||||
private _logInternal(eventName: string, attributes: SpanAttributes | undefined, timestamp?: number): void { | ||||
if (attributes && eventName === 'error') { | ||||
const entries = Object.entries(attributes); | ||||
const errorEntry = entries.find(([key]) => key === 'error.object'); | ||||
const error = errorEntry?.[1]; | ||||
if (typeof error === "string") { | ||||
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. do you mean the error type from our api or from opentracing conventions ? Because i believe we handle error object too ? https://github.com/open-telemetry/opentelemetry-js/blob/main/packages/opentelemetry-tracing/src/Span.ts#L189 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. You are correct,
The issue here is that the shim restricts the types more than the OpenTracing lib: https://github.com/opentracing/opentracing-javascript/blob/111ea4f7939c8f8f538333330d72115e5b28bcce/src/span.ts#L143 where you can pass |
||||
this._span.recordException(error, timestamp); | ||||
return; | ||||
} | ||||
|
||||
const mappedAttributes: api.SpanAttributes = {}; | ||||
for (const [k, v] of entries) { | ||||
switch (k) { | ||||
case "error.kind": { | ||||
mappedAttributes[SemanticAttributes.EXCEPTION_TYPE] = v; | ||||
break; | ||||
} | ||||
case "message": { | ||||
mappedAttributes[SemanticAttributes.EXCEPTION_MESSAGE] = v; | ||||
break; | ||||
} | ||||
case "stack": { | ||||
mappedAttributes[SemanticAttributes.EXCEPTION_STACKTRACE] = v; | ||||
break; | ||||
} | ||||
default: { | ||||
mappedAttributes[k] = v; | ||||
break; | ||||
} | ||||
} | ||||
} | ||||
this._span.addEvent('exception', mappedAttributes, timestamp); | ||||
return; | ||||
} | ||||
this._span.addEvent(eventName, attributes, timestamp); | ||||
} | ||||
|
||||
/** | ||||
* Adds a set of tags to the span. | ||||
* @param keyValueMap set of KV pairs representing tags | ||||
|
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.
this will no longer register any default instrumentations so it can be removed or some default instrumentations should be added here
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.
Updated! Removed the line, since server.js and client.js generate spans inline.