Skip to content

Commit

Permalink
[OpenTelemetry Exporter] Update AI mapping with latest spec changes (#…
Browse files Browse the repository at this point in the history
…17916)

* Update with latest mapping spec

* Format
  • Loading branch information
hectorhdzg authored Sep 30, 2021
1 parent 0dea73f commit 4aeb47a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
33 changes: 28 additions & 5 deletions sdk/monitor/monitor-opentelemetry-exporter/src/utils/spanUtils.ts
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}`;
} 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,13 @@ function createDependencyData(span: ReadableSpan): RemoteDependencyData {
const rpcSystem = span.attributes[SemanticAttributes.RPC_SYSTEM];
// HTTP Dependency
if (httpMethod) {
const httpUrl = span.attributes[SemanticAttributes.HTTP_URL];
if (httpUrl) {
try {
let dependencyUrl = new URL(String(httpUrl));
remoteDependencyData.name = `${httpMethod} ${dependencyUrl.pathname}`;
} catch (ex) {}
}
remoteDependencyData.type = DependencyTypes.Http;
remoteDependencyData.data = getUrl(span);
const httpStatusCode = span.attributes[SemanticAttributes.HTTP_STATUS_CODE];
Expand Down Expand Up @@ -243,13 +263,16 @@ 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

0 comments on commit 4aeb47a

Please sign in to comment.