Skip to content

Commit 47db633

Browse files
authored
[Azure Monitor OpenTelemetry ]Fix issue with wrong values for Statsbeat Features and Instrumentations (Azure#27330)
### Packages impacted by this PR @azure/monitor-opentelemetry Fixed issue with features/instrumentations were ignored when set outside of this package
1 parent 677a921 commit 47db633

File tree

3 files changed

+63
-14
lines changed

3 files changed

+63
-14
lines changed

sdk/monitor/monitor-opentelemetry/src/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export function shutdownAzureMonitor() {
5151
}
5252

5353
function _setStatsbeatFeatures(config: InternalConfig) {
54-
let instrumentationBitMap = 0;
54+
let instrumentationBitMap = StatsbeatInstrumentation.NONE;
5555
if (config.instrumentationOptions?.azureSdk?.enabled) {
5656
instrumentationBitMap |= StatsbeatInstrumentation.AZURE_CORE_TRACING;
5757
}
@@ -68,10 +68,14 @@ function _setStatsbeatFeatures(config: InternalConfig) {
6868
instrumentationBitMap |= StatsbeatInstrumentation.REDIS;
6969
}
7070

71-
let featureBitMap = 0;
71+
let featureBitMap = StatsbeatFeature.NONE;
7272
featureBitMap |= StatsbeatFeature.DISTRO;
7373

7474
try {
75+
const currentFeaturesBitMap = Number(process.env[AZURE_MONITOR_STATSBEAT_FEATURES]);
76+
if (!isNaN(currentFeaturesBitMap)) {
77+
featureBitMap |= currentFeaturesBitMap;
78+
}
7579
process.env[AZURE_MONITOR_STATSBEAT_FEATURES] = JSON.stringify({
7680
instrumentation: instrumentationBitMap,
7781
feature: featureBitMap,

sdk/monitor/monitor-opentelemetry/src/types.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@ export const AZURE_MONITOR_OPENTELEMETRY_VERSION = "1.0.0";
55
export const AZURE_MONITOR_STATSBEAT_FEATURES = "AZURE_MONITOR_STATSBEAT_FEATURES";
66

77
export enum StatsbeatFeature {
8-
DISK_RETRY = 0,
9-
AAD_HANDLING = 1,
10-
WEB_SNIPPET = 2,
11-
DISTRO = 4,
8+
NONE = 0,
9+
DISK_RETRY = 1,
10+
AAD_HANDLING = 2,
11+
WEB_SNIPPET = 4,
12+
DISTRO = 8,
1213
}
1314

1415
export enum StatsbeatInstrumentation {
15-
AZURE_CORE_TRACING = 0,
16-
MONGODB = 1,
17-
MYSQL = 2,
18-
REDIS = 4,
19-
POSTGRES = 8,
16+
NONE = 0,
17+
AZURE_CORE_TRACING = 1,
18+
MONGODB = 2,
19+
MYSQL = 4,
20+
REDIS = 8,
21+
POSTGRES = 16,
2022
}

sdk/monitor/monitor-opentelemetry/test/internal/unit/main.test.ts

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,19 @@ import {
1010
shutdownAzureMonitor,
1111
} from "../../../src/index";
1212
import { MeterProvider } from "@opentelemetry/sdk-metrics";
13+
import { StatsbeatFeature, StatsbeatInstrumentation } from "../../../src/types";
1314

1415
describe("Main functions", () => {
16+
let originalEnv: NodeJS.ProcessEnv;
17+
18+
beforeEach(() => {
19+
originalEnv = process.env;
20+
});
21+
22+
afterEach(() => {
23+
process.env = originalEnv;
24+
});
25+
1526
after(() => {
1627
trace.disable();
1728
metrics.disable();
@@ -60,9 +71,41 @@ describe("Main functions", () => {
6071
},
6172
};
6273
useAzureMonitor(config);
63-
assert.strictEqual(
64-
process.env["AZURE_MONITOR_STATSBEAT_FEATURES"],
65-
JSON.stringify({ instrumentation: 15, feature: 4 })
74+
let output = JSON.parse(String(process.env["AZURE_MONITOR_STATSBEAT_FEATURES"]));
75+
const features = Number(output["feature"]);
76+
const instrumentations = Number(output["instrumentation"]);
77+
assert.ok(!(features & StatsbeatFeature.AAD_HANDLING), "AAD_HANDLING is set");
78+
assert.ok(!(features & StatsbeatFeature.DISK_RETRY), "DISK_RETRY is set");
79+
assert.ok(!(features & StatsbeatFeature.WEB_SNIPPET), "WEB_SNIPPET is set");
80+
assert.ok(features & StatsbeatFeature.DISTRO, "DISTRO is not set");
81+
assert.ok(
82+
instrumentations & StatsbeatInstrumentation.AZURE_CORE_TRACING,
83+
"AZURE_CORE_TRACING not set"
6684
);
85+
assert.ok(instrumentations & StatsbeatInstrumentation.MONGODB, "MONGODB not set");
86+
assert.ok(instrumentations & StatsbeatInstrumentation.MYSQL, "MYSQL not set");
87+
assert.ok(instrumentations & StatsbeatInstrumentation.POSTGRES, "POSTGRES not set");
88+
assert.ok(instrumentations & StatsbeatInstrumentation.REDIS, "REDIS not set");
89+
});
90+
91+
it("should use statsbeat features if already available", () => {
92+
const env = <{ [id: string]: string }>{};
93+
let current = 0;
94+
current |= StatsbeatFeature.AAD_HANDLING;
95+
current |= StatsbeatFeature.DISK_RETRY;
96+
env.AZURE_MONITOR_STATSBEAT_FEATURES = current.toString();
97+
process.env = env;
98+
let config: AzureMonitorOpenTelemetryOptions = {
99+
azureMonitorExporterOptions: {
100+
connectionString: "InstrumentationKey=00000000-0000-0000-0000-000000000000",
101+
},
102+
};
103+
useAzureMonitor(config);
104+
let output = JSON.parse(String(process.env["AZURE_MONITOR_STATSBEAT_FEATURES"]));
105+
const numberOutput = Number(output["feature"]);
106+
assert.ok(numberOutput & StatsbeatFeature.AAD_HANDLING, "AAD_HANDLING not set");
107+
assert.ok(numberOutput & StatsbeatFeature.DISK_RETRY, "DISK_RETRY not set");
108+
assert.ok(numberOutput & StatsbeatFeature.DISTRO, "DISTRO not set");
109+
assert.ok(!(numberOutput & StatsbeatFeature.WEB_SNIPPET), "WEB_SNIPPET is set");
67110
});
68111
});

0 commit comments

Comments
 (0)