Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Azure SDK Patching Subscriber (#562)
- Loading branch information
Mark Wolff
committed
Jun 18, 2020
1 parent
18691d3
commit 162d731
Showing
28 changed files
with
461 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { CorrelationContextManager, CorrelationContext } from "./CorrelationContextManager" | ||
import { ISpanContext } from "diagnostic-channel"; | ||
import { EventEmitter } from "events"; | ||
|
||
/** | ||
* Type of span. Can be used to specify additional relationships between spans | ||
* in addition to a parent/child relationship. | ||
*/ | ||
export enum SpanKind { | ||
/** Default value. Indicates that the span is used internally. */ | ||
INTERNAL = 0, | ||
|
||
/** | ||
* Indicates that the span covers server-side handling of an RPC or other | ||
* remote request. | ||
*/ | ||
SERVER = 1, | ||
|
||
/** | ||
* Indicates that the span covers the client-side wrapper around an RPC or | ||
* other remote request. | ||
*/ | ||
CLIENT = 2, | ||
|
||
/** | ||
* Indicates that the span describes producer sending a message to a | ||
* broker. Unlike client and server, there is no direct critical path latency | ||
* relationship between producer and consumer spans. | ||
*/ | ||
PRODUCER = 3, | ||
|
||
/** | ||
* Indicates that the span describes consumer receiving a message from a | ||
* broker. Unlike client and server, there is no direct critical path latency | ||
* relationship between producer and consumer spans. | ||
*/ | ||
CONSUMER = 4, | ||
} | ||
|
||
export interface Link { | ||
/** The {@link SpanContext} of a linked span. */ | ||
spanContext: SpanContext; | ||
/** A set of {@link Attributes} on the link. */ | ||
attributes?: Record<string, string>; | ||
} | ||
|
||
export interface SpanContext { | ||
traceId: string; | ||
spanId: string; | ||
traceFlags?: { toString: () => string }; | ||
tracestate?: string; | ||
} | ||
|
||
export interface Span { | ||
_duration: [number, number]; // hrTime | ||
name: string; | ||
parentSpanId?: string; | ||
status: { code: number, message?: string }, | ||
attributes: Record<string, string>, | ||
kind: SpanKind; | ||
links: Link[]; | ||
context: () => SpanContext; | ||
} | ||
|
||
export class OpenTelemetryScopeManagerWrapper { | ||
public active() { | ||
const context = CorrelationContextManager.getCurrentContext() as any; | ||
return { ...context, getValue: () => context, setValue: () => {} }; | ||
} | ||
|
||
public with(span: Span, fn: () => any) { | ||
const parentSpanId = span.parentSpanId; | ||
const name = span.name; | ||
const correlationContext = OpenTelemetryScopeManagerWrapper._spanToContext(span, parentSpanId, name); | ||
return CorrelationContextManager.runWithContext(correlationContext, fn)(); | ||
} | ||
|
||
public bind<T>(target: T): T { | ||
if (typeof target === "function") { | ||
return CorrelationContextManager.wrapCallback(target); | ||
} else if (target instanceof EventEmitter) { | ||
CorrelationContextManager.wrapEmitter(target); | ||
} | ||
return target; | ||
} | ||
|
||
public enable(): this { | ||
CorrelationContextManager.enable(); | ||
return this; | ||
} | ||
|
||
public disable(): this { | ||
CorrelationContextManager.disable(); | ||
return this; | ||
} | ||
|
||
private static _spanToContext(span: Span, parentSpanId?: string, name?: string): CorrelationContext { | ||
const _parentId = parentSpanId ? `|${span.context().traceId}.${parentSpanId}.` : span.context().traceId; | ||
const context: ISpanContext = { | ||
...span.context(), | ||
traceFlags: span.context().traceFlags.toString() | ||
}; | ||
const correlationContext = CorrelationContextManager.spanToContextObject(context, _parentId, name) | ||
return correlationContext; | ||
} | ||
} | ||
|
||
export const AsyncScopeManager = new OpenTelemetryScopeManagerWrapper(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for details. | ||
import { Span, SpanKind } from "../AsyncHooksScopeManager"; | ||
import * as Contracts from "../../Declarations/Contracts"; | ||
import * as Constants from "../../Declarations/Constants"; | ||
|
||
function filterSpanAttributes(attributes: Record<string, string>) { | ||
const newAttributes = { ...attributes }; | ||
Object.keys(Constants.SpanAttribute).forEach(key => { | ||
delete newAttributes[key]; | ||
}); | ||
return newAttributes | ||
} | ||
|
||
export function spanToTelemetryContract(span: Span): (Contracts.DependencyTelemetry & Contracts.RequestTelemetry) & Contracts.Identified { | ||
const id = `|${span.context().traceId}.${span.context().spanId}.`; | ||
const duration = Math.round(span._duration[0] * 1e3 + span._duration[1] / 1e6); | ||
const isHttp: boolean = ((span.attributes.component || "").toUpperCase() === Constants.DependencyTypeName.Http) || (!!span.attributes[Constants.SpanAttribute.HttpUrl]); | ||
const isGrpc: boolean = (span.attributes.component || "").toLowerCase() === Constants.DependencyTypeName.Grpc; | ||
if (isHttp) { | ||
// Read http span attributes | ||
const method = span.attributes[Constants.SpanAttribute.HttpMethod] || "GET"; | ||
const url = new URL(span.attributes[Constants.SpanAttribute.HttpUrl]); | ||
const host = span.attributes[Constants.SpanAttribute.HttpHost] || url.host; | ||
const port = span.attributes[Constants.SpanAttribute.HttpPort] || url.port || null; | ||
const pathname = url.pathname || "/"; | ||
|
||
// Translate to AI Dependency format | ||
const name = `${method} ${pathname}`; | ||
const dependencyTypeName = Constants.DependencyTypeName.Http; | ||
const target = port ? `${host}:${port}` : host; | ||
const data = url.toString(); | ||
const resultCode = span.attributes[Constants.SpanAttribute.HttpStatusCode] || span.status.code || 0; | ||
const success = resultCode < 400; // Status.OK | ||
return { | ||
id, name, dependencyTypeName, | ||
target, data, | ||
success, duration, | ||
url: data, | ||
resultCode: String(resultCode), | ||
properties: filterSpanAttributes(span.attributes) | ||
}; | ||
} else if (isGrpc) { | ||
const method = span.attributes[Constants.SpanAttribute.GrpcMethod] || "rpc"; | ||
const service = span.attributes[Constants.SpanAttribute.GrpcService]; | ||
const name = service ? `${method} ${service}` : span.name; | ||
return { | ||
id, duration, name, | ||
target: service, | ||
data: service || name, | ||
url: service || name, | ||
dependencyTypeName: Constants.DependencyTypeName.Grpc, | ||
resultCode: String(span.status.code || 0), | ||
success: span.status.code === 0, | ||
properties: filterSpanAttributes(span.attributes), | ||
} | ||
} else { | ||
const name = span.name; | ||
const links = span.links && span.links.map(link => { | ||
return { | ||
operation_Id: link.spanContext.traceId, | ||
id: link.spanContext.spanId | ||
}; | ||
}); | ||
return { | ||
id, duration, name, | ||
target: span.attributes["peer.address"], | ||
data: span.attributes["peer.address"] || name, | ||
url: span.attributes["peer.address"] || name, | ||
dependencyTypeName: span.kind === SpanKind.INTERNAL ? Constants.DependencyTypeName.InProc : (span.attributes.component || span.name), | ||
resultCode: String(span.status.code || 0), | ||
success: span.status.code === 0, | ||
properties: { | ||
...filterSpanAttributes(span.attributes), | ||
"_MS.links": links || undefined | ||
}, | ||
}; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
AutoCollection/diagnostic-channel/azure-coretracing.sub.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for details. | ||
import TelemetryClient = require("../../Library/TelemetryClient"); | ||
import { channel, IStandardEvent } from "diagnostic-channel"; | ||
|
||
import Traceparent = require("../../Library/Traceparent"); | ||
import * as SpanParser from "./SpanParser"; | ||
import { Span, AsyncScopeManager, SpanKind } from "../AsyncHooksScopeManager"; | ||
|
||
let clients: TelemetryClient[] = []; | ||
|
||
export const subscriber = (event: IStandardEvent<Span>) => { | ||
const span = event.data; | ||
const telemetry = SpanParser.spanToTelemetryContract(span); | ||
const spanContext = span.context(); | ||
const traceparent = new Traceparent(); | ||
traceparent.traceId = spanContext.traceId; | ||
traceparent.spanId = spanContext.spanId; | ||
traceparent.traceFlag = spanContext.traceFlags.toString(); | ||
traceparent.parentId = span.parentSpanId ? `|${spanContext.traceId}.${span.parentSpanId}.` : null; | ||
|
||
AsyncScopeManager.with(span, () => { | ||
clients.forEach((client) => { | ||
if (span.kind === SpanKind.SERVER) { | ||
// Server or Consumer | ||
client.trackRequest(telemetry); | ||
} else if (span.kind === SpanKind.CLIENT || span.kind === SpanKind.INTERNAL) { | ||
// Client or Producer or Internal | ||
client.trackDependency(telemetry); | ||
} | ||
// else - ignore producer/consumer spans for now until it is clear how this sdk should interpret them | ||
}); | ||
}); | ||
}; | ||
|
||
export function enable(enabled: boolean, client: TelemetryClient) { | ||
if (enabled) { | ||
if (clients.length === 0) { | ||
channel.subscribe<any>("azure-coretracing", subscriber); | ||
}; | ||
clients.push(client); | ||
} else { | ||
clients = clients.filter((c) => c != client); | ||
if (clients.length === 0) { | ||
channel.unsubscribe("azure-coretracing", subscriber); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.