Skip to content
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
4 changes: 4 additions & 0 deletions sdk/monitor/monitor-opentelemetry-exporter/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ export const legacySemanticValues = [
*/
export enum experimentalOpenTelemetryValues {
SYNTHETIC_TYPE = "user_agent.synthetic.type",
ATTR_ENDUSER_PSEUDO_ID = "enduser.pseudo.id",
ATTR_ENDUSER_ID = "enduser.id",
}

/**
Expand Down Expand Up @@ -210,6 +212,8 @@ export const httpSemanticValues = [
ATTR_EXCEPTION_TYPE,
ATTR_EXCEPTION_MESSAGE,
ATTR_EXCEPTION_STACKTRACE,
experimentalOpenTelemetryValues.ATTR_ENDUSER_ID,
experimentalOpenTelemetryValues.ATTR_ENDUSER_PSEUDO_ID,
experimentalOpenTelemetryValues.SYNTHETIC_TYPE,
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
ATTR_EXCEPTION_STACKTRACE,
ATTR_EXCEPTION_TYPE,
} from "@opentelemetry/semantic-conventions";
import { experimentalOpenTelemetryValues } from "../types.js";
import type { Measurements, Properties, Tags } from "../types.js";
import { httpSemanticValues, legacySemanticValues, MaxPropertyLengths } from "../types.js";
import type { Attributes } from "@opentelemetry/api";
Expand Down Expand Up @@ -161,6 +162,23 @@ function createTagsFromLog(log: ReadableLogRecord): Tags {
tags[KnownContextTagKeys.AiLocationIp] = String(microsoftClientIp);
}

// Map user ID attributes
const attributes = log.attributes as Attributes;
if (attributes[experimentalOpenTelemetryValues.ATTR_ENDUSER_ID]) {
const endUserId = String(attributes[experimentalOpenTelemetryValues.ATTR_ENDUSER_ID]);
if (endUserId && endUserId.length > 0) {
tags[KnownContextTagKeys.AiUserAuthUserId] = endUserId;
}
}
if (attributes[experimentalOpenTelemetryValues.ATTR_ENDUSER_PSEUDO_ID]) {
const endUserPseudoId = String(
attributes[experimentalOpenTelemetryValues.ATTR_ENDUSER_PSEUDO_ID],
);
if (endUserPseudoId && endUserPseudoId.length > 0) {
tags[KnownContextTagKeys.AiUserId] = endUserPseudoId;
}
}

return tags;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
SEMATTRS_DB_OPERATION,
SEMATTRS_DB_STATEMENT,
SEMATTRS_DB_SYSTEM,
SEMATTRS_ENDUSER_ID,
SEMATTRS_EXCEPTION_ESCAPED,
SEMATTRS_EXCEPTION_MESSAGE,
SEMATTRS_EXCEPTION_STACKTRACE,
Expand Down Expand Up @@ -62,6 +61,7 @@ import {
internalMicrosoftAttributes,
legacySemanticValues,
MaxPropertyLengths,
experimentalOpenTelemetryValues,
} from "../types.js";
import { parseEventHubSpan } from "./eventhub.js";
import {
Expand All @@ -88,10 +88,18 @@ function createTagsFromSpan(span: ReadableSpan): Tags {
if (span.parentSpanContext?.spanId) {
tags[KnownContextTagKeys.AiOperationParentId] = span.parentSpanContext.spanId;
}
const endUserId = span.attributes[SEMATTRS_ENDUSER_ID];

// Map OpenTelemetry enduser attributes to Application Insights user attributes
const endUserId = span.attributes[experimentalOpenTelemetryValues.ATTR_ENDUSER_ID];
if (endUserId) {
tags[KnownContextTagKeys.AiUserId] = String(endUserId);
tags[KnownContextTagKeys.AiUserAuthUserId] = String(endUserId);
}

const endUserPseudoId = span.attributes[experimentalOpenTelemetryValues.ATTR_ENDUSER_PSEUDO_ID];
if (endUserPseudoId) {
tags[KnownContextTagKeys.AiUserId] = String(endUserPseudoId);
}

const httpUserAgent = getUserAgent(span.attributes);
if (httpUserAgent) {
// TODO: Not exposed in Swagger, need to update def
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -567,4 +567,68 @@ describe("logUtils.ts", () => {
expectedServiceTagsBase,
);
});

it("should map ATTR_ENDUSER_ID to ai.user.authUserId in log tags", () => {
testLogRecord.body = "Test message";
testLogRecord.severityLevel = "Information";
testLogRecord.attributes = {
[experimentalOpenTelemetryValues.ATTR_ENDUSER_ID]: "test-auth-user-id",
"extra.attribute": "foo",
[experimentalOpenTelemetryValues.SYNTHETIC_TYPE]: "",
};

const envelope = logToEnvelope(testLogRecord as ReadableLogRecord, "ikey");

// Verify the user auth ID is mapped to the correct tag
assert.deepStrictEqual(envelope?.tags, {
...context.tags,
...expectedServiceTagsBase,
[KnownContextTagKeys.AiUserAuthUserId]: "test-auth-user-id",
});

// Verify properties don't include the user ID (it should be filtered out)
assert.deepStrictEqual((envelope?.data?.baseData as any).properties, {
"extra.attribute": "foo",
});

// Verify that ATTR_ENDUSER_ID is not in properties
assert.ok(
envelope &&
!envelope.data?.baseData?.properties?.[experimentalOpenTelemetryValues.ATTR_ENDUSER_ID],
"ATTR_ENDUSER_ID should not be included in properties",
);
});

it("should map ATTR_ENDUSER_PSEUDO_ID to ai.user.id in log tags", () => {
testLogRecord.body = "Test message";
testLogRecord.severityLevel = "Information";
testLogRecord.attributes = {
[experimentalOpenTelemetryValues.ATTR_ENDUSER_PSEUDO_ID]: "test-pseudo-user-id",
"extra.attribute": "foo",
[experimentalOpenTelemetryValues.SYNTHETIC_TYPE]: "",
};

const envelope = logToEnvelope(testLogRecord as ReadableLogRecord, "ikey");

// Verify the pseudo user ID is mapped to the correct tag
assert.deepStrictEqual(envelope?.tags, {
...context.tags,
...expectedServiceTagsBase,
[KnownContextTagKeys.AiUserId]: "test-pseudo-user-id",
});

// Verify properties don't include the user ID (it should be filtered out)
assert.deepStrictEqual((envelope?.data?.baseData as any).properties, {
"extra.attribute": "foo",
});

// Verify that ATTR_ENDUSER_PSEUDO_ID is not in properties
assert.ok(
envelope &&
!envelope.data?.baseData?.properties?.[
experimentalOpenTelemetryValues.ATTR_ENDUSER_PSEUDO_ID
],
"ATTR_ENDUSER_PSEUDO_ID should not be included in properties",
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import {
SEMRESATTRS_SERVICE_INSTANCE_ID,
SEMRESATTRS_SERVICE_NAME,
SEMRESATTRS_SERVICE_NAMESPACE,
SEMATTRS_ENDUSER_ID,
} from "@opentelemetry/semantic-conventions";

import type { Tags, Properties, Measurements } from "../../src/types.js";
Expand Down Expand Up @@ -1536,13 +1535,13 @@ describe("spanUtils.ts", () => {
expectedBaseData,
);
});
it("should ensure SEMATTRS_ENDUSER_ID is not included in properties", () => {
it("should ensure ATTR_ENDUSER_ID is not included in properties", () => {
const spanOptions: SpanOptions = {
kind: SpanKind.SERVER,
};
const span = tracer.startSpan("span", spanOptions, ROOT_CONTEXT);
span.setAttributes({
[SEMATTRS_ENDUSER_ID]: "test-user-id",
[experimentalOpenTelemetryValues.ATTR_ENDUSER_ID]: "test-user-id",
"extra.attribute": "foo",
});
span.setStatus({
Expand All @@ -1552,7 +1551,7 @@ describe("spanUtils.ts", () => {
const readableSpan = spanToReadableSpan(span);
const expectedTags: Tags = {};
expectedTags[KnownContextTagKeys.AiOperationId] = span.spanContext().traceId;
expectedTags[KnownContextTagKeys.AiUserId] = "test-user-id";
expectedTags[KnownContextTagKeys.AiUserAuthUserId] = "test-user-id";
expectedTags[KnownContextTagKeys.AiOperationName] = "span";

const expectedProperties = {
Expand Down Expand Up @@ -1582,10 +1581,65 @@ describe("spanUtils.ts", () => {
expectedBaseData,
);

// Specifically verify that SEMATTRS_ENDUSER_ID is not in properties
// Specifically verify that ATTR_ENDUSER_ID is not in properties
assert.ok(
!envelope.data?.baseData?.properties?.[SEMATTRS_ENDUSER_ID],
"SEMATTRS_ENDUSER_ID should not be included in properties",
!envelope.data?.baseData?.properties?.[experimentalOpenTelemetryValues.ATTR_ENDUSER_ID],
"ATTR_ENDUSER_ID should not be included in properties",
);
});

it("should ensure ATTR_ENDUSER_PSEUDO_ID is mapped to ai.user.id and not included in properties", () => {
const spanOptions: SpanOptions = {
kind: SpanKind.SERVER,
};
const span = tracer.startSpan("span", spanOptions, ROOT_CONTEXT);
span.setAttributes({
[experimentalOpenTelemetryValues.ATTR_ENDUSER_PSEUDO_ID]: "test-pseudo-user-id",
"extra.attribute": "foo",
});
span.setStatus({
code: SpanStatusCode.OK,
});
span.end();
const readableSpan = spanToReadableSpan(span);
const expectedTags: Tags = {};
expectedTags[KnownContextTagKeys.AiOperationId] = span.spanContext().traceId;
expectedTags[KnownContextTagKeys.AiUserId] = "test-pseudo-user-id";
expectedTags[KnownContextTagKeys.AiOperationName] = "span";

const expectedProperties = {
"extra.attribute": "foo",
};

const expectedBaseData: Partial<RequestData> = {
id: `${span.spanContext().spanId}`,
success: true,
responseCode: "0",
name: `span`,
version: 2,
source: undefined,
properties: expectedProperties,
measurements: {},
};

const envelope = readableSpanToEnvelope(readableSpan, "ikey");
assertEnvelope(
envelope,
"Microsoft.ApplicationInsights.Request",
100,
"RequestData",
expectedTags,
expectedProperties,
emptyMeasurements,
expectedBaseData,
);

// Specifically verify that ATTR_ENDUSER_PSEUDO_ID is not in properties
assert.ok(
!envelope.data?.baseData?.properties?.[
experimentalOpenTelemetryValues.ATTR_ENDUSER_PSEUDO_ID
],
"ATTR_ENDUSER_PSEUDO_ID should not be included in properties",
);
});
});