Skip to content

Commit

Permalink
Add collection of metrics to events when measurements are passed.
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonWeber committed Apr 26, 2024
1 parent 0a74f1a commit 708553d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
9 changes: 9 additions & 0 deletions src/shim/logsApi.ts
Expand Up @@ -19,6 +19,7 @@ import {
} from "../declarations/generated";
import { Util } from "../shared/util";
import { parseStack } from "../logs/exceptions";
import { defaultClient } from "./applicationinsights";

/**
* Log manual API to generate Application Insights telemetry
Expand Down Expand Up @@ -104,6 +105,14 @@ export class LogApi {
try {
const logRecord = this._eventToLogRecord(telemetry);
this._logger.emit(logRecord);
if (Object.keys(telemetry.measurements).length > 0) {
for (const [key, value] of Object.entries(telemetry.measurements)) {
defaultClient.trackMetric({
name: key,
value: value,
});
}
}
} catch (err) {
diag.error("Failed to send telemetry.", err);
}
Expand Down
12 changes: 9 additions & 3 deletions test/unitTests/logs/api.tests.ts
Expand Up @@ -3,7 +3,6 @@ import * as sinon from "sinon";
import * as nock from "nock";
import { Logger } from "@opentelemetry/api-logs";
import { LogRecord } from "@opentelemetry/sdk-logs";

import {
AvailabilityTelemetry,
EventTelemetry,
Expand All @@ -14,6 +13,7 @@ import {
} from "../../../src/declarations/contracts";
import { AvailabilityData, MessageData, MonitorDomain, PageViewData, TelemetryEventData, TelemetryExceptionData } from "../../../src/declarations/generated";
import { LogApi } from "../../../src/shim/logsApi";
import { defaultClient, setup, dispose } from "../../../applicationinsights";

describe("logs/API", () => {
let sandbox: sinon.SinonSandbox;
Expand All @@ -31,7 +31,7 @@ describe("logs/API", () => {
sandbox.restore();
});

after(() => {
after(async () => {
nock.cleanAll();
nock.enableNetConnect();
});
Expand Down Expand Up @@ -170,7 +170,10 @@ describe("logs/API", () => {
assert.equal(logs[0].attributes["_MS.baseType"], "ExceptionData");
});

it("trackEvent", () => {
it("trackEvent with measurements", async () => {
setup("InstrumentationKey=1aa11111-bbbb-1ccc-8ddd-eeeeffff3330;IngestionEndpoint=https://centralus-0.in.applicationinsights.azure.com/");
const stub = sandbox.stub(defaultClient, "trackMetric");

let testLogger = new TestLogger();
let logApi = new LogApi(testLogger);
const measurements: { [key: string]: number } = {};
Expand All @@ -187,6 +190,9 @@ describe("logs/API", () => {
assert.equal(baseData.name, "TestName");
assert.equal(baseData.measurements["test"], 123);
assert.equal(logs[0].attributes["_MS.baseType"], "EventData");

assert.ok(stub.calledOnce);
await dispose();
});
});
});
4 changes: 2 additions & 2 deletions test/unitTests/shim/telemetryClient.tests.ts
Expand Up @@ -57,10 +57,10 @@ describe("shim/TelemetryClient", () => {
});


after(() => {
after(async () => {
nock.cleanAll();
nock.enableNetConnect();
client.shutdown();
await client.shutdown();
});

class TestSpanProcessor implements SpanProcessor {
Expand Down

0 comments on commit 708553d

Please sign in to comment.