Skip to content

Commit

Permalink
Add OpenTelemetry SpanKind as a magic attribute
Browse files Browse the repository at this point in the history
To identify the origin of the HTTP requests from their spans, the
`SpanKind` attribute is needed. As adding a completely new attribute
will add a lot of overhead requiring modifications in all steps, the
attribute is now added from the SpanProcessor as a magic attribute.

More info on OpenTelemetry's SpanKind:
https://opentelemetry.io/docs/reference/specification/trace/api/#spankind
  • Loading branch information
luismiramirez committed Oct 24, 2022
1 parent 3a35552 commit bcf182b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
bump: "patch"
type: "add"
---

Add OpenTelemetry SpanKind as a magic attribute
16 changes: 16 additions & 0 deletions src/__tests__/span_processor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,20 @@ describe("Span processor", () => {
})
)
})

it("gets the span kind as an attribute", () => {
tracer.startSpan("kindSpan").end()

expect(SpanTestRegistry.count()).toEqual(1)
expect(SpanTestRegistry.lastSpan()?.toObject()).toMatchObject({
name: "kindSpan",
closed: true,
attributes: expect.objectContaining({
"appsignal:category": "unknown",
"appsignal.kind": "INTERNAL"
}),
parent_span_id: "",
error: null
})
})
})
9 changes: 8 additions & 1 deletion src/span_processor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as fs from "fs"
import type { Context } from "@opentelemetry/api"
import { SpanKind } from "@opentelemetry/api"
import type {
Span,
ReadableSpan,
Expand All @@ -22,6 +23,12 @@ export class SpanProcessor implements OpenTelemetrySpanProcessor {
onStart(_span: Span, _parentContext: Context): void {}

onEnd(span: ReadableSpan): void {
// Add OpenTelemetry kind enum value as a magic attribute
const spanAttributes = {
...span.attributes,
"appsignal.kind": SpanKind[span.kind]
}

const opentelemetrySpan = this.client.extension.createOpenTelemetrySpan(
span.spanContext().spanId,
span.parentSpanId || "",
Expand All @@ -31,7 +38,7 @@ export class SpanProcessor implements OpenTelemetrySpanProcessor {
span.endTime[0],
span.endTime[1],
span.name,
span.attributes,
spanAttributes,
span.instrumentationLibrary.name
)

Expand Down

0 comments on commit bcf182b

Please sign in to comment.