Skip to content

Update span parser #797

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

Merged
merged 5 commits into from
Jul 26, 2021
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
23 changes: 10 additions & 13 deletions AutoCollection/diagnostic-channel/SpanParser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
import { SpanAttributes, SpanKind } from "@opentelemetry/api";
import { SpanAttributes, SpanKind, SpanStatusCode } from "@opentelemetry/api";
import { Span } from "@opentelemetry/tracing";
import * as Contracts from "../../Declarations/Contracts";
import * as Constants from "../../Declarations/Constants";
Expand All @@ -17,22 +17,19 @@ export function spanToTelemetryContract(span: Span): (Contracts.DependencyTeleme
const id = `|${span.spanContext().traceId}.${span.spanContext().spanId}.`;
const duration = Math.round(span["_duration"][0] * 1e3 + span["_duration"][1] / 1e6);
let peerAddress = span.attributes["peer.address"] ? span.attributes["peer.address"].toString() : "";
let component = span.attributes["component"] ? span.attributes["component"].toString() : "";

const isHttp: boolean = ((component).toUpperCase() === Constants.DependencyTypeName.Http) || (!!span.attributes[Constants.SpanAttribute.HttpUrl]);
const isGrpc: boolean = (component).toLowerCase() === Constants.DependencyTypeName.Grpc;
const isHttp: boolean = (!!span.attributes[Constants.SpanAttribute.HttpStatusCode]) || (!!span.attributes[Constants.SpanAttribute.HttpUrl]);
const isGrpc: boolean = (!!span.attributes[Constants.SpanAttribute.GrpcStatusCode]);
Comment on lines +21 to +22
Copy link
Contributor Author

@xiao-lix xiao-lix Jul 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • to check if it's http, check span[HTTP_STATUS_CODE] reference
  • to check if it's grpc, check span[RPC_GRPC_STATUS_CODE] reference

if (isHttp) {
// Read http span attributes
const method = span.attributes[Constants.SpanAttribute.HttpMethod] || "GET";
const url = new URL(span.attributes[Constants.SpanAttribute.HttpUrl].toString());
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}`.toString() : host.toString();
const target = span.attributes[Constants.SpanAttribute.HttpUrl].toString() ? url.hostname : undefined;
const data = url.toString();
const resultCode = span.attributes[Constants.SpanAttribute.HttpStatusCode] || span.status.code || 0;
const success = resultCode < 400; // Status.OK
Expand All @@ -50,12 +47,12 @@ export function spanToTelemetryContract(span: Span): (Contracts.DependencyTeleme
const name = service ? `${method} ${service}` : span.name;
return {
id, duration, name,
target: service.toString(),
target: method.toString(),
data: service.toString() || name,
url: service.toString() || name,
dependencyTypeName: Constants.DependencyTypeName.Grpc,
resultCode: String(span.status.code || 0),
success: span.status.code === 0,
resultCode: String(span.attributes[Constants.SpanAttribute.GrpcStatusCode] || span.status.code || 0),
success: span.status.code === SpanStatusCode.OK,
properties: filterSpanAttributes(span.attributes),
}
} else {
Expand All @@ -68,12 +65,12 @@ export function spanToTelemetryContract(span: Span): (Contracts.DependencyTeleme
});
return {
id, duration, name,
target: peerAddress,
target: span.attributes[Constants.SpanAttribute.HttpUrl].toString() || undefined,
data: peerAddress || name,
url: peerAddress || name,
dependencyTypeName: span.kind === SpanKind.INTERNAL ? Constants.DependencyTypeName.InProc : (component || span.name),
dependencyTypeName: span.kind === SpanKind.INTERNAL ? Constants.DependencyTypeName.InProc : span.name,
resultCode: String(span.status.code || 0),
success: span.status.code === 0,
success: span.status.code === SpanStatusCode.OK,
properties: {
...filterSpanAttributes(span.attributes),
"_MS.links": links || undefined
Expand Down
16 changes: 8 additions & 8 deletions Declarations/Constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Contracts = require("./Contracts")
import * as conventions from "@opentelemetry/semantic-conventions";

export const DEFAULT_BREEZE_ENDPOINT = "https://dc.services.visualstudio.com";
export const DEFAULT_LIVEMETRICS_ENDPOINT = "https://rt.services.visualstudio.com";
Expand Down Expand Up @@ -123,16 +124,15 @@ export const TelemetryTypeStringToQuickPulseDocumentType: { [key in Contracts.Te
// OpenTelemetry Span Attributes
export const SpanAttribute = {
// HTTP
HttpHost: "http.host",
HttpMethod: "http.method",
HttpPort: "http.port",
HttpStatusCode: "http.status_code",
HttpUrl: "http.url",
HttpUserAgent: "http.user_agent",
HttpMethod: conventions.SemanticAttributes.HTTP_METHOD,
HttpStatusCode: conventions.SemanticAttributes.HTTP_STATUS_CODE,
HttpUrl: conventions.SemanticAttributes.HTTP_URL,
HttpUserAgent: conventions.SemanticAttributes.HTTP_USER_AGENT,
Comment on lines +127 to +130
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All HTTP related constants reference


// GRPC
GrpcMethod: "grpc.method",
GrpcService: "rpc.service", // rpc not grpc
GrpcStatusCode: conventions.SemanticAttributes.RPC_GRPC_STATUS_CODE,
GrpcMethod: conventions.SemanticAttributes.RPC_METHOD, // rpc not grpc
GrpcService: conventions.SemanticAttributes.RPC_SERVICE, // rpc not grpc
Comment on lines +133 to +135
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All GRPC related constants reference

};

export const DependencyTypeName = {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"dependencies": {
"@azure/core-http": "^2.0.0",
"@opentelemetry/api": "^1.0.0",
"@opentelemetry/semantic-conventions": "^0.23.0",
"@opentelemetry/tracing": "^0.23.0",
"cls-hooked": "^4.2.2",
"continuation-local-storage": "^3.2.1",
Expand Down