Skip to content

Commit

Permalink
[monitor-opentelemetry] Fix issue with standard metrics dependency ca…
Browse files Browse the repository at this point in the history
…rdinality (#30468)

### Packages impacted by this PR
@azure/monitor-opentelemetry

### Issues associated with this PR
microsoft/ApplicationInsights-node.js#1342
  • Loading branch information
hectorhdzg authored Jul 22, 2024
1 parent 8e1e066 commit f1ecbf9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 21 deletions.
17 changes: 5 additions & 12 deletions sdk/monitor/monitor-opentelemetry/src/metrics/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ import {
SEMRESATTRS_SERVICE_NAMESPACE,
SEMRESATTRS_SERVICE_INSTANCE_ID,
SEMATTRS_PEER_SERVICE,
SEMATTRS_HTTP_HOST,
SEMATTRS_HTTP_URL,
SEMATTRS_NET_PEER_NAME,
SEMATTRS_NET_PEER_IP,
SEMATTRS_NET_HOST_PORT,
SEMATTRS_EXCEPTION_MESSAGE,
SEMATTRS_EXCEPTION_TYPE,
SEMATTRS_HTTP_USER_AGENT,
Expand Down Expand Up @@ -88,25 +86,20 @@ export function getBaseDimensions(resource: Resource): StandardMetricBaseDimensi
return dimensions;
}

// Get metric dependency target, avoiding high cardinality.
export function getDependencyTarget(attributes: Attributes): string {
if (!attributes) {
return "";
}
const peerService = attributes[SEMATTRS_PEER_SERVICE];
const httpHost = attributes[SEMATTRS_HTTP_HOST];
const httpUrl = attributes[SEMATTRS_HTTP_URL];
const hostPort = attributes[SEMATTRS_NET_HOST_PORT];
const netPeerName = attributes[SEMATTRS_NET_PEER_NAME];
const netPeerIp = attributes[SEMATTRS_NET_PEER_IP];
if (peerService) {
return String(peerService);
} else if (httpHost) {
return String(httpHost);
} else if (httpUrl) {
return String(httpUrl);
} else if (hostPort && netPeerName) {
return `${netPeerName}:${hostPort}`;
} else if (netPeerName) {
return String(netPeerName);
} else if (netPeerIp) {
return String(netPeerIp);
}
return "";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import { Attributes, SpanKind, SpanStatusCode } from "@opentelemetry/api";
import { Histogram } from "@opentelemetry/sdk-metrics";
import {
SEMATTRS_HTTP_STATUS_CODE,
SEMATTRS_HTTP_URL,
SEMATTRS_NET_HOST_PORT,
SEMATTRS_HTTP_USER_AGENT,
SEMATTRS_NET_PEER_IP,
SEMATTRS_NET_PEER_NAME,
SEMATTRS_PEER_SERVICE,
SEMRESATTRS_SERVICE_INSTANCE_ID,
Expand Down Expand Up @@ -273,17 +272,17 @@ describe("#StandardMetricsHandler", () => {
);
});

it("should set depenedncy targets", () => {
it("should set dependency targets", () => {
let attributes: Attributes;

attributes = { [SEMATTRS_HTTP_URL]: "http://testHttpHost" };
assert.strictEqual(getDependencyTarget(attributes), "http://testHttpHost");
attributes = { [SEMATTRS_PEER_SERVICE]: "TestService" };
assert.strictEqual(getDependencyTarget(attributes), "TestService");

attributes = { [SEMATTRS_NET_PEER_NAME]: "testNetPeerName" };
assert.strictEqual(getDependencyTarget(attributes), "testNetPeerName");
attributes = { [SEMATTRS_NET_PEER_NAME]: "test.com" };
assert.strictEqual(getDependencyTarget(attributes), "test.com");

attributes = { [SEMATTRS_NET_PEER_IP]: "testNetPeerIp" };
assert.strictEqual(getDependencyTarget(attributes), "testNetPeerIp");
attributes = { [SEMATTRS_NET_PEER_NAME]: "test.com", [SEMATTRS_NET_HOST_PORT]: "8080" };
assert.strictEqual(getDependencyTarget(attributes), "test.com:8080");

attributes = { "unknown.attribute": "value" };
assert.strictEqual(getDependencyTarget(attributes), "");
Expand Down

0 comments on commit f1ecbf9

Please sign in to comment.