diff --git a/sdk/monitor/monitor-opentelemetry-exporter/src/utils/spanUtils.ts b/sdk/monitor/monitor-opentelemetry-exporter/src/utils/spanUtils.ts index ced142d83e52..136e633b4f02 100644 --- a/sdk/monitor/monitor-opentelemetry-exporter/src/utils/spanUtils.ts +++ b/sdk/monitor/monitor-opentelemetry-exporter/src/utils/spanUtils.ts @@ -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"; @@ -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) { @@ -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", @@ -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]; @@ -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}`; } diff --git a/sdk/monitor/monitor-opentelemetry-exporter/test/internal/spanUtils.test.ts b/sdk/monitor/monitor-opentelemetry-exporter/test/internal/spanUtils.test.ts index 9c18a61e7d66..b0ec9e38c9f7 100644 --- a/sdk/monitor/monitor-opentelemetry-exporter/test/internal/spanUtils.test.ts +++ b/sdk/monitor/monitor-opentelemetry-exporter/test/internal/spanUtils.test.ts @@ -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" @@ -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, @@ -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: {}