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
feat: add Azure SDK Patching Subscriber #562
Merged
Merged
Changes from 13 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
71dab54
feat: add azure SDK subscriber
markwolff 769ce1b
Update winston version in tests
markwolff 720d2a6
Fix double requiring SDK in Config
markwolff ef22ade
fix parallel functional tests
markwolff 320f4fe
do not run azure sdk tests on old node vers
markwolff 7db8a57
Remove from travis non-lts odd versions
markwolff 4bbda8c
fix module exports syntax
markwolff ffd123b
skip async hooks tests
markwolff 92bef90
fix typo
markwolff 1d892ef
fix incorrect func test path
markwolff f7643fc
revert old StressTestTime
markwolff c6fb329
add SpanKind enum, handle producer/consumer kind
markwolff b74ded5
refactor: ignore producer/consumer spans
3bb57d1
refactor: add SpanParser
markwolff 233a9b4
fix: failing functional tests
markwolff c2b9900
test: add event hubs tests
markwolff 52834e3
Merge branch 'develop' into markwolff/add-azuresdk-patching
markwolff d54b600
update diag channel dep
markwolff 4062b9a
add: filterSpanAttributes
markwolff f01a03e
update: package.json
markwolff a39ea0d
Merge branch 'develop' into markwolff/add-azuresdk-patching
60cce8e
update for beta opentelemetry
markwolff b1a4f57
Merge branch 'develop' of github.com:microsoft/ApplicationInsights-no…
markwolff 22a0732
fix failing tests
markwolff c1d913f
remove unsupported eventhubs tests
markwolff 77e4d50
bump dependencies
markwolff 40b7e3c
add missing test dependency
markwolff 0276be4
enable w3c mode by default
markwolff File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
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 Span { | ||
_duration: [number, number]; // hrTime | ||
name: string; | ||
parentSpanId?: string; | ||
status: { code: number, message?: string }, | ||
attributes: Record<string, string>, | ||
kind: SpanKind; | ||
context: () => { | ||
traceId: string; | ||
spanId: string; | ||
traceFlags?: { toString: () => string }; | ||
tracestate?: string; | ||
} | ||
} | ||
|
||
export class OpenTelemetryScopeManagerWrapper { | ||
public active() { | ||
return CorrelationContextManager.getCurrentContext(); | ||
} | ||
|
||
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
73 changes: 73 additions & 0 deletions
73
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,73 @@ | ||
// 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 Contracts from "../../Declarations/Contracts"; | ||
import { Span, AsyncScopeManager, SpanKind } from "../AsyncHooksScopeManager"; | ||
|
||
let clients: TelemetryClient[] = []; | ||
|
||
export const subscriber = (event: IStandardEvent<Span>) => { | ||
const span = event.data; | ||
const spanContext = span.context(); | ||
const duration = Math.round(span._duration[0] * 1e3 + span._duration[1] / 1e6); | ||
const id = `|${span.context().traceId}.${span.context().spanId}.`; | ||
|
||
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; | ||
|
||
const properties = { | ||
...span.attributes, | ||
}; | ||
if (span.status.message) { | ||
properties["status.message"] = span.status.message; | ||
} | ||
AsyncScopeManager.with(span, () => { | ||
clients.forEach((client) => { | ||
if (span.kind === SpanKind.SERVER) { | ||
// Server or Consumer | ||
client.trackRequest({ | ||
id: id, | ||
name: span.name, | ||
duration: duration, | ||
resultCode: span.status.code, | ||
success: span.status.code === 0, | ||
url: span.attributes.component, | ||
properties: properties, | ||
} as Contracts.RequestTelemetry & Contracts.Identified); | ||
} else if (span.kind === SpanKind.CLIENT || span.kind === SpanKind.INTERNAL) { | ||
// Client or Producer or Internal | ||
client.trackDependency({ | ||
id: id, | ||
name: span.name, | ||
duration: duration, | ||
data: span.attributes.component || span.name, | ||
dependencyTypeName: span.attributes.component || span.name, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The span attributes aren't guaranteed to provide meaningful information in a standardized way right now, so for now just populate them with current "standards" and iterate back on this in the future |
||
resultCode: span.status.code, | ||
success: span.status.code === 0, | ||
properties: properties | ||
} as Contracts.DependencyTelemetry & Contracts.Identified); | ||
} | ||
// 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A clone of Span interface so that we don't need to take a Typescript 3 dependency