-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[eventhubs] - Upgrade Event Hubs to core-tracing preview.14 #20240
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,136 +1,37 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT license. | ||
|
|
||
| import { | ||
| Span, | ||
| SpanContext, | ||
| SpanKind, | ||
| SpanOptions, | ||
| context, | ||
| createSpanFunction, | ||
| setSpan, | ||
| setSpanContext, | ||
| } from "@azure/core-tracing"; | ||
| import { createTracingClient, TracingSpanOptions, TracingSpanKind } from "@azure/core-tracing"; | ||
| import { EventHubConnectionConfig } from "../eventhubConnectionConfig"; | ||
| import { OperationOptions } from "../util/operationOptions"; | ||
| import { TryAddOptions } from "../eventDataBatch"; | ||
| import { packageJsonInfo } from "../util/constants"; | ||
|
|
||
| const _createSpan = createSpanFunction({ | ||
| /** | ||
| * The {@link TracingClient} that is used to add tracing spans. | ||
| */ | ||
| export const tracingClient = createTracingClient({ | ||
| namespace: "Microsoft.EventHub", | ||
| packagePrefix: "Azure.EventHubs", | ||
| packageName: packageJsonInfo.name, | ||
| packageVersion: packageJsonInfo.version, | ||
| }); | ||
|
|
||
| /** | ||
| * Creates an EventHubs specific span, with peer.address and message_bus.destination filled out. | ||
| * @internal | ||
| * Creates {@link TracingSpanOptions} from the provided data. | ||
| * @param eventHubConfig - The configuration object containing initial attributes to set on the span. | ||
| * @param spanKind - The {@link TracingSpanKind} for the newly created span. | ||
| * @returns a {@link TracingSpanOptions} that can be passed to a {@link TracingClient} | ||
| */ | ||
| export function createEventHubSpan( | ||
| operationName: string, | ||
| operationOptions: OperationOptions | undefined, | ||
| connectionConfig: Pick<EventHubConnectionConfig, "entityPath" | "host">, | ||
| additionalSpanOptions?: SpanOptions | ||
| ): { span: Span; updatedOptions: OperationOptions } { | ||
| const { span, updatedOptions } = _createSpan(operationName, { | ||
| ...operationOptions, | ||
| tracingOptions: { | ||
| ...operationOptions?.tracingOptions, | ||
| spanOptions: { | ||
| // By passing spanOptions if they exist at runtime, we're backwards compatible with @azure/core-tracing@preview.13 and earlier. | ||
| ...(operationOptions?.tracingOptions as any)?.spanOptions, | ||
| ...additionalSpanOptions, | ||
| }, | ||
| export function toSpanOptions( | ||
| eventHubConfig: Pick<EventHubConnectionConfig, "entityPath" | "host">, | ||
| spanKind?: TracingSpanKind | ||
| ): TracingSpanOptions { | ||
| const spanOptions: TracingSpanOptions = { | ||
| spanAttributes: { | ||
| "message_bus.destination": eventHubConfig.entityPath, | ||
| "peer.address": eventHubConfig.host, | ||
| }, | ||
| }); | ||
|
|
||
| span.setAttribute("message_bus.destination", connectionConfig.entityPath); | ||
| span.setAttribute("peer.address", connectionConfig.host); | ||
|
|
||
| return { | ||
| span, | ||
| updatedOptions, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| export function createMessageSpan( | ||
| operationOptions: OperationOptions, | ||
| eventHubConfig: Pick<EventHubConnectionConfig, "entityPath" | "host"> | ||
| ): ReturnType<typeof createEventHubSpan> { | ||
| return createEventHubSpan("message", operationOptions, eventHubConfig, { | ||
| kind: SpanKind.PRODUCER, | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Converts TryAddOptions into the modern shape (OperationOptions) when needed. | ||
| * (this is something we can eliminate at the next major release of EH _or_ when | ||
| * we release with the GA version of opentelemetry). | ||
| * | ||
| * @internal | ||
| */ | ||
| export function convertTryAddOptionsForCompatibility(tryAddOptions: TryAddOptions): TryAddOptions { | ||
|
Member
Author
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. What this does if I understand it correctly is that it supports a few obsolete span parenting options. In general our policy has been that it's unnecessary to keep backwards compat for tracing options, which is what this function does, at least while tracing is in preview. |
||
| /* eslint-disable-next-line @typescript-eslint/ban-ts-comment */ | ||
| // @ts-ignore: parentSpan is deprecated and this is compat code to translate it until we can get rid of it. | ||
| const legacyParentSpanOrSpanContext = tryAddOptions.parentSpan; | ||
|
|
||
| /* | ||
| Our goal here is to offer compatibility but there is a case where a user might accidentally pass | ||
| _both_ sets of options. We'll assume they want the OperationTracingOptions code path in that case. | ||
|
|
||
| Example of accidental span passing: | ||
|
|
||
| const someOptionsPassedIntoTheirFunction = { | ||
| parentSpan: span; // set somewhere else in their code | ||
| } | ||
|
|
||
| function takeSomeOptionsFromSomewhere(someOptionsPassedIntoTheirFunction) { | ||
|
|
||
| batch.tryAddMessage(message, { | ||
| // "runtime" blend of options from some other part of their app | ||
| ...someOptionsPassedIntoTheirFunction, // parentSpan comes along for the ride... | ||
|
|
||
| tracingOptions: { | ||
| // thank goodness, I'm doing this right! (thinks the developer) | ||
| spanOptions: { | ||
| context: context | ||
| } | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| And now they've accidentally been opted into the legacy code path even though they think | ||
| they're using the modern code path. | ||
|
|
||
| This does kick the can down the road a bit - at some point we will be putting them in this | ||
| situation where things looked okay but their spans are becoming unparented but we can | ||
| try to announce this (and other changes related to tracing) in our next big rev. | ||
| */ | ||
|
|
||
| if (!legacyParentSpanOrSpanContext || tryAddOptions.tracingOptions) { | ||
| // assume that the options are already in the modern shape even if (possibly) | ||
| // they were still specifying `parentSpan` | ||
| return tryAddOptions; | ||
| } | ||
|
|
||
| const convertedOptions: TryAddOptions = { | ||
| ...tryAddOptions, | ||
| tracingOptions: { | ||
| tracingContext: isSpan(legacyParentSpanOrSpanContext) | ||
| ? setSpan(context.active(), legacyParentSpanOrSpanContext) | ||
| : setSpanContext(context.active(), legacyParentSpanOrSpanContext), | ||
| }, | ||
| }; | ||
|
|
||
| return convertedOptions; | ||
| } | ||
|
|
||
| function isSpan(possibleSpan: Span | SpanContext | undefined): possibleSpan is Span { | ||
| if (possibleSpan == null) { | ||
| return false; | ||
| if (spanKind) { | ||
| spanOptions.spanKind = spanKind; | ||
| } | ||
|
|
||
| const x = possibleSpan as Span; | ||
| return typeof x.spanContext === "function"; | ||
| return spanOptions; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.