Skip to content
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

[EventHubs][DistributedTracing] add destination and peer.address attributes #10389

Merged
merged 5 commits into from
Aug 4, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion sdk/eventhub/event-hubs/src/diagnostics/messageSpan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,26 @@

import { getTracer } from "@azure/core-tracing";
import { Span, SpanContext, SpanKind } from "@opentelemetry/api";
import { EventHubConnectionConfig } from "@azure/core-amqp";

/**
* @internal
* @ignore
*/
export function createMessageSpan(parentSpan?: Span | SpanContext | null): Span {
export function createMessageSpan(
parentSpan?: Span | SpanContext | null,
eventHubConfig?: Pick<EventHubConnectionConfig, "entityPath" | "host">
): Span {
const tracer = getTracer();
const span = tracer.startSpan("Azure.EventHubs.message", {
kind: SpanKind.PRODUCER,
parent: parentSpan
});
span.setAttribute("az.namespace", "Microsoft.EventHub");
if (eventHubConfig) {
span.setAttribute("message_bus.destination", eventHubConfig.entityPath);
span.setAttribute("peer.address", eventHubConfig.host);
}

return span;
}
2 changes: 1 addition & 1 deletion sdk/eventhub/event-hubs/src/eventDataBatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ export class EventDataBatchImpl implements EventDataBatch {
);
let spanContext: SpanContext | undefined;
if (!previouslyInstrumented) {
const messageSpan = createMessageSpan(options.parentSpan);
const messageSpan = createMessageSpan(options.parentSpan, this._context.config);
eventData = instrumentEventData(eventData, messageSpan);
spanContext = messageSpan.context();
messageSpan.end();
Expand Down
5 changes: 4 additions & 1 deletion sdk/eventhub/event-hubs/src/eventHubProducerClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,10 @@ export class EventHubProducerClient {
for (let i = 0; i < batch.length; i++) {
const event = batch[i];
if (!event.properties || !event.properties[TRACEPARENT_PROPERTY]) {
const messageSpan = createMessageSpan(getParentSpan(options.tracingOptions));
const messageSpan = createMessageSpan(
getParentSpan(options.tracingOptions),
this._context.config
);
// since these message spans are created from same context as the send span,
// these message spans don't need to be linked.
// replace the original event with the instrumented one
Expand Down
72 changes: 72 additions & 0 deletions sdk/eventhub/event-hubs/test/diagnostics/messageSpan.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import chai from "chai";
import { createMessageSpan } from "../../src/diagnostics/messageSpan";
import { TraceFlags, SpanContext } from "@opentelemetry/api";
import { EventHubConnectionConfig } from '@azure/core-amqp';

const should = chai.should();

describe("#createMessageSpan()", () => {
const mockSpanContext: SpanContext = {
traceId: "d4cda95b652f4a1592b449d5929fda1b",
spanId: "6e0c63257de34c92",
traceFlags: TraceFlags.SAMPLED
};
const mockEventHubConnectionConfig: Pick<EventHubConnectionConfig, "entityPath" | "host"> = {
entityPath: "entity",
host: "foo.example.com"
};

it("should create a span without a parent", () => {
const span = createMessageSpan();

should.exist(span);
should.not.exist(span.context().traceId);
should.not.exist(span.context().spanId);

should.equal((span as any).name, "Azure.EventHubs.message");
should.equal((span as any).attributes, {
"az.namespace": "Microsoft.EventHub"
});
should.equal((span as any).ended, false);

span.end();
should.equal((span as any).ended, true);
});

it("should create a span with a parent", () => {
const span = createMessageSpan(mockSpanContext);

should.exist(span);
should.equal(span.context(), mockSpanContext);

should.equal((span as any).name, "Azure.EventHubs.message");
should.equal((span as any).attributes, {
"az.namespace": "Microsoft.EventHub"
});
should.equal((span as any).ended, false);

span.end();
should.equal((span as any).ended, true);
});

it("should create a span with an eventHubConfig", () => {
const span = createMessageSpan(mockSpanContext, mockEventHubConnectionConfig);

should.exist(span);
should.equal(span.context(), mockSpanContext);

should.equal((span as any).name, "Azure.EventHubs.message");
should.equal((span as any).attributes, {
"az.namespace": "Microsoft.EventHub",
"message_bus.destination": mockEventHubConnectionConfig.entityPath,
"peer.address": mockEventHubConnectionConfig.host
});
should.equal((span as any).ended, false);

span.end();
should.equal((span as any).ended, true);
});
});