Skip to content

Commit

Permalink
[Monitor OpenTelemetry Exporter] Fix Property Truncation (#31004)
Browse files Browse the repository at this point in the history
### Packages impacted by this PR
@azure/monitor-opentelemetry-exporter

### Issues associated with this PR
#30969

### Describe the problem that is addressed by this PR
Enforces truncation on properties parsed from user telemetry.

### Are there test cases added in this PR? _(If not, why?)_
Yes

### Checklists
- [x] Added impacted package name to the issue description
- [ ] Does this PR needs any fixes in the SDK Generator?** _(If so,
create an Issue in the
[Autorest/typescript](https://github.com/Azure/autorest.typescript)
repository and link it here)_
- [x] Added a changelog (if necessary)
  • Loading branch information
JacksonWeber authored Sep 9, 2024
1 parent a637648 commit 703c820
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 8 deletions.
3 changes: 2 additions & 1 deletion sdk/monitor/monitor-opentelemetry-exporter/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

### Other Changes

- Updated OTel dependencies
- Enforce property length limits on telemetry using truncation.
- Updated OTel dependencies.

## 1.0.0-beta.25 (2024-08-14)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ export class FileSystemPersist implements PersistentStorage {
try {
const buffer = await this._getFirstFileOnDisk();
if (buffer) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return JSON.parse(buffer.toString("utf8"));
}
} catch (e: any) {
Expand Down
11 changes: 11 additions & 0 deletions sdk/monitor/monitor-opentelemetry-exporter/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,14 @@ export enum BreezePerformanceCounterNames {
REQUEST_RATE = "\\ASP.NET Applications(??APP_W3SVC_PROC??)\\Requests/Sec",
REQUEST_DURATION = "\\ASP.NET Applications(??APP_W3SVC_PROC??)\\Request Execution Time",
}

/**
* Property Max Lengths
* @internal
*/
export enum MaxPropertyLengths {
NINE_BIT = 512,
TEN_BIT = 1024,
THIRTEEN_BIT = 8192,
FIFTEEN_BIT = 32768,
}
11 changes: 10 additions & 1 deletion sdk/monitor/monitor-opentelemetry-exporter/src/utils/logUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
SEMATTRS_EXCEPTION_STACKTRACE,
SEMATTRS_EXCEPTION_TYPE,
} from "@opentelemetry/semantic-conventions";
import { Measurements, Properties, Tags } from "../types";
import { MaxPropertyLengths, Measurements, Properties, Tags } from "../types";
import { diag } from "@opentelemetry/api";
import {
ApplicationInsightsAvailabilityBaseType,
Expand Down Expand Up @@ -92,6 +92,15 @@ export function logToEnvelope(log: ReadableLogRecord, ikey: string): Envelope |
return;
}
}
// Truncate properties
if (baseData.message) {
baseData.message = String(baseData.message).substring(0, MaxPropertyLengths.FIFTEEN_BIT);
}
if (properties) {
for (const key of Object.keys(properties)) {
properties[key] = String(properties[key]).substring(0, MaxPropertyLengths.THIRTEEN_BIT);
}
}
return {
name,
sampleRate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import {
isSqlDB,
serializeAttribute,
} from "./common";
import { Tags, Properties, MSLink, Measurements } from "../types";
import { Tags, Properties, MSLink, Measurements, MaxPropertyLengths } from "../types";
import { parseEventHubSpan } from "./eventhub";
import { AzureMonitorSampleRate, DependencyTypes, MS_LINKS } from "./constants/applicationinsights";
import { AzNamespace, MicrosoftEventHub } from "./constants/span/azAttributes";
Expand Down Expand Up @@ -345,6 +345,34 @@ export function readableSpanToEnvelope(span: ReadableSpan, ikey: string): Envelo
}
}

// Truncate properties
if (baseData.id) {
baseData.id = baseData.id.substring(0, MaxPropertyLengths.NINE_BIT);
}
if (baseData.name) {
baseData.name = baseData.name.substring(0, MaxPropertyLengths.TEN_BIT);
}
if (baseData.resultCode) {
baseData.resultCode = String(baseData.resultCode).substring(0, MaxPropertyLengths.TEN_BIT);
}
if (baseData.data) {
baseData.data = String(baseData.data).substring(0, MaxPropertyLengths.THIRTEEN_BIT);
}
if (baseData.type) {
baseData.type = String(baseData.type).substring(0, MaxPropertyLengths.TEN_BIT);
}
if (baseData.target) {
baseData.target = String(baseData.target).substring(0, MaxPropertyLengths.TEN_BIT);
}
if (baseData.properties) {
for (const key of Object.keys(baseData.properties)) {
baseData.properties[key] = baseData.properties[key].substring(
0,
MaxPropertyLengths.THIRTEEN_BIT,
);
}
}

