Skip to content

Commit

Permalink
[Azure Monitor OpenTelemetry ]Fix issue with wrong values for Statsbe…
Browse files Browse the repository at this point in the history
…at Features and Instrumentations (#27330)

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

Fixed issue with features/instrumentations were ignored when set outside
of this package
  • Loading branch information
hectorhdzg authored Oct 5, 2023
1 parent 677a921 commit 47db633
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 14 deletions.
8 changes: 6 additions & 2 deletions sdk/monitor/monitor-opentelemetry/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function shutdownAzureMonitor() {
}

function _setStatsbeatFeatures(config: InternalConfig) {
let instrumentationBitMap = 0;
let instrumentationBitMap = StatsbeatInstrumentation.NONE;
if (config.instrumentationOptions?.azureSdk?.enabled) {
instrumentationBitMap |= StatsbeatInstrumentation.AZURE_CORE_TRACING;
}
Expand All @@ -68,10 +68,14 @@ function _setStatsbeatFeatures(config: InternalConfig) {
instrumentationBitMap |= StatsbeatInstrumentation.REDIS;
}

let featureBitMap = 0;
let featureBitMap = StatsbeatFeature.NONE;
featureBitMap |= StatsbeatFeature.DISTRO;

try {
const currentFeaturesBitMap = Number(process.env[AZURE_MONITOR_STATSBEAT_FEATURES]);
if (!isNaN(currentFeaturesBitMap)) {
featureBitMap |= currentFeaturesBitMap;
}
process.env[AZURE_MONITOR_STATSBEAT_FEATURES] = JSON.stringify({
instrumentation: instrumentationBitMap,
feature: featureBitMap,
Expand Down
20 changes: 11 additions & 9 deletions sdk/monitor/monitor-opentelemetry/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ export const AZURE_MONITOR_OPENTELEMETRY_VERSION = "1.0.0";
export const AZURE_MONITOR_STATSBEAT_FEATURES = "AZURE_MONITOR_STATSBEAT_FEATURES";

export enum StatsbeatFeature {
DISK_RETRY = 0,
AAD_HANDLING = 1,
WEB_SNIPPET = 2,
DISTRO = 4,
NONE = 0,
DISK_RETRY = 1,
AAD_HANDLING = 2,
WEB_SNIPPET = 4,
DISTRO = 8,
}

export enum StatsbeatInstrumentation {
AZURE_CORE_TRACING = 0,
MONGODB = 1,
MYSQL = 2,
REDIS = 4,
POSTGRES = 8,
NONE = 0,
AZURE_CORE_TRACING = 1,
MONGODB = 2,
MYSQL = 4,
REDIS = 8,
POSTGRES = 16,
}
49 changes: 46 additions & 3 deletions sdk/monitor/monitor-opentelemetry/test/internal/unit/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,19 @@ import {
shutdownAzureMonitor,
} from "../../../src/index";
import { MeterProvider } from "@opentelemetry/sdk-metrics";
import { StatsbeatFeature, StatsbeatInstrumentation } from "../../../src/types";

describe("Main functions", () => {
let originalEnv: NodeJS.ProcessEnv;

beforeEach(() => {
originalEnv = process.env;
});

afterEach(() => {
process.env = originalEnv;
});

after(() => {
trace.disable();
metrics.disable();
Expand Down Expand Up @@ -60,9 +71,41 @@ describe("Main functions", () => {
},
};
useAzureMonitor(config);
assert.strictEqual(
process.env["AZURE_MONITOR_STATSBEAT_FEATURES"],
JSON.stringify({ instrumentation: 15, feature: 4 })
let output = JSON.parse(String(process.env["AZURE_MONITOR_STATSBEAT_FEATURES"]));
const features = Number(output["feature"]);
const instrumentations = Number(output["instrumentation"]);
assert.ok(!(features & StatsbeatFeature.AAD_HANDLING), "AAD_HANDLING is set");
assert.ok(!(features & StatsbeatFeature.DISK_RETRY), "DISK_RETRY is set");
assert.ok(!(features & StatsbeatFeature.WEB_SNIPPET), "WEB_SNIPPET is set");
assert.ok(features & StatsbeatFeature.DISTRO, "DISTRO is not set");
assert.ok(
instrumentations & StatsbeatInstrumentation.AZURE_CORE_TRACING,
"AZURE_CORE_TRACING not set"
);
assert.ok(instrumentations & StatsbeatInstrumentation.MONGODB, "MONGODB not set");
assert.ok(instrumentations & StatsbeatInstrumentation.MYSQL, "MYSQL not set");
assert.ok(instrumentations & StatsbeatInstrumentation.POSTGRES, "POSTGRES not set");
assert.ok(instrumentations & StatsbeatInstrumentation.REDIS, "REDIS not set");
});

it("should use statsbeat features if already available", () => {
const env = <{ [id: string]: string }>{};
let current = 0;
current |= StatsbeatFeature.AAD_HANDLING;
current |= StatsbeatFeature.DISK_RETRY;
env.AZURE_MONITOR_STATSBEAT_FEATURES = current.toString();
process.env = env;
let config: AzureMonitorOpenTelemetryOptions = {
azureMonitorExporterOptions: {
connectionString: "InstrumentationKey=00000000-0000-0000-0000-000000000000",
},
};
useAzureMonitor(config);
let output = JSON.parse(String(process.env["AZURE_MONITOR_STATSBEAT_FEATURES"]));
const numberOutput = Number(output["feature"]);
assert.ok(numberOutput & StatsbeatFeature.AAD_HANDLING, "AAD_HANDLING not set");
assert.ok(numberOutput & StatsbeatFeature.DISK_RETRY, "DISK_RETRY not set");
assert.ok(numberOutput & StatsbeatFeature.DISTRO, "DISTRO not set");
assert.ok(!(numberOutput & StatsbeatFeature.WEB_SNIPPET), "WEB_SNIPPET is set");
});
});

0 comments on commit 47db633

Please sign in to comment.