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

Add manual tracing to Identity #5283

Merged
merged 19 commits into from
Sep 30, 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
4 changes: 2 additions & 2 deletions sdk/core/core-http/lib/policies/tracingPolicy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import { TracerProxy, TraceFlags } from "@azure/core-tracing";
import { getTracer, TraceFlags } from "@azure/core-tracing";
import {
RequestPolicyFactory,
RequestPolicy,
Expand Down Expand Up @@ -30,7 +30,7 @@ export class TracingPolicy extends BaseRequestPolicy {
}

// create a new span
const tracer = TracerProxy.getTracer();
const tracer = getTracer();
const span = tracer.startSpan("core-http", request.spanOptions);

try {
Expand Down
5 changes: 5 additions & 0 deletions sdk/core/core-http/lib/webResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,10 @@ export class WebResource {
}
}

if (options.spanOptions) {
this.spanOptions = options.spanOptions;
}

this.abortSignal = options.abortSignal;
this.onDownloadProgress = options.onDownloadProgress;
this.onUploadProgress = options.onUploadProgress;
Expand Down Expand Up @@ -468,6 +472,7 @@ export interface RequestPrepareOptions {
abortSignal?: AbortSignalLike;
onUploadProgress?: (progress: TransferProgressEvent) => void;
onDownloadProgress?: (progress: TransferProgressEvent) => void;
spanOptions?: SpanOptions;
}

/**
Expand Down
14 changes: 7 additions & 7 deletions sdk/core/core-http/test/policies/tracingPolicyTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT License.

import { assert } from "chai";
import { RequestPolicy, WebResource, HttpOperationResponse, HttpHeaders, TracerProxy, RequestPolicyOptions, TraceFlags, NoOpTracer, SpanOptions, SpanContext, NoOpSpan } from "../../lib/coreHttp";
import { RequestPolicy, WebResource, HttpOperationResponse, HttpHeaders, setTracer, RequestPolicyOptions, TraceFlags, NoOpTracer, SpanOptions, SpanContext, NoOpSpan } from "../../lib/coreHttp";
import { tracingPolicy } from "../../lib/policies/tracingPolicy";

class MockSpan extends NoOpSpan {
Expand Down Expand Up @@ -83,7 +83,7 @@ describe("tracingPolicy", function () {

it("will not create a span if spanOptions are missing", async () => {
const mockTracer = new MockTracer();
TracerProxy.setTracer(mockTracer);
setTracer(mockTracer);
const request = new WebResource();
const policy = tracingPolicy().create(mockPolicy, new RequestPolicyOptions());
await policy.sendRequest(request);
Expand All @@ -95,7 +95,7 @@ describe("tracingPolicy", function () {
const mockTraceId = "11111111111111111111111111111111";
const mockSpanId = "2222222222222222";
const mockTracer = new MockTracer(mockTraceId, mockSpanId, TraceFlags.SAMPLED);
TracerProxy.setTracer(mockTracer);
setTracer(mockTracer);
const request = new WebResource();
request.spanOptions = {
parent: ROOT_SPAN
Expand All @@ -117,7 +117,7 @@ describe("tracingPolicy", function () {
const mockSpanId = "2222222222222222";
// leave out the TraceOptions
const mockTracer = new MockTracer(mockTraceId, mockSpanId);
TracerProxy.setTracer(mockTracer);
setTracer(mockTracer);
const request = new WebResource();
request.spanOptions = {
parent: ROOT_SPAN
Expand All @@ -139,7 +139,7 @@ describe("tracingPolicy", function () {
const mockSpanId = "2222222222222222";
const mockTraceState = "foo=bar";
const mockTracer = new MockTracer(mockTraceId, mockSpanId, TraceFlags.SAMPLED, mockTraceState);
TracerProxy.setTracer(mockTracer);
setTracer(mockTracer);
const request = new WebResource();
request.spanOptions = {
parent: ROOT_SPAN
Expand All @@ -161,7 +161,7 @@ describe("tracingPolicy", function () {
const mockSpanId = "2222222222222222";
const mockTraceState = "foo=bar";
const mockTracer = new MockTracer(mockTraceId, mockSpanId, TraceFlags.SAMPLED, mockTraceState);
TracerProxy.setTracer(mockTracer);
setTracer(mockTracer);
const request = new WebResource();
request.spanOptions = {
parent: ROOT_SPAN
Expand Down Expand Up @@ -191,7 +191,7 @@ describe("tracingPolicy", function () {
});

it("will not set headers if span is a NoOpSpan", async () => {
TracerProxy.setTracer(new NoOpTracer());
setTracer(new NoOpTracer());
const request = new WebResource();
request.spanOptions = {
parent: ROOT_SPAN
Expand Down
23 changes: 8 additions & 15 deletions sdk/core/core-tracing/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,15 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import { getTracer, setTracer, ITracerProxy } from "./tracerProxy";
/**
* A global registry for the active OpenTelemtry Tracer.
* Clients inside the Azure SDK will use this Tracer for all trace logging.
*/
const TracerProxy: ITracerProxy = {
getTracer,
setTracer,
};
export { TracerProxy, ITracerProxy };
export { getTracer, setTracer } from "./tracerProxy";

// Wrappers
export { NoOpSpan } from "./wrappers/noop/noOpSpan";
export { NoOpTracer } from "./wrappers/noop/noOpTracer";
export { OpenCensusSpanWrapper } from "./wrappers/opencensus/openCensusSpanWrapper";
export { OpenCensusTracerWrapper } from "./wrappers/opencensus/openCensusTracerWrapper";
// Tracers and wrappers
export { NoOpSpan } from "./tracers/noop/noOpSpan";
export { NoOpTracer } from "./tracers/noop/noOpTracer";
export { OpenCensusSpanWrapper } from "./tracers/opencensus/openCensusSpanWrapper";
export { OpenCensusTracerWrapper } from "./tracers/opencensus/openCensusTracerWrapper";
export { TestTracer, SpanGraph, SpanGraphNode } from "./tracers/test/testTracer";
export { TestSpan } from "./tracers/test/testSpan";

// Interfaces
export { Attributes } from "./interfaces/attributes";
Expand Down
21 changes: 6 additions & 15 deletions sdk/core/core-tracing/lib/tracerProxy.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,24 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import { NoOpTracer } from "./wrappers/noop/noOpTracer";
import { NoOpTracer } from "./tracers/noop/noOpTracer";
import { Tracer } from "./interfaces/tracer";


let _tracerPlugin: Tracer;

/**
* A global registry for the active OpenTelemtry Tracer.
* Clients inside the Azure SDK will use this Tracer for all trace logging.
*/
export interface ITracerProxy {
/**
* Sets the global tracer, enabling tracing.
* Sets the global tracer, enabling tracing for the AzureSDK.
* @param tracer An OpenTelemetry Tracer instance.
*/
setTracer(tracer: Tracer): void;
/**
* Retrieves the active tracer, or returns a
* no-op implementation if one is not set.
*/
getTracer(): Tracer;
}

export function setTracer(tracer: Tracer) {
_tracerPlugin = tracer;
}

/**
* Retrieves the active tracer, or returns a
* no-op implementation if one is not set.
*/
export function getTracer() {
if (!_tracerPlugin) {
_tracerPlugin = new NoOpTracer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ export class OpenCensusSpanWrapper implements Span {
* @param attributes attributes to be added that are associated with the link
*/
addLink(spanContext: SpanContext, attributes?: Attributes): this {
// Since there is no way to specify the link relationship
// It is set as Unspecified = 0
this._span.addLink(spanContext.traceId, spanContext.spanId, LinkType.UNSPECIFIED, attributes as OpenCensusAttributes);
// 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);
return this;
}

Expand Down
117 changes: 117 additions & 0 deletions sdk/core/core-tracing/lib/tracers/test/testSpan.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { NoOpSpan } from "../noop/noOpSpan";
import { SpanOptions } from "../../interfaces/SpanOptions";
import { Status, CanonicalCode } from "../../interfaces/status";
import { SpanContext } from "../../interfaces/span_context";
import { TestTracer } from "./testTracer";
import { SpanKind } from "../../interfaces/span_kind";
import { TimeInput } from "../../interfaces/Time";
import { Tracer } from "../../interfaces/tracer";

/**
* A mock span useful for testing.
*/
export class TestSpan extends NoOpSpan {

/**
* The Span's current name
*/
name: string;

/**
* The Span's current status
*/
status: Status;

/**
* The Span's kind
*/
kind: SpanKind;

/**
* True if end() has been called on the Span
*/
endCalled: boolean;

/**
* The start time of the Span
*/
readonly startTime: TimeInput;

/**
* The id of the parent Span, if any.
*/
readonly parentSpanId?: string;

private _context: SpanContext;
private readonly _tracer: Tracer;

/**
* Starts a new Span.
* @param parentTracer The tracer that created this Span
* @param name The name of the span.
* @param context The SpanContext this span belongs to
* @param kind The SpanKind of this Span
* @param parentSpanId The identifier of the parent Span
* @param startTime The startTime of the event (defaults to now)
*/
constructor(
parentTracer: TestTracer,
name: string,
context: SpanContext,
kind: SpanKind,
parentSpanId?: string,
startTime: TimeInput = Date.now()) {
super();
this._tracer = parentTracer;
this.name = name;
this.kind = kind;
this.startTime = startTime;
this.parentSpanId = parentSpanId;
this.status = {
code: CanonicalCode.OK
};
this.endCalled = false;
this._context = context;
}

/**
* Returns the Tracer that created this Span
*/
tracer(): Tracer {
return this._tracer;
}

/**
* Returns the SpanContext associated with this Span.
*/
context(): SpanContext {
return this._context;
}

/**
* Marks the end of Span execution.
* @param _endTime The time to use as the Span's end time. Defaults to
* the current time.
*/
end(_endTime?: number): void {
this.endCalled = true;
}

/**
* Sets a status on the span. Overrides the default of CanonicalCode.OK.
* @param status The status to set.
*/
setStatus(status: Status): this {
this.status = status;
return this;
}

/**
* Returns whether this span will be recorded
*/
isRecordingEvents(): boolean {
return true;
}
}
Loading