Skip to content

Add metadata configuration object to TelemetryItem, for use in .track(...) function to enable customizing event property truncation threshold #2419

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ export function EventEnvelopeCreator(logger: IDiagnosticLogger, telemetryItem: I
_convertPropsUndefinedToCustomDefinedValue(customProperties, customUndefinedValue);
}
const eventName = telemetryItem[strBaseData].name;
const eventData = new Event(logger, eventName, customProperties, customMeasurements);
const eventData = new Event(logger, eventName, customProperties, customMeasurements, telemetryItem.metadata["maxLength"]);
const data = new Data<Event>(Event.dataType, eventData);
return _createEnvelope<Event>(logger, Event.envelopeType, telemetryItem, data);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { strRepeat } from "@nevware21/ts-utils";
import { Assert, AITestClass } from "@microsoft/ai-test-framework";
import { DiagnosticLogger } from "@microsoft/applicationinsights-core-js";
import { dataSanitizeInput, dataSanitizeKey, dataSanitizeMessage, DataSanitizerValues, dataSanitizeString } from "../../../src/Telemetry/Common/DataSanitizer";
import { dataSanitizeInput, dataSanitizeKey, dataSanitizeMessage, dataSanitizeProperties, DataSanitizerValues, dataSanitizeString } from "../../../src/Telemetry/Common/DataSanitizer";


export class ApplicationInsightsTests extends AITestClass {
Expand Down Expand Up @@ -34,13 +34,55 @@ export class ApplicationInsightsTests extends AITestClass {
}
});

this.testCase({
name: 'DataSanitizerTests: property sanitizer respects default truncation limit.',
test: () => {
// const define
const MAX_PROPERTY_LENGTH: number = DataSanitizerValues.MAX_PROPERTY_LENGTH;

// use cases
const messageShort: string = "hi";
const messageLong: string = strRepeat("abc", MAX_PROPERTY_LENGTH + 1);
const testProperties = {
messageLong,
messageShort
}

// Assert
Assert.equal(messageShort.length, dataSanitizeProperties(this.logger, testProperties).messageShort.length);
Assert.notEqual(messageLong.length, dataSanitizeProperties(this.logger, testProperties).messageLong.length);
Assert.equal(MAX_PROPERTY_LENGTH, dataSanitizeProperties(this.logger, testProperties).messageLong.length);
}
})

this.testCase({
name: 'DataSanitizerTests: property sanitizer respects max length parameter being passed in.',
test: () => {
// const define
const customMaxLength = 5;

// use cases
const messageShort: string = "hi";
const messageLong: string = strRepeat("abc", customMaxLength + 1);
const testProperties = {
messageLong,
messageShort
}

// Assert
Assert.equal(messageShort.length, dataSanitizeProperties(this.logger, testProperties, customMaxLength).messageShort.length);
Assert.notEqual(messageLong.length, dataSanitizeProperties(this.logger, testProperties, customMaxLength).messageLong.length);
Assert.equal(customMaxLength, dataSanitizeProperties(this.logger, testProperties, customMaxLength).messageLong.length);
}
})

this.testCase({
name: 'DataSanitizerTests: throwInternal function is called correctly in sanitizeMessage function',
test: () => {
// const define
const loggerStub = this.sandbox.stub(this.logger , "throwInternal");
const MAX_MESSAGE_LENGTH = DataSanitizerValues.MAX_MESSAGE_LENGTH;

// use cases
const messageShort: String = "hi";
const messageLong = strRepeat("a", MAX_MESSAGE_LENGTH + 2);
Expand Down Expand Up @@ -139,7 +181,7 @@ export class ApplicationInsightsTests extends AITestClass {

// const define
const MAX_STRING_LENGTH = DataSanitizerValues.MAX_STRING_LENGTH;

// use cases
const strShort: String = "hi";
const strLong = strRepeat("a", MAX_STRING_LENGTH + 2);
Expand All @@ -162,7 +204,7 @@ export class ApplicationInsightsTests extends AITestClass {
test: () => {
// const define
const MAX_NAME_LENGTH = DataSanitizerValues.MAX_NAME_LENGTH;

// use cases
const nameShort: String = "hi";
const nameLong = strRepeat("a", MAX_NAME_LENGTH + 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export function dataSanitizeException(logger: IDiagnosticLogger, exception: any)
return exceptionTrunc || exception;
}

export function dataSanitizeProperties(logger: IDiagnosticLogger, properties: any) {
export function dataSanitizeProperties(logger: IDiagnosticLogger, properties: any, maxLength: number = DataSanitizerValues.MAX_PROPERTY_LENGTH) {
if (properties) {
const tempProps = {};
objForEachKey(properties, (prop, value) => {
Expand All @@ -146,7 +146,7 @@ export function dataSanitizeProperties(logger: IDiagnosticLogger, properties: an
_throwInternal(logger,eLoggingSeverity.WARNING, _eInternalMessageId.CannotSerializeObjectNonSerializable, "custom property is not valid", { exception: e}, true);
}
}
value = dataSanitizeString(logger, value, DataSanitizerValues.MAX_PROPERTY_LENGTH);
value = dataSanitizeString(logger, value, maxLength);
prop = dataSanitizeKeyAndAddUniqueness(logger, prop, tempProps);
tempProps[prop] = value;
});
Expand Down
4 changes: 2 additions & 2 deletions shared/AppInsightsCommon/src/Telemetry/Event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ export class Event implements IEventData, ISerializable {
/**
* Constructs a new instance of the EventTelemetry object
*/
constructor(logger: IDiagnosticLogger, name: string, properties?: any, measurements?: any) {
constructor(logger: IDiagnosticLogger, name: string, properties?: any, measurements?: any, maxLength?: number) {
let _self = this;
_self.ver = 2;
_self.name = dataSanitizeString(logger, name) || strNotSpecified;
_self.properties = dataSanitizeProperties(logger, properties);
_self.properties = dataSanitizeProperties(logger, properties, maxLength);
_self.measurements = dataSanitizeMeasurements(logger, measurements);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ export interface ITelemetryItem {
*/
baseData?: { [key: string]: any };

/**
* Custom metadata
*/
metadata?: { [key: string]: any };

}

export interface Tags {
Expand Down
Loading