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

[OpenTelemetry Exporter] Update AI mapping with latest spec changes #17916

Merged
merged 2 commits into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license.

import os from "os";
import { URL } from "url";
import { ReadableSpan } from "@opentelemetry/tracing";
import { hrTimeToMilliseconds } from "@opentelemetry/core";
import { diag, SpanKind, SpanStatusCode, Link } from "@opentelemetry/api";
Expand Down Expand Up @@ -56,10 +57,22 @@ function createTagsFromSpan(span: ReadableSpan): Tags {
}
if (span.kind === SpanKind.SERVER) {
const httpMethod = span.attributes[SemanticAttributes.HTTP_METHOD];
let httpClientIp = span.attributes[SemanticAttributes.HTTP_CLIENT_IP];
let netPeerIp = span.attributes[SemanticAttributes.NET_PEER_IP];
const httpClientIp = span.attributes[SemanticAttributes.HTTP_CLIENT_IP];
const netPeerIp = span.attributes[SemanticAttributes.NET_PEER_IP];
if (httpMethod) {
tags[KnownContextTagKeys.AiOperationName] = `${httpMethod as string} ${span.name as string}`;
const httpRoute = span.attributes[SemanticAttributes.HTTP_ROUTE];
const httpUrl = span.attributes[SemanticAttributes.HTTP_URL];
tags[KnownContextTagKeys.AiOperationName] = span.name; // Default
if (httpRoute) {
tags[KnownContextTagKeys.AiOperationName] = `${httpMethod as string} ${httpRoute as string}`;
}
else if (httpUrl) {
try {
let url = new URL(String(httpUrl));
tags[KnownContextTagKeys.AiOperationName] = `${httpMethod} ${url.pathname}`;
Copy link
Member

Choose a reason for hiding this comment

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

Will the path name change to / if empty?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes

}
catch (ex) { }
}
if (httpClientIp) {
tags[KnownContextTagKeys.AiLocationIp] = String(httpClientIp);
} else if (netPeerIp) {
Expand Down Expand Up @@ -182,7 +195,7 @@ function getDependencyTarget(span: ReadableSpan): string {

function createDependencyData(span: ReadableSpan): RemoteDependencyData {
const remoteDependencyData: RemoteDependencyData = {
name: span.name,
name: span.name, //Default
id: `${span.spanContext().spanId}`,
success: span.status.code != SpanStatusCode.ERROR,
resultCode: "0",
Expand All @@ -202,6 +215,14 @@ function createDependencyData(span: ReadableSpan): RemoteDependencyData {
const rpcSystem = span.attributes[SemanticAttributes.RPC_SYSTEM];
// HTTP Dependency
if (httpMethod) {
const httpUrl = span.attributes[SemanticAttributes.HTTP_URL];
Copy link
Member

@lzchen lzchen Sep 29, 2021

Choose a reason for hiding this comment

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

Curious, accessing dictionary attributes won't through an exception if it doesn't exist in JS?

Copy link
Member Author

Choose a reason for hiding this comment

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

This will only throw if span.attributes doesn't exist, in this case if HTTP_URL is not there will return undefined

if (httpUrl) {
try {
let dependencyUrl = new URL(String(httpUrl));
remoteDependencyData.name = `${httpMethod} ${dependencyUrl.pathname}`;
Copy link
Member

Choose a reason for hiding this comment

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

Comment about default / applies here as well.

Copy link
Member Author

Choose a reason for hiding this comment

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

Is added by URL object, there must be a test in this PR where this is validated

}
catch (ex) { }
}
remoteDependencyData.type = DependencyTypes.Http;
remoteDependencyData.data = getUrl(span);
const httpStatusCode = span.attributes[SemanticAttributes.HTTP_STATUS_CODE];
Expand All @@ -222,7 +243,7 @@ function createDependencyData(span: ReadableSpan): RemoteDependencyData {
target = res[1] + res[2] + res[4];
}
}
} catch (error) {}
} catch (error) { }
remoteDependencyData.target = `${target}`;
}
}
Expand All @@ -243,13 +264,17 @@ function createDependencyData(span: ReadableSpan): RemoteDependencyData {
remoteDependencyData.type = String(dbSystem);
}
const dbStatement = span.attributes[SemanticAttributes.DB_STATEMENT];
const dbOperation = span.attributes[SemanticAttributes.DB_OPERATION];
if (dbStatement) {
remoteDependencyData.data = String(dbStatement);
}
else if (dbOperation) {
remoteDependencyData.data = String(dbOperation);
}
let target = getDependencyTarget(span);
const dbName = span.attributes[SemanticAttributes.DB_NAME];
if (target) {
remoteDependencyData.target = dbName ? `${target}/${dbName}` : `${target}`;
remoteDependencyData.target = dbName ? `${target}|${dbName}` : `${target}`;
} else {
remoteDependencyData.target = dbName ? `${dbName}` : `${dbSystem}`;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ describe("spanUtils.ts", () => {
const expectedTags: Tags = {};
expectedTags[KnownContextTagKeys.AiOperationId] = "traceid";
expectedTags[KnownContextTagKeys.AiOperationParentId] = "parentSpanId";
expectedTags[KnownContextTagKeys.AiOperationName] = "GET parent span";
expectedTags[KnownContextTagKeys.AiOperationName] = "GET /api/example";

const expectedProperties = {
"extra.attribute": "foo"
Expand All @@ -319,7 +319,7 @@ describe("spanUtils.ts", () => {
success: true,
responseCode: "200",
url: "https://example.com/api/example",
name: `GET parent span`,
name: expectedTags[KnownContextTagKeys.AiOperationName],
version: 2,
source: undefined,
properties: expectedProperties,
Expand Down Expand Up @@ -372,7 +372,7 @@ describe("spanUtils.ts", () => {
type: "Http",
target: "https://someotherexample.com/api/example",
data: "https://example.com/api/example",
name: `parent span`,
name: `GET /api/example`,
version: 2,
properties: expectedProperties,
measurements: {}
Expand Down