Skip to content

Commit

Permalink
[telemetry] Centralize (as much as is practical) the creation of span…
Browse files Browse the repository at this point in the history
…s to ease upgrades (#13887)

As part of prepping for the next release of OpenTelemetry we found some code patterns that were going to become a large maintenance burden for us, primarily around the parenting of spans. To make this easier I've removed as many duplicate implementation of createSpan and tried to centralize everything into core-tracing instead. 

This won't completely remove changes needed for a newer version of OpenTelemetry but it'll eliminate one of the bigger bottlenecks.
  • Loading branch information
richardpark-msft committed Feb 26, 2021
1 parent d0d1fca commit 82c42a2
Show file tree
Hide file tree
Showing 130 changed files with 1,273 additions and 2,865 deletions.
2 changes: 1 addition & 1 deletion sdk/anomalydetector/ai-anomaly-detector/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"@azure/logger": "^1.0.0",
"@opentelemetry/api": "^0.10.2",
"tslib": "^2.0.0",
"@azure/core-tracing": "1.0.0-preview.9"
"@azure/core-tracing": "1.0.0-beta.1"
},
"devDependencies": {
"@azure/dev-tool": "^1.0.0",
Expand Down
55 changes: 5 additions & 50 deletions sdk/anomalydetector/ai-anomaly-detector/src/tracing.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,13 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { getTracer, OperationTracingOptions } from "@azure/core-tracing";
import { Span, SpanOptions, SpanKind } from "@opentelemetry/api";
import { OperationOptions } from "@azure/core-http";
import { createSpanFunction } from "@azure/core-tracing";

/**
* Creates a span using the global tracer.
* @internal
* @param name - The name of the operation being performed.
* @param tracingOptions - The options for the underlying http request.
*/
export function createSpan<T extends OperationOptions>(
operationName: string,
operationOptions: T
): { span: Span; updatedOptions: T } {
const tracer = getTracer();
const tracingOptions = operationOptions.tracingOptions || {};
const spanOptions: SpanOptions = {
...tracingOptions.spanOptions,
kind: SpanKind.INTERNAL
};

const span = tracer.startSpan(
`Azure.CognitiveServices.AnomalyDetector.${operationName}`,
spanOptions
);

span.setAttribute("az.namespace", "Microsoft.CognitiveServices");

let newSpanOptions = tracingOptions.spanOptions || {};
if (span.isRecording()) {
newSpanOptions = {
...tracingOptions.spanOptions,
parent: span.context(),
attributes: {
...spanOptions.attributes,
"az.namespace": "Microsoft.CognitiveServices"
}
};
}

const newTracingOptions: OperationTracingOptions = {
...tracingOptions,
spanOptions: newSpanOptions
};

const newOperationOptions: T = {
...operationOptions,
tracingOptions: newTracingOptions
};

return {
span,
updatedOptions: newOperationOptions
};
}
export const createSpan = createSpanFunction({
packagePrefix: "Azure.CognitiveServices.AnomalyDetector",
namespace: "Microsoft.CognitiveServices"
});
2 changes: 1 addition & 1 deletion sdk/appconfiguration/app-configuration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
"@azure/core-asynciterator-polyfill": "^1.0.0",
"@azure/core-http": "^1.2.0",
"@azure/core-paging": "^1.1.1",
"@azure/core-tracing": "1.0.0-preview.9",
"@azure/core-tracing": "1.0.0-beta.1",
"@opentelemetry/api": "^0.10.2",
"tslib": "^2.0.0"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"@azure/core-lro": "^1.0.2",
"@azure/core-paging": "^1.1.1",
"@azure/logger": "^1.0.0",
"@azure/core-tracing": "1.0.0-preview.9",
"@azure/core-tracing": "1.0.0-beta.1",
"@opentelemetry/api": "^0.10.2",
"events": "^3.0.0",
"tslib": "^2.0.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,57 +1,13 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { OperationOptions } from "@azure/core-http";
import { getTracer } from "@azure/core-tracing";
import { Span, SpanOptions, SpanKind } from "@opentelemetry/api";

type OperationTracingOptions = OperationOptions["tracingOptions"];
import { createSpanFunction } from "@azure/core-tracing";

/**
* Creates a span using the global tracer.
* @internal
* @param name - The name of the operation being performed.
* @param tracingOptions - The options for the underlying http request.
*/
export function createSpan<T extends OperationOptions>(
operationName: string,
operationOptions: T
): { span: Span; updatedOptions: T } {
const tracer = getTracer();
const tracingOptions = operationOptions.tracingOptions || {};
const spanOptions: SpanOptions = {
...tracingOptions.spanOptions,
kind: SpanKind.INTERNAL
};

const span = tracer.startSpan(`Azure.Communication.${operationName}`, spanOptions);

span.setAttribute("az.namespace", "Microsoft.Communication");

let newSpanOptions = tracingOptions.spanOptions || {};
if (span.isRecording()) {
newSpanOptions = {
...tracingOptions.spanOptions,
parent: span.context(),
attributes: {
...spanOptions.attributes,
"az.namespace": "Microsoft.Communication"
}
};
}

const newTracingOptions: OperationTracingOptions = {
...tracingOptions,
spanOptions: newSpanOptions
};

const newOperationOptions: T = {
...operationOptions,
tracingOptions: newTracingOptions
};

return {
span,
updatedOptions: newOperationOptions
};
}
export const createSpan = createSpanFunction({
packagePrefix: "Azure.Communication",
namespace: "Microsoft.Communication"
});
2 changes: 1 addition & 1 deletion sdk/communication/communication-chat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"@azure/communication-signaling": "1.0.0-beta.2",
"@azure/core-auth": "^1.2.0",
"@azure/core-http": "^1.2.0",
"@azure/core-tracing": "1.0.0-preview.9",
"@azure/core-tracing": "1.0.0-beta.1",
"@azure/logger": "^1.0.0",
"@opentelemetry/api": "^0.10.2",
"events": "^3.0.0",
Expand Down
54 changes: 5 additions & 49 deletions sdk/communication/communication-chat/src/tracing.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,13 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { getTracer } from "@azure/core-tracing";
import { Span, SpanOptions, SpanKind } from "@opentelemetry/api";
import { OperationOptions } from "@azure/core-http";

type OperationTracingOptions = OperationOptions["tracingOptions"];
import { createSpanFunction } from "@azure/core-tracing";

/**
* Creates a span using the global tracer.
* @internal
* @param name - The name of the operation being performed.
* @param tracingOptions - The options for the underlying http request.
*/
export function createSpan<T extends OperationOptions>(
operationName: string,
operationOptions: T
): { span: Span; updatedOptions: T } {
const tracer = getTracer();
const tracingOptions = operationOptions.tracingOptions || {};
const spanOptions: SpanOptions = {
...tracingOptions.spanOptions,
kind: SpanKind.INTERNAL
};

const span = tracer.startSpan(`Azure.Communication.${operationName}`, spanOptions);

span.setAttribute("az.namespace", "Microsoft.Communication");

let newSpanOptions = tracingOptions.spanOptions || {};
if (span.isRecording()) {
newSpanOptions = {
...tracingOptions.spanOptions,
parent: span.context(),
attributes: {
...spanOptions.attributes,
"az.namespace": "Microsoft.Communication"
}
};
}

const newTracingOptions: OperationTracingOptions = {
...tracingOptions,
spanOptions: newSpanOptions
};

const newOperationOptions: T = {
...operationOptions,
tracingOptions: newTracingOptions
};

return {
span,
updatedOptions: newOperationOptions
};
}
export const createSpan = createSpanFunction({
packagePrefix: "Azure.Communication",
namespace: "Microsoft.Communication"
});
2 changes: 1 addition & 1 deletion sdk/communication/communication-identity/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"@azure/core-lro": "^1.0.2",
"@azure/core-paging": "^1.1.1",
"@azure/logger": "^1.0.0",
"@azure/core-tracing": "1.0.0-preview.9",
"@azure/core-tracing": "1.0.0-beta.1",
"@opentelemetry/api": "^0.10.2",
"events": "^3.0.0",
"tslib": "^2.0.0"
Expand Down
54 changes: 5 additions & 49 deletions sdk/communication/communication-identity/src/common/tracing.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,13 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { OperationOptions } from "@azure/core-http";
import { getTracer } from "@azure/core-tracing";
import { Span, SpanOptions, SpanKind } from "@opentelemetry/api";

type OperationTracingOptions = OperationOptions["tracingOptions"];
import { createSpanFunction } from "@azure/core-tracing";

/**
* Creates a span using the global tracer.
* @internal
* @param name - The name of the operation being performed.
* @param tracingOptions - The options for the underlying http request.
*/
export function createSpan<T extends OperationOptions>(
operationName: string,
operationOptions: T
): { span: Span; updatedOptions: T } {
const tracer = getTracer();
const tracingOptions = operationOptions.tracingOptions || {};
const spanOptions: SpanOptions = {
...tracingOptions.spanOptions,
kind: SpanKind.INTERNAL
};

const span = tracer.startSpan(`Azure.Communication.${operationName}`, spanOptions);

span.setAttribute("az.namespace", "Microsoft.Communication");

let newSpanOptions = tracingOptions.spanOptions || {};
if (span.isRecording()) {
newSpanOptions = {
...tracingOptions.spanOptions,
parent: span.context(),
attributes: {
...spanOptions.attributes,
"az.namespace": "Microsoft.Communication"
}
};
}

const newTracingOptions: OperationTracingOptions = {
...tracingOptions,
spanOptions: newSpanOptions
};

const newOperationOptions: T = {
...operationOptions,
tracingOptions: newTracingOptions
};

return {
span,
updatedOptions: newOperationOptions
};
}
export const createSpan = createSpanFunction({
packagePrefix: "Azure.Communication",
namespace: "Microsoft.Communication"
});
2 changes: 1 addition & 1 deletion sdk/communication/communication-sms/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"@azure/communication-common": "1.0.0-beta.6",
"@azure/core-auth": "^1.2.0",
"@azure/core-http": "^1.2.0",
"@azure/core-tracing": "1.0.0-preview.9",
"@azure/core-tracing": "1.0.0-beta.1",
"@azure/logger": "^1.0.0",
"@opentelemetry/api": "^0.10.2",
"events": "^3.0.0",
Expand Down
54 changes: 5 additions & 49 deletions sdk/communication/communication-sms/src/tracing.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,13 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { OperationOptions } from "@azure/core-http";
import { getTracer } from "@azure/core-tracing";
import { Span, SpanOptions, SpanKind } from "@opentelemetry/api";

type OperationTracingOptions = OperationOptions["tracingOptions"];
import { createSpanFunction } from "@azure/core-tracing";

/**
* Creates a span using the global tracer.
* @internal
* @param name - The name of the operation being performed.
* @param tracingOptions - The options for the underlying http request.
*/
export function createSpan<T extends OperationOptions>(
operationName: string,
operationOptions: T
): { span: Span; updatedOptions: T } {
const tracer = getTracer();
const tracingOptions = operationOptions.tracingOptions || {};
const spanOptions: SpanOptions = {
...tracingOptions.spanOptions,
kind: SpanKind.INTERNAL
};

const span = tracer.startSpan(`Azure.Communication.${operationName}`, spanOptions);

span.setAttribute("az.namespace", "Microsoft.Communication");

let newSpanOptions = tracingOptions.spanOptions || {};
if (span.isRecording()) {
newSpanOptions = {
...tracingOptions.spanOptions,
parent: span.context(),
attributes: {
...spanOptions.attributes,
"az.namespace": "Microsoft.Communication"
}
};
}

const newTracingOptions: OperationTracingOptions = {
...tracingOptions,
spanOptions: newSpanOptions
};

const newOperationOptions: T = {
...operationOptions,
tracingOptions: newTracingOptions
};

return {
span,
updatedOptions: newOperationOptions
};
}
export const createSpan = createSpanFunction({
packagePrefix: "Azure.Communication",
namespace: "Microsoft.Communication"
});
3 changes: 3 additions & 0 deletions sdk/core/core-client/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## 1.0.0-beta.2 (Unreleased)

- [Breaking] Removed `createSpanFunction` and `SpanConfig`. These have been moved into
`@azure/core-tracing`.

## 1.0.0-beta.1 (2021-02-04)

- First release of package, see README.md for details.
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/core-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"@azure/core-asynciterator-polyfill": "^1.0.0",
"@azure/core-auth": "^1.2.0",
"@azure/core-https": "1.0.0-beta.2",
"@azure/core-tracing": "1.0.0-preview.9",
"@azure/core-tracing": "1.0.0-beta.1",
"@opentelemetry/api": "^0.10.2",
"tslib": "^2.0.0"
},
Expand Down
Loading

0 comments on commit 82c42a2

Please sign in to comment.