Skip to content

Commit

Permalink
[Azure Monitor OpenTelemetry Exporter] Capture Measurements when Crea…
Browse files Browse the repository at this point in the history
…ting Logs (#29511)

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

### Issues associated with this PR
microsoft/ApplicationInsights-node.js#1318

### Describe the problem that is addressed by this PR
Previously measurements passed from the Application Insights 3.X SDK
were not being populated on logs in the exporter. These should be
populated from the log body and exported to breeze.

### Are there test cases added in this PR? _(If not, why?)_
Yes, added a test case to ensure that when measurements exist in the log
body field, they're populated on the final envelope.

### 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 committed May 1, 2024
1 parent c10ccc2 commit c5d25a2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
6 changes: 6 additions & 0 deletions sdk/monitor/monitor-opentelemetry-exporter/CHANGELOG.md
@@ -1,5 +1,11 @@
# Release History

## Unreleased ()

### Features Added

- Capture and export measurements when creating log records from the Application Insights 3.X SDK.

## 1.0.0-beta.22 (2024-04-16)

### Features Added
Expand Down
11 changes: 10 additions & 1 deletion sdk/monitor/monitor-opentelemetry-exporter/src/utils/logUtils.ts
Expand Up @@ -45,7 +45,7 @@ export function logToEnvelope(log: ReadableLogRecord, ikey: string): Envelope |
const sampleRate = 100;
const instrumentationKey = ikey;
const tags = createTagsFromLog(log);
const [properties, measurements] = createPropertiesFromLog(log);
let [properties, measurements] = createPropertiesFromLog(log);
let name: string;
let baseType: string;
let baseData: MonitorDomain;
Expand Down Expand Up @@ -85,6 +85,7 @@ export function logToEnvelope(log: ReadableLogRecord, ikey: string): Envelope |
baseType = String(log.attributes[ApplicationInsightsBaseType]);
name = getLegacyApplicationInsightsName(log);
baseData = getLegacyApplicationInsightsBaseData(log);
measurements = getLegacyApplicationInsightsMeasurements(log);
if (!baseData) {
// Failed to parse log
return;
Expand Down Expand Up @@ -180,6 +181,14 @@ function getLegacyApplicationInsightsName(log: ReadableLogRecord): string {
return name;
}

function getLegacyApplicationInsightsMeasurements(log: ReadableLogRecord): Measurements {
let measurements: Measurements = {};
if ((log.body as MonitorDomain)?.measurements) {
measurements = { ...(log.body as MonitorDomain).measurements };
}
return measurements;
}

function getLegacyApplicationInsightsBaseData(log: ReadableLogRecord): MonitorDomain {
let baseData: MonitorDomain = {
version: 2,
Expand Down
Expand Up @@ -114,7 +114,7 @@ describe("logUtils.ts", () => {
severityLevel: `Information`,
version: 2,
properties: expectedProperties,
measurements: {},
measurements: emptyMeasurements,
};

const envelope = logToEnvelope(testLogRecord as ReadableLogRecord, "ikey");
Expand Down Expand Up @@ -178,6 +178,7 @@ describe("logUtils.ts", () => {
const data: MessageData = {
message: "testMessage",
severityLevel: "Verbose",
measurements: { testMeasurement: 1 },
version: 2,
};
testLogRecord.attributes = {
Expand All @@ -192,12 +193,15 @@ describe("logUtils.ts", () => {
"extra.attribute": "foo",
[SemanticAttributes.MESSAGE_TYPE]: "test message type",
};
const expectedMeasurements: Measurements = {
testMeasurement: 1,
};
const expectedBaseData: Partial<MessageData> = {
message: `testMessage`,
severityLevel: `Verbose`,
version: 2,
properties: expectedProperties,
measurements: {},
measurements: expectedMeasurements,
};

const envelope = logToEnvelope(testLogRecord as ReadableLogRecord, "ikey");
Expand All @@ -207,7 +211,7 @@ describe("logUtils.ts", () => {
100,
"MessageData",
expectedProperties,
emptyMeasurements,
expectedMeasurements,
expectedBaseData,
expectedTime,
);
Expand Down

0 comments on commit c5d25a2

Please sign in to comment.