return {
name,
sampleRate,
Expand Down Expand Up @@ -438,6 +466,18 @@ export function spanEventsToEnvelopes(span: ReadableSpan, ikey: string): Envelop
if (span.attributes[AzureMonitorSampleRate]) {
sampleRate = Number(span.attributes[AzureMonitorSampleRate]);
}
// Truncate properties
if (baseData.message) {
baseData.message = String(baseData.message).substring(0, MaxPropertyLengths.FIFTEEN_BIT);
}
if (baseData.properties) {
for (const key of Object.keys(baseData.properties)) {
baseData.properties[key] = baseData.properties[key].substring(
0,
MaxPropertyLengths.THIRTEEN_BIT,
);
}
}
const env: Envelope = {
name: name,
time: time,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ describe("FileSystemPersist", () => {
afterEach((done) => {
fs.readdir(tempDir, (err, files) => {
if (err) {
// eslint-disable-next-line no-console
console.error(err);
done();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
SemanticResourceAttributes,
} from "@opentelemetry/semantic-conventions";

import { Tags, Properties, Measurements } from "../../src/types";
import { Tags, Properties, Measurements, MaxPropertyLengths } from "../../src/types";
import { getInstance } from "../../src/platform";
import {
AvailabilityData,
Expand Down Expand Up @@ -217,6 +217,49 @@ describe("logUtils.ts", () => {
);
});

it("should truncate properties of a Message Envelope", () => {
const data: MessageData = {
message: "a".repeat(MaxPropertyLengths.FIFTEEN_BIT + 1),
severityLevel: "Verbose",
measurements: { testMeasurement: 1 },
version: 2,
};
testLogRecord.attributes = {
"_MS.baseType": "MessageData",
"extra.attribute": "foo",
[SemanticAttributes.MESSAGE_TYPE]: "test message type",
};
testLogRecord.body = data;

const expectedTime = hrTimeToDate(testLogRecord.hrTime);
const expectedProperties = {
"extra.attribute": "foo",
[SemanticAttributes.MESSAGE_TYPE]: "test message type",
};
const expectedMeasurements: Measurements = {
testMeasurement: 1,
};
const expectedBaseData: Partial<MessageData> = {
message: "a".repeat(MaxPropertyLengths.FIFTEEN_BIT),
severityLevel: `Verbose`,
version: 2,
properties: expectedProperties,
measurements: expectedMeasurements,
};

const envelope = logToEnvelope(testLogRecord as ReadableLogRecord, "ikey");
assertEnvelope(
envelope,
"Microsoft.ApplicationInsights.Message",
100,
"MessageData",
expectedProperties,
expectedMeasurements,
expectedBaseData,
expectedTime,
);
});

it("should create a Exception Envelope", () => {
const data: TelemetryExceptionData = {
message: "testMessage",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
SEMRESATTRS_SERVICE_NAMESPACE,
} from "@opentelemetry/semantic-conventions";

import { Tags, Properties, Measurements } from "../../src/types";
import { Tags, Properties, Measurements, MaxPropertyLengths } from "../../src/types";
import { Context, getInstance } from "../../src/platform";
import { readableSpanToEnvelope, spanEventsToEnvelopes } from "../../src/utils/spanUtils";
import {
Expand Down Expand Up @@ -1304,6 +1304,41 @@ describe("spanUtils.ts", () => {
version: 2,
};

assertEnvelope(
envelopes[0],
"Microsoft.ApplicationInsights.Message",
100,
"MessageData",
expectedTags,
expectedProperties,
undefined,
expectedBaseData,
);
});
it("should truncate message envelope for span events", () => {
const message = "a".repeat(MaxPropertyLengths.FIFTEEN_BIT + 1);
const span = new Span(
tracer,
ROOT_CONTEXT,
"parent span",
{ traceId: "traceid", spanId: "spanId", traceFlags: 0 },
SpanKind.SERVER,
"parentSpanId",
);
span.addEvent(message);
span.end();
const envelopes = spanEventsToEnvelopes(span, "ikey");

const expectedTags: Tags = {};
expectedTags[KnownContextTagKeys.AiOperationId] = span.spanContext().traceId;
expectedTags[KnownContextTagKeys.AiOperationParentId] = "spanId";
const expectedProperties = {};
const expectedBaseData: Partial<MessageData> = {
message: message.substring(0, MaxPropertyLengths.FIFTEEN_BIT),
properties: expectedProperties,
version: 2,
};

assertEnvelope(
envelopes[0],
"Microsoft.ApplicationInsights.Message",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ describe("#AzureMonitorStatsbeatExporter", () => {

it("should add correct network properites to the custom metric", (done) => {
const statsbeat = new NetworkStatsbeatMetrics(options);
// eslint-disable-next-line no-unused-expressions, @typescript-eslint/no-unused-expressions
// eslint-disable-next-line no-unused-expressions
statsbeat["statsCollectionShortInterval"];
statsbeat.countSuccess(100);
const metric = statsbeat["networkStatsbeatCollection"][0];
Expand Down

0 comments on commit 703c820

Please sign in to comment.