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

[Event Hubs] add tracing support when sending events #5207

Merged
merged 9 commits into from
Oct 1, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
51 changes: 25 additions & 26 deletions sdk/core/core-tracing/api-extractor.json
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
"mainEntryPointFilePath": "types/index.d.ts",
"docModel": {
"enabled": false
},
"apiReport": {
"enabled": true,
"reportFolder": "./review"
},
"dtsRollup": {
"enabled": true,
"untrimmedFilePath": "",
"publicTrimmedFilePath": "./types/core-tracing.d.ts"
"$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
"mainEntryPointFilePath": "types/lib/index.d.ts",
"docModel": {
"enabled": false
},
"apiReport": {
"enabled": true,
"reportFolder": "./review"
},
"dtsRollup": {
"enabled": true,
"untrimmedFilePath": "",
"publicTrimmedFilePath": "./types/core-tracing.d.ts"
},
"messages": {
"tsdocMessageReporting": {
"default": {
"logLevel": "none"
}
},
"messages": {
"tsdocMessageReporting": {
"default": {
"logLevel": "none"
}
"extractorMessageReporting": {
"ae-missing-release-tag": {
"logLevel": "none"
},
"extractorMessageReporting": {
"ae-missing-release-tag": {
"logLevel": "none"
},
"ae-unresolved-link": {
"logLevel": "none"
}
"ae-unresolved-link": {
"logLevel": "none"
}
}
}
}
6 changes: 6 additions & 0 deletions sdk/core/core-tracing/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ export { TraceFlags } from "./interfaces/trace_flags";
export { TraceState } from "./interfaces/trace_state";
export { Tracer } from "./interfaces/tracer";

// Utilities
export {
extractSpanContextFromTraceParentHeader,
getTraceParentHeader
} from "./utils/traceParentHeader";

// OpenCensus Interfaces
export {
Tracer as OpenCensusTracer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Status } from "../../interfaces/status";
import { OpenCensusTraceStateWrapper } from "./openCensusTraceStateWrapper";
import { SpanOptions } from "../../interfaces/SpanOptions";
import { OpenCensusTracerWrapper } from "./openCensusTracerWrapper";
import { Attributes as OpenCensusAttributes, Span as OpenCensusSpan, LinkType } from "@opencensus/web-types";
import { Attributes as OpenCensusAttributes, Span as OpenCensusSpan } from "@opencensus/web-types";

function isWrappedSpan(span?: Span | SpanContext): span is OpenCensusSpanWrapper {
return !!span && (span as OpenCensusSpanWrapper).getWrappedSpan !== undefined;
Expand Down Expand Up @@ -101,7 +101,12 @@ export class OpenCensusSpanWrapper implements Span {
addLink(spanContext: SpanContext, attributes?: Attributes): this {
// Since there is no way to specify the link relationship,
// it is set as Unspecified.
this._span.addLink(spanContext.traceId, spanContext.spanId, 0 /* LinkType.UNSPECIFIED */, attributes as OpenCensusAttributes);
this._span.addLink(
spanContext.traceId,
spanContext.spanId,
0 /* LinkType.UNSPECIFIED */,
attributes as OpenCensusAttributes
);
return this;
}

Expand Down
28 changes: 28 additions & 0 deletions sdk/core/core-tracing/lib/utils/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import debug from "debug";

/**
* @ignore
* Log statements for errors that the application is unlikely to recover from.
*/
export const error = debug("azure:core-tracing:error");

/**
* @ignore
* Log statements for warnings when a function fails to perform its intended task.
*/
export const warning = debug("azure:core-tracing:warning");

/**
* @ignore
* Log statements for info when a function operates normally.
*/
export const info = debug("azure:core-tracing:info");

/**
* @ignore
* Log statements for verbose for troubleshooting scenarios.
*/
export const verbose = debug("azure:core-tracing:verbose");
68 changes: 68 additions & 0 deletions sdk/core/core-tracing/lib/utils/traceParentHeader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import { SpanContext } from "../interfaces/span_context";
import { TraceFlags } from "../interfaces/trace_flags";
import * as log from "./log";

const VERSION = "00";

/**
* Generates a `SpanContext` given a `traceparent` header value.
* @param traceParent Serialized span context data as a `traceparent` header value.
* @returns The `SpanContext` generated from the `traceparent` value.
*/
export function extractSpanContextFromTraceParentHeader(
traceParentHeader: string
): SpanContext | undefined {
const parts = traceParentHeader.split("-");

if (parts.length !== 4) {
log.warning(`Unable to extract span context from traceparent header "${traceParentHeader}".`);
return;
}

const [version, traceId, spanId, traceOptions] = parts;

if (version !== VERSION) {
log.warning(`Unexpected traceparent header version "${version}" found, expected "${VERSION}".`);
return;
}

const traceFlags = parseInt(traceOptions, 16);

const spanContext: SpanContext = {
spanId,
traceId,
traceFlags
};

return spanContext;
}

/**
* Generates a `traceparent` value given a span context.
* @param spanContext Contains context for a specific span.
* @returns The `spanContext` represented as a `traceparent` value.
*/
export function getTraceParentHeader(spanContext: SpanContext): string | undefined {
const missingFields: string[] = [];
if (!spanContext.traceId) {
missingFields.push("traceId");
}
if (!spanContext.spanId) {
missingFields.push("spanId");
}

if (missingFields.length) {
log.warning(`Missing required field(s) ${missingFields.join(", ")} from spanContext`);
return;
}

const flags = spanContext.traceFlags || TraceFlags.UNSAMPLED;
const hexFlags = flags.toString(16);
const traceFlags = hexFlags.length === 1 ? `0${hexFlags}` : hexFlags;

// https://www.w3.org/TR/trace-context/#traceparent-header-field-values
return `${VERSION}-${spanContext.traceId}-${spanContext.spanId}-${traceFlags}`;
}
6 changes: 4 additions & 2 deletions sdk/core/core-tracing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Provides low-level interfaces and helper methods for tracing in Azure SDK",
"sdk-type": "client",
"main": "dist/index.js",
"module": "dist-esm/index.js",
"module": "dist-esm/lib/index.js",
chradek marked this conversation as resolved.
Show resolved Hide resolved
"browser": {
"./dist/index.js": "./browser/index.js"
},
Expand Down Expand Up @@ -37,7 +37,7 @@
"files": [
"browser/*.js*",
"dist/",
"dist-esm/",
"dist-esm/lib/",
"src/",
"types/core-tracing.d.ts",
"ThirdPartyNotices.txt"
Expand All @@ -59,11 +59,13 @@
"sideEffects": false,
"dependencies": {
"@opencensus/web-types": "0.0.7",
"debug": "^4.1.1",
"tslib": "^1.9.3"
},
"devDependencies": {
"@azure/eslint-plugin-azure-sdk": "^2.0.1",
"@microsoft/api-extractor": "^7.1.5",
"@types/debug": "^4.1.4",
"@types/mocha": "^5.2.5",
"@types/node": "^8.0.0",
"@typescript-eslint/eslint-plugin": "^2.0.0",
Expand Down
6 changes: 6 additions & 0 deletions sdk/core/core-tracing/review/core-tracing.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ export interface Event {
name: string;
}

// @public
export function extractSpanContextFromTraceParentHeader(traceParentHeader: string): SpanContext | undefined;

// @public
export function getTraceParentHeader(spanContext: SpanContext): string | undefined;

// @public
export function getTracer(): Tracer;

Expand Down
2 changes: 1 addition & 1 deletion sdk/core/core-tracing/rollup.base.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import viz from "rollup-plugin-visualizer";

const pkg = require("./package.json");
const depNames = Object.keys(pkg.dependencies);
const input = "dist-esm/index.js";
const input = "dist-esm/lib/index.js";
const production = process.env.NODE_ENV === "production";

export function nodeConfig(test = false) {
Expand Down
128 changes: 128 additions & 0 deletions sdk/core/core-tracing/test/traceParentHeader.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import * as assert from "assert";
import {
extractSpanContextFromTraceParentHeader,
TraceFlags,
SpanContext,
getTraceParentHeader
} from "../lib";

describe("traceParentHeader", () => {
describe("#extractSpanContextFromTraceParentHeader", () => {
it("should extract a SpanContext from a properly formatted traceparent", () => {
const traceId = "11111111111111111111111111111111";
const spanId = "2222222222222222";
const flags = "00";
const traceParentHeader = `00-${traceId}-${spanId}-${flags}`;

const spanContext = extractSpanContextFromTraceParentHeader(traceParentHeader);
if (!spanContext) {
assert.fail("Extracted spanContext should be defined.");
return;
}
assert.equal(spanContext.traceId, traceId, "Extracted traceId does not match expectation.");
assert.equal(spanContext.spanId, spanId, "Extracted spanId does not match expectation.");
assert.equal(
spanContext.traceFlags,
TraceFlags.UNSAMPLED,
"Extracted traceFlags do not match expectations."
);
});

describe("should return undefined", () => {
it("when traceparent contains an unknown version", () => {
const traceId = "11111111111111111111111111111111";
const spanId = "2222222222222222";
const flags = "00";
const traceParentHeader = `99-${traceId}-${spanId}-${flags}`;

const spanContext = extractSpanContextFromTraceParentHeader(traceParentHeader);

assert.strictEqual(
spanContext,
undefined,
"Invalid traceparent version should return undefined spanContext."
);
});

it("when traceparent is malformed", () => {
const traceParentHeader = `123abc`;

const spanContext = extractSpanContextFromTraceParentHeader(traceParentHeader);

assert.strictEqual(
spanContext,
undefined,
"Malformed traceparent should return undefined spanContext."
);
});
});
});

describe("#getTraceParentHeader", () => {
it("should return a traceparent header from a SpanContext", () => {
const spanContext: SpanContext = {
spanId: "2222222222222222",
traceId: "11111111111111111111111111111111",
traceFlags: TraceFlags.SAMPLED
};

const traceParentHeader = getTraceParentHeader(spanContext);

assert.strictEqual(
traceParentHeader,
`00-${spanContext.traceId}-${spanContext.spanId}-01`,
"TraceParentHeader does not match expectation."
);
});

it("should set the traceFlag to UNSAMPLED if not provided in SpanContext", () => {
const spanContext: SpanContext = {
spanId: "2222222222222222",
traceId: "11111111111111111111111111111111"
};

const traceParentHeader = getTraceParentHeader(spanContext);

assert.strictEqual(
traceParentHeader,
`00-${spanContext.traceId}-${spanContext.spanId}-00`,
"TraceParentHeader does not match expectation."
);
});

describe("should return undefined", () => {
it("when traceId is not defined", () => {
const spanContext: any = {
spanId: "2222222222222222",
traceFlags: TraceFlags.SAMPLED
};

const traceParentHeader = getTraceParentHeader(spanContext);

assert.strictEqual(
traceParentHeader,
undefined,
"Missing traceId should return undefined spanContext."
);
});

it("when spanId is not defined", () => {
const spanContext: any = {
traceId: "11111111111111111111111111111111",
traceFlags: TraceFlags.SAMPLED
};

const traceParentHeader = getTraceParentHeader(spanContext);

assert.strictEqual(
traceParentHeader,
undefined,
"Missing spanId should return undefined spanContext."
);
});
});
});
});
1 change: 1 addition & 0 deletions sdk/eventhub/event-hubs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"@azure/abort-controller": "1.0.0-preview.2",
"@azure/core-amqp": "1.0.0-preview.4",
"@azure/core-asynciterator-polyfill": "1.0.0-preview.1",
"@azure/core-tracing": "1.0.0-preview.3",
"async-lock": "^1.1.3",
"buffer": "^5.2.1",
"debug": "^4.1.1",
Expand Down
Loading