Skip to content

Commit

Permalink
[EventHubs][DistributedTracing] add destination and peer.address attr…
Browse files Browse the repository at this point in the history
…ibutes (#10389)
  • Loading branch information
markwolff authored Aug 4, 2020
1 parent a3111ec commit ae9b199
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 3 deletions.
6 changes: 6 additions & 0 deletions sdk/eventhub/event-hubs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
where events could be _skipped_ while receiving messages.
Previously this could occur when a retryable error was encountered and retries were exhausted while receiving a batch of events.

### Tracing updates:

- Addresses [#10276](https://github.com/Azure/azure-sdk-for-js/issues/10276): adds
`message_bus.destination` and `peer.address` attributes to `Azure.EventHubs.message` spans.
([PR 10389](https://github.com/Azure/azure-sdk-for-js/pull/10389))

## 5.3.0-preview.1 (2020-07-07)

- Adds `loadBalancingOptions` to the `EventHubConsumerClient` to add control around
Expand Down
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
82 changes: 82 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,82 @@
// 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 { TestTracer, setTracer, getTracer } from "@azure/core-tracing";
import { EventHubConnectionConfig } from '@azure/core-amqp';

const should = chai.should();
const assert = chai.assert;

describe("#createMessageSpan()", () => {
const origTracer = getTracer();

before(() => {
setTracer(new TestTracer());
});

after(() => {
setTracer(origTracer);
});

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.exist(span.context().spanId);
should.exist(span.context().traceId);

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

span.end();
});

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

should.exist(span);
should.equal(span.context().traceId, mockSpanContext.traceId);
should.exist(span.context().spanId);
should.not.equal(span.context().spanId, mockSpanContext.spanId);

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

span.end();
});

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

should.exist(span);
should.equal(span.context().traceId, mockSpanContext.traceId);
should.exist(span.context().spanId);
should.not.equal(span.context().spanId, mockSpanContext.spanId);

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

span.end();
});
});

0 comments on commit ae9b199

Please sign in to comment.