forked from open-telemetry/opentelemetry-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(propagation-utils): end pub-sub process span on promise settled (o…
…pen-telemetry#1055) Co-authored-by: Rauno Viskus <Rauno56@users.noreply.github.com> Co-authored-by: Rauno Viskus <rauno56@gmail.com> Co-authored-by: Valentin Marchaud <contact@vmarchaud.fr>
- Loading branch information
1 parent
227ae20
commit cb83d30
Showing
3 changed files
with
156 additions
and
2 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
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
131 changes: 131 additions & 0 deletions
131
packages/opentelemetry-propagation-utils/test/pubsub-propagation.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,131 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import utils from '../src/pubsub-propagation'; | ||
import { | ||
getTestSpans, | ||
registerInstrumentationTestingProvider, | ||
resetMemoryExporter, | ||
} from '@opentelemetry/contrib-test-utils'; | ||
import { ROOT_CONTEXT, trace } from '@opentelemetry/api'; | ||
import * as expect from 'expect'; | ||
|
||
registerInstrumentationTestingProvider(); | ||
|
||
const tracer = trace.getTracer('test'); | ||
|
||
afterEach(() => { | ||
resetMemoryExporter(); | ||
}); | ||
|
||
describe('Pubsub propagation', () => { | ||
it('Span ends immediately when the function returns a non-promise', () => { | ||
const messages = [{}]; | ||
utils.patchMessagesArrayToStartProcessSpans({ | ||
messages, | ||
tracer, | ||
parentContext: ROOT_CONTEXT, | ||
messageToSpanDetails: () => ({ | ||
name: 'test', | ||
parentContext: ROOT_CONTEXT, | ||
attributes: {}, | ||
}), | ||
}); | ||
utils.patchArrayForProcessSpans(messages, tracer, ROOT_CONTEXT); | ||
|
||
expect(getTestSpans().length).toBe(0); | ||
|
||
messages.map(x => x); | ||
|
||
expect(getTestSpans().length).toBe(1); | ||
expect(getTestSpans()[0]).toMatchObject({ name: 'test process' }); | ||
}); | ||
|
||
it('Span ends on promise-resolution', () => { | ||
const messages = [{}]; | ||
utils.patchMessagesArrayToStartProcessSpans({ | ||
messages, | ||
tracer, | ||
parentContext: ROOT_CONTEXT, | ||
messageToSpanDetails: () => ({ | ||
name: 'test', | ||
parentContext: ROOT_CONTEXT, | ||
attributes: {}, | ||
}), | ||
}); | ||
utils.patchArrayForProcessSpans(messages, tracer, ROOT_CONTEXT); | ||
|
||
expect(getTestSpans().length).toBe(0); | ||
|
||
let resolve: (value: unknown) => void; | ||
|
||
messages.map( | ||
() => | ||
new Promise(res => { | ||
resolve = res; | ||
}) | ||
); | ||
|
||
expect(getTestSpans().length).toBe(0); | ||
|
||
// @ts-expect-error Typescript thinks this value is used before assignment | ||
resolve(undefined); | ||
|
||
// We use setTimeout here to make sure our assertations run | ||
// after the promise resolves | ||
return new Promise(res => setTimeout(res, 0)).then(() => { | ||
expect(getTestSpans().length).toBe(1); | ||
expect(getTestSpans()[0]).toMatchObject({ name: 'test process' }); | ||
}); | ||
}); | ||
|
||
it('Span ends on promise-rejection', () => { | ||
const messages = [{}]; | ||
utils.patchMessagesArrayToStartProcessSpans({ | ||
messages, | ||
tracer, | ||
parentContext: ROOT_CONTEXT, | ||
messageToSpanDetails: () => ({ | ||
name: 'test', | ||
parentContext: ROOT_CONTEXT, | ||
attributes: {}, | ||
}), | ||
}); | ||
utils.patchArrayForProcessSpans(messages, tracer, ROOT_CONTEXT); | ||
|
||
expect(getTestSpans().length).toBe(0); | ||
|
||
let reject: (value: unknown) => void; | ||
|
||
messages.map( | ||
() => | ||
new Promise((_, rej) => { | ||
reject = rej; | ||
}) | ||
); | ||
|
||
expect(getTestSpans().length).toBe(0); | ||
|
||
// @ts-expect-error Typescript thinks this value is used before assignment | ||
reject(new Error('Failed')); | ||
|
||
// We use setTimeout here to make sure our assertations run | ||
// after the promise resolves | ||
return new Promise(res => setTimeout(res, 0)).then(() => { | ||
expect(getTestSpans().length).toBe(1); | ||
expect(getTestSpans()[0]).toMatchObject({ name: 'test process' }); | ||
}); | ||
}); | ||
}); |