-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(opentelemetry): Add
addLink(s)
to span (#15387)
Link spans which are related. Example: ```javascript const span1 = startInactiveSpan({ name: 'span1' }); startSpan({ name: 'span2' }, span2 => { span2.addLink({ context: span1.spanContext(), attributes: { 'sentry.link.type': 'previous_trace' }, }); ```
- Loading branch information
Showing
11 changed files
with
374 additions
and
4 deletions.
There are no files selected for viewing
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
33 changes: 33 additions & 0 deletions
33
dev-packages/node-integration-tests/suites/tracing/linking/scenario-addLink-nested.ts
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,33 @@ | ||
import { loggingTransport } from '@sentry-internal/node-integration-tests'; | ||
import * as Sentry from '@sentry/node'; | ||
|
||
Sentry.init({ | ||
dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
release: '1.0', | ||
tracesSampleRate: 1.0, | ||
integrations: [], | ||
transport: loggingTransport, | ||
}); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
Sentry.startSpan({ name: 'parent1' }, async parentSpan1 => { | ||
// eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
Sentry.startSpan({ name: 'child1.1' }, async childSpan1 => { | ||
childSpan1.addLink({ | ||
context: parentSpan1.spanContext(), | ||
attributes: { 'sentry.link.type': 'previous_trace' }, | ||
}); | ||
|
||
childSpan1.end(); | ||
}); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
Sentry.startSpan({ name: 'child1.2' }, async childSpan2 => { | ||
childSpan2.addLink({ | ||
context: parentSpan1.spanContext(), | ||
attributes: { 'sentry.link.type': 'previous_trace' }, | ||
}); | ||
|
||
childSpan2.end(); | ||
}); | ||
}); |
20 changes: 20 additions & 0 deletions
20
dev-packages/node-integration-tests/suites/tracing/linking/scenario-addLink.ts
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,20 @@ | ||
import { loggingTransport } from '@sentry-internal/node-integration-tests'; | ||
import * as Sentry from '@sentry/node'; | ||
|
||
Sentry.init({ | ||
dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
release: '1.0', | ||
tracesSampleRate: 1.0, | ||
integrations: [], | ||
transport: loggingTransport, | ||
}); | ||
|
||
const span1 = Sentry.startInactiveSpan({ name: 'span1' }); | ||
span1.end(); | ||
|
||
Sentry.startSpan({ name: 'rootSpan' }, rootSpan => { | ||
rootSpan.addLink({ | ||
context: span1.spanContext(), | ||
attributes: { 'sentry.link.type': 'previous_trace' }, | ||
}); | ||
}); |
31 changes: 31 additions & 0 deletions
31
dev-packages/node-integration-tests/suites/tracing/linking/scenario-addLinks-nested.ts
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,31 @@ | ||
import { loggingTransport } from '@sentry-internal/node-integration-tests'; | ||
import * as Sentry from '@sentry/node'; | ||
|
||
Sentry.init({ | ||
dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
release: '1.0', | ||
tracesSampleRate: 1.0, | ||
integrations: [], | ||
transport: loggingTransport, | ||
}); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
Sentry.startSpan({ name: 'parent1' }, async parentSpan1 => { | ||
// eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
Sentry.startSpan({ name: 'child1.1' }, async childSpan1 => { | ||
// eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
Sentry.startSpan({ name: 'child2.1' }, async childSpan2 => { | ||
childSpan2.addLinks([ | ||
{ context: parentSpan1.spanContext() }, | ||
{ | ||
context: childSpan1.spanContext(), | ||
attributes: { 'sentry.link.type': 'previous_trace' }, | ||
}, | ||
]); | ||
|
||
childSpan2.end(); | ||
}); | ||
|
||
childSpan1.end(); | ||
}); | ||
}); |
26 changes: 26 additions & 0 deletions
26
dev-packages/node-integration-tests/suites/tracing/linking/scenario-addLinks.ts
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,26 @@ | ||
import { loggingTransport } from '@sentry-internal/node-integration-tests'; | ||
import * as Sentry from '@sentry/node'; | ||
|
||
Sentry.init({ | ||
dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
release: '1.0', | ||
tracesSampleRate: 1.0, | ||
integrations: [], | ||
transport: loggingTransport, | ||
}); | ||
|
||
const span1 = Sentry.startInactiveSpan({ name: 'span1' }); | ||
span1.end(); | ||
|
||
const span2 = Sentry.startInactiveSpan({ name: 'span2' }); | ||
span2.end(); | ||
|
||
Sentry.startSpan({ name: 'rootSpan' }, rootSpan => { | ||
rootSpan.addLinks([ | ||
{ context: span1.spanContext() }, | ||
{ | ||
context: span2.spanContext(), | ||
attributes: { 'sentry.link.type': 'previous_trace' }, | ||
}, | ||
]); | ||
}); |
157 changes: 157 additions & 0 deletions
157
dev-packages/node-integration-tests/suites/tracing/linking/test.ts
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,157 @@ | ||
import { createRunner } from '../../../utils/runner'; | ||
|
||
describe('span links', () => { | ||
test('should link spans with addLink() in trace context', done => { | ||
let span1_traceId: string, span1_spanId: string; | ||
|
||
createRunner(__dirname, 'scenario-addLink.ts') | ||
.expect({ | ||
transaction: event => { | ||
expect(event.transaction).toBe('span1'); | ||
|
||
span1_traceId = event.contexts?.trace?.trace_id as string; | ||
span1_spanId = event.contexts?.trace?.span_id as string; | ||
|
||
expect(event.spans).toEqual([]); | ||
}, | ||
}) | ||
.expect({ | ||
transaction: event => { | ||
expect(event.transaction).toBe('rootSpan'); | ||
|
||
expect(event.contexts?.trace?.links).toEqual([ | ||
expect.objectContaining({ | ||
trace_id: expect.stringMatching(span1_traceId), | ||
span_id: expect.stringMatching(span1_spanId), | ||
attributes: expect.objectContaining({ | ||
'sentry.link.type': 'previous_trace', | ||
}), | ||
}), | ||
]); | ||
}, | ||
}) | ||
.start(done); | ||
}); | ||
|
||
test('should link spans with addLinks() in trace context', done => { | ||
let span1_traceId: string, span1_spanId: string, span2_traceId: string, span2_spanId: string; | ||
|
||
createRunner(__dirname, 'scenario-addLinks.ts') | ||
.expect({ | ||
transaction: event => { | ||
expect(event.transaction).toBe('span1'); | ||
|
||
span1_traceId = event.contexts?.trace?.trace_id as string; | ||
span1_spanId = event.contexts?.trace?.span_id as string; | ||
|
||
expect(event.spans).toEqual([]); | ||
}, | ||
}) | ||
.expect({ | ||
transaction: event => { | ||
expect(event.transaction).toBe('span2'); | ||
|
||
span2_traceId = event.contexts?.trace?.trace_id as string; | ||
span2_spanId = event.contexts?.trace?.span_id as string; | ||
|
||
expect(event.spans).toEqual([]); | ||
}, | ||
}) | ||
.expect({ | ||
transaction: event => { | ||
expect(event.transaction).toBe('rootSpan'); | ||
|
||
expect(event.contexts?.trace?.links).toEqual([ | ||
expect.not.objectContaining({ attributes: expect.anything() }) && | ||
expect.objectContaining({ | ||
trace_id: expect.stringMatching(span1_traceId), | ||
span_id: expect.stringMatching(span1_spanId), | ||
}), | ||
expect.objectContaining({ | ||
trace_id: expect.stringMatching(span2_traceId), | ||
span_id: expect.stringMatching(span2_spanId), | ||
attributes: expect.objectContaining({ | ||
'sentry.link.type': 'previous_trace', | ||
}), | ||
}), | ||
]); | ||
}, | ||
}) | ||
.start(done); | ||
}); | ||
|
||
test('should link spans with addLink() in nested startSpan() calls', done => { | ||
createRunner(__dirname, 'scenario-addLink-nested.ts') | ||
.expect({ | ||
transaction: event => { | ||
expect(event.transaction).toBe('parent1'); | ||
|
||
const parent1_traceId = event.contexts?.trace?.trace_id as string; | ||
const parent1_spanId = event.contexts?.trace?.span_id as string; | ||
|
||
const spans = event.spans || []; | ||
const child1_1 = spans.find(span => span.description === 'child1.1'); | ||
const child1_2 = spans.find(span => span.description === 'child1.2'); | ||
|
||
expect(child1_1).toBeDefined(); | ||
expect(child1_1?.links).toEqual([ | ||
expect.objectContaining({ | ||
trace_id: expect.stringMatching(parent1_traceId), | ||
span_id: expect.stringMatching(parent1_spanId), | ||
attributes: expect.objectContaining({ | ||
'sentry.link.type': 'previous_trace', | ||
}), | ||
}), | ||
]); | ||
|
||
expect(child1_2).toBeDefined(); | ||
expect(child1_2?.links).toEqual([ | ||
expect.objectContaining({ | ||
trace_id: expect.stringMatching(parent1_traceId), | ||
span_id: expect.stringMatching(parent1_spanId), | ||
attributes: expect.objectContaining({ | ||
'sentry.link.type': 'previous_trace', | ||
}), | ||
}), | ||
]); | ||
}, | ||
}) | ||
.start(done); | ||
}); | ||
|
||
test('should link spans with addLinks() in nested startSpan() calls', done => { | ||
createRunner(__dirname, 'scenario-addLinks-nested.ts') | ||
.expect({ | ||
transaction: event => { | ||
expect(event.transaction).toBe('parent1'); | ||
|
||
const parent1_traceId = event.contexts?.trace?.trace_id as string; | ||
const parent1_spanId = event.contexts?.trace?.span_id as string; | ||
|
||
const spans = event.spans || []; | ||
const child1_1 = spans.find(span => span.description === 'child1.1'); | ||
const child2_1 = spans.find(span => span.description === 'child2.1'); | ||
|
||
expect(child1_1).toBeDefined(); | ||
|
||
expect(child2_1).toBeDefined(); | ||
|
||
expect(child2_1?.links).toEqual([ | ||
expect.not.objectContaining({ attributes: expect.anything() }) && | ||
expect.objectContaining({ | ||
trace_id: expect.stringMatching(parent1_traceId), | ||
span_id: expect.stringMatching(parent1_spanId), | ||
}), | ||
expect.objectContaining({ | ||
trace_id: expect.stringMatching(child1_1?.trace_id || 'non-existent-id-fallback'), | ||
span_id: expect.stringMatching(child1_1?.span_id || 'non-existent-id-fallback'), | ||
attributes: expect.objectContaining({ | ||
'sentry.link.type': 'previous_trace', | ||
}), | ||
}), | ||
]); | ||
}, | ||
}) | ||
.start(done); | ||
}); | ||
}); |
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.