-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Service Bus] Tracing for send API (#11651)
- Loading branch information
1 parent
d77196b
commit 3448028
Showing
14 changed files
with
793 additions
and
92 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
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
56 changes: 56 additions & 0 deletions
56
sdk/servicebus/service-bus/src/diagnostics/instrumentServiceBusMessage.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,56 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
import { extractSpanContextFromTraceParentHeader, getTraceParentHeader } from "@azure/core-tracing"; | ||
import { Span, SpanContext } from "@opentelemetry/api"; | ||
import { ServiceBusMessage } from "../serviceBusMessage"; | ||
|
||
/** | ||
* @ignore | ||
*/ | ||
export const TRACEPARENT_PROPERTY = "Diagnostic-Id"; | ||
|
||
/** | ||
* Populates the `ServiceBusMessage` with `SpanContext` info to support trace propagation. | ||
* Creates and returns a copy of the passed in `ServiceBusMessage` unless the `ServiceBusMessage` | ||
* has already been instrumented. | ||
* @param message The `ServiceBusMessage` to instrument. | ||
* @param span The `Span` containing the context to propagate tracing information. | ||
* @ignore | ||
* @internal | ||
*/ | ||
export function instrumentServiceBusMessage( | ||
message: ServiceBusMessage, | ||
span: Span | ||
): ServiceBusMessage { | ||
if (message.properties && message.properties[TRACEPARENT_PROPERTY]) { | ||
return message; | ||
} | ||
|
||
// create a copy so the original isn't modified | ||
message = { ...message, properties: { ...message.properties } }; | ||
|
||
const traceParent = getTraceParentHeader(span.context()); | ||
if (traceParent) { | ||
message.properties![TRACEPARENT_PROPERTY] = traceParent; | ||
} | ||
|
||
return message; | ||
} | ||
|
||
/** | ||
* Extracts the `SpanContext` from an `ServiceBusMessage` if the context exists. | ||
* @param message An individual `ServiceBusMessage` object. | ||
* @internal | ||
* @ignore | ||
*/ | ||
export function extractSpanContextFromServiceBusMessage( | ||
message: ServiceBusMessage | ||
): SpanContext | undefined { | ||
if (!message.properties || !message.properties[TRACEPARENT_PROPERTY]) { | ||
return; | ||
} | ||
|
||
const diagnosticId = message.properties[TRACEPARENT_PROPERTY] as string; | ||
return extractSpanContextFromTraceParentHeader(diagnosticId); | ||
} |
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,27 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
import { ConnectionConfig } from "@azure/core-amqp"; | ||
import { getTracer } from "@azure/core-tracing"; | ||
import { Span, SpanContext, SpanKind } from "@opentelemetry/api"; | ||
|
||
/** | ||
* @internal | ||
* @ignore | ||
*/ | ||
export function createMessageSpan( | ||
parentSpan?: Span | SpanContext | null, | ||
config?: Pick<ConnectionConfig, "entityPath" | "host"> | ||
): Span { | ||
const tracer = getTracer(); | ||
const span = tracer.startSpan("Azure.ServiceBus.message", { | ||
kind: SpanKind.PRODUCER, | ||
parent: parentSpan | ||
}); | ||
span.setAttribute("az.namespace", "Microsoft.ServiceBus"); | ||
if (config) { | ||
span.setAttribute("message_bus.destination", config.entityPath); | ||
span.setAttribute("peer.address", config.host); | ||
} | ||
return span; | ||
} |
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.