Skip to content

Commit 07fe2f3

Browse files
authored
Hide OTEL functionality behind an additional environment variable OTEL_TOGGLE (#1520)
1 parent 5aae586 commit 07fe2f3

File tree

2 files changed

+235
-2
lines changed

2 files changed

+235
-2
lines changed

appmonitoring/ts/src/server.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ import { randomUUID } from 'crypto';
1212
const containerMode = process.env.CONTAINER_MODE;
1313
const clusterArmId = process.env.ARM_ID;
1414
const clusterArmRegion = process.env.ARM_REGION;
15+
16+
// hiding OTEL functionality behind an additional environment variable OTEL_TOGGLE
17+
// this environment variable is controlled by a private preview toggle, once toggle is gone we have to remove this boolean and assume it's true
18+
const otelToggleEnabled: boolean = String(process.env.OTEL_TOGGLE).trim().toLowerCase() === "true";
1519
const otelParams: OtelParams = {
16-
logsEnabled: String(process.env.OTEL_LOGS_ENABLED).trim().toLowerCase() === "true",
17-
metricsEnabled: String(process.env.OTEL_METRICS_ENABLED).trim().toLowerCase() === "true",
20+
logsEnabled: otelToggleEnabled && String(process.env.OTEL_LOGS_ENABLED).trim().toLowerCase() === "true",
21+
metricsEnabled: otelToggleEnabled && String(process.env.OTEL_METRICS_ENABLED).trim().toLowerCase() === "true",
1822
logsPortHttpProtobuf: Number(process.env.OTEL_LOGS_PORT_HTTPPROTOBUF) || 28331,
1923
metricsPortHttpProtobuf: Number(process.env.OTEL_METRICS_PORT_HTTPPROTOBUF) || 28333
2024
};
Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
import { expect, describe, it, beforeEach, afterEach } from "@jest/globals";
2+
import { OtelParams } from "RequestDefinition.js";
3+
4+
// Function to simulate the OTEL parameters construction logic from server.ts
5+
function createOtelParams(): OtelParams {
6+
const otelToggleEnabled = String(process.env.OTEL_TOGGLE).trim().toLowerCase() === "true";
7+
return {
8+
logsEnabled: otelToggleEnabled && String(process.env.OTEL_LOGS_ENABLED).trim().toLowerCase() === "true",
9+
metricsEnabled: otelToggleEnabled && String(process.env.OTEL_METRICS_ENABLED).trim().toLowerCase() === "true",
10+
logsPortHttpProtobuf: Number(process.env.OTEL_LOGS_PORT_HTTPPROTOBUF) || 28331,
11+
metricsPortHttpProtobuf: Number(process.env.OTEL_METRICS_PORT_HTTPPROTOBUF) || 28333
12+
};
13+
}
14+
15+
describe("OTEL Toggle Behavior", () => {
16+
let originalEnv: NodeJS.ProcessEnv;
17+
18+
beforeEach(() => {
19+
// Save original environment
20+
originalEnv = { ...process.env };
21+
// Clear OTEL-related environment variables
22+
delete process.env.OTEL_TOGGLE;
23+
delete process.env.OTEL_LOGS_ENABLED;
24+
delete process.env.OTEL_METRICS_ENABLED;
25+
delete process.env.OTEL_LOGS_PORT_HTTPPROTOBUF;
26+
delete process.env.OTEL_METRICS_PORT_HTTPPROTOBUF;
27+
});
28+
29+
afterEach(() => {
30+
// Restore original environment
31+
process.env = originalEnv;
32+
});
33+
34+
describe("when OTEL_TOGGLE is not set", () => {
35+
it("should disable both logs and metrics even if individual flags are true", () => {
36+
process.env.OTEL_LOGS_ENABLED = "true";
37+
process.env.OTEL_METRICS_ENABLED = "true";
38+
39+
const otelParams = createOtelParams();
40+
41+
expect(otelParams.logsEnabled).toBe(false);
42+
expect(otelParams.metricsEnabled).toBe(false);
43+
});
44+
45+
it("should use default ports when OTEL_TOGGLE is not set", () => {
46+
const otelParams = createOtelParams();
47+
48+
expect(otelParams.logsPortHttpProtobuf).toBe(28331);
49+
expect(otelParams.metricsPortHttpProtobuf).toBe(28333);
50+
});
51+
});
52+
53+
describe("when OTEL_TOGGLE is set to empty string", () => {
54+
it("should disable both logs and metrics", () => {
55+
process.env.OTEL_TOGGLE = "";
56+
process.env.OTEL_LOGS_ENABLED = "true";
57+
process.env.OTEL_METRICS_ENABLED = "true";
58+
59+
const otelParams = createOtelParams();
60+
61+
expect(otelParams.logsEnabled).toBe(false);
62+
expect(otelParams.metricsEnabled).toBe(false);
63+
});
64+
});
65+
66+
describe("when OTEL_TOGGLE is set to false", () => {
67+
it("should disable both logs and metrics", () => {
68+
process.env.OTEL_TOGGLE = "false";
69+
process.env.OTEL_LOGS_ENABLED = "true";
70+
process.env.OTEL_METRICS_ENABLED = "true";
71+
72+
const otelParams = createOtelParams();
73+
74+
expect(otelParams.logsEnabled).toBe(false);
75+
expect(otelParams.metricsEnabled).toBe(false);
76+
});
77+
});
78+
79+
describe("when OTEL_TOGGLE is set to true (lowercase)", () => {
80+
beforeEach(() => {
81+
process.env.OTEL_TOGGLE = "true";
82+
});
83+
84+
it("should enable logs when OTEL_LOGS_ENABLED is true", () => {
85+
process.env.OTEL_LOGS_ENABLED = "true";
86+
process.env.OTEL_METRICS_ENABLED = "false";
87+
88+
const otelParams = createOtelParams();
89+
90+
expect(otelParams.logsEnabled).toBe(true);
91+
expect(otelParams.metricsEnabled).toBe(false);
92+
});
93+
94+
it("should enable metrics when OTEL_METRICS_ENABLED is true", () => {
95+
process.env.OTEL_LOGS_ENABLED = "false";
96+
process.env.OTEL_METRICS_ENABLED = "true";
97+
98+
const otelParams = createOtelParams();
99+
100+
expect(otelParams.logsEnabled).toBe(false);
101+
expect(otelParams.metricsEnabled).toBe(true);
102+
});
103+
104+
it("should enable both when both individual flags are true", () => {
105+
process.env.OTEL_LOGS_ENABLED = "true";
106+
process.env.OTEL_METRICS_ENABLED = "true";
107+
108+
const otelParams = createOtelParams();
109+
110+
expect(otelParams.logsEnabled).toBe(true);
111+
expect(otelParams.metricsEnabled).toBe(true);
112+
});
113+
114+
it("should disable both when both individual flags are false", () => {
115+
process.env.OTEL_LOGS_ENABLED = "false";
116+
process.env.OTEL_METRICS_ENABLED = "false";
117+
118+
const otelParams = createOtelParams();
119+
120+
expect(otelParams.logsEnabled).toBe(false);
121+
expect(otelParams.metricsEnabled).toBe(false);
122+
});
123+
124+
it("should disable both when individual flags are not set", () => {
125+
const otelParams = createOtelParams();
126+
127+
expect(otelParams.logsEnabled).toBe(false);
128+
expect(otelParams.metricsEnabled).toBe(false);
129+
});
130+
});
131+
132+
describe("when OTEL_TOGGLE is set to TRUE (uppercase)", () => {
133+
it("should work case-insensitively", () => {
134+
process.env.OTEL_TOGGLE = "TRUE";
135+
process.env.OTEL_LOGS_ENABLED = "true";
136+
process.env.OTEL_METRICS_ENABLED = "true";
137+
138+
const otelParams = createOtelParams();
139+
140+
expect(otelParams.logsEnabled).toBe(true);
141+
expect(otelParams.metricsEnabled).toBe(true);
142+
});
143+
});
144+
145+
describe("when OTEL_TOGGLE is set to True (mixed case)", () => {
146+
it("should work case-insensitively", () => {
147+
process.env.OTEL_TOGGLE = "True";
148+
process.env.OTEL_LOGS_ENABLED = "TRUE";
149+
process.env.OTEL_METRICS_ENABLED = "True";
150+
151+
const otelParams = createOtelParams();
152+
153+
expect(otelParams.logsEnabled).toBe(true);
154+
expect(otelParams.metricsEnabled).toBe(true);
155+
});
156+
});
157+
158+
describe("when OTEL_TOGGLE has whitespace", () => {
159+
it("should handle leading and trailing whitespace", () => {
160+
process.env.OTEL_TOGGLE = " true ";
161+
process.env.OTEL_LOGS_ENABLED = "true";
162+
process.env.OTEL_METRICS_ENABLED = "true";
163+
164+
const otelParams = createOtelParams();
165+
166+
expect(otelParams.logsEnabled).toBe(true);
167+
expect(otelParams.metricsEnabled).toBe(true);
168+
});
169+
});
170+
171+
describe("port configuration", () => {
172+
beforeEach(() => {
173+
process.env.OTEL_TOGGLE = "true";
174+
});
175+
176+
it("should use custom ports when provided", () => {
177+
process.env.OTEL_LOGS_PORT_HTTPPROTOBUF = "9999";
178+
process.env.OTEL_METRICS_PORT_HTTPPROTOBUF = "8888";
179+
180+
const otelParams = createOtelParams();
181+
182+
expect(otelParams.logsPortHttpProtobuf).toBe(9999);
183+
expect(otelParams.metricsPortHttpProtobuf).toBe(8888);
184+
});
185+
186+
it("should use default ports when custom ports are invalid", () => {
187+
process.env.OTEL_LOGS_PORT_HTTPPROTOBUF = "invalid";
188+
process.env.OTEL_METRICS_PORT_HTTPPROTOBUF = "also-invalid";
189+
190+
const otelParams = createOtelParams();
191+
192+
expect(otelParams.logsPortHttpProtobuf).toBe(28331);
193+
expect(otelParams.metricsPortHttpProtobuf).toBe(28333);
194+
});
195+
196+
it("should handle zero ports correctly", () => {
197+
process.env.OTEL_LOGS_PORT_HTTPPROTOBUF = "0";
198+
process.env.OTEL_METRICS_PORT_HTTPPROTOBUF = "0";
199+
200+
const otelParams = createOtelParams();
201+
202+
expect(otelParams.logsPortHttpProtobuf).toBe(28331);
203+
expect(otelParams.metricsPortHttpProtobuf).toBe(28333);
204+
});
205+
});
206+
207+
describe("edge cases", () => {
208+
it("should handle individual flags with whitespace", () => {
209+
process.env.OTEL_TOGGLE = "true";
210+
process.env.OTEL_LOGS_ENABLED = " TRUE ";
211+
process.env.OTEL_METRICS_ENABLED = " false ";
212+
213+
const otelParams = createOtelParams();
214+
215+
expect(otelParams.logsEnabled).toBe(true);
216+
expect(otelParams.metricsEnabled).toBe(false);
217+
});
218+
219+
it("should handle undefined individual flags when OTEL_TOGGLE is true", () => {
220+
process.env.OTEL_TOGGLE = "true";
221+
// Don't set OTEL_LOGS_ENABLED and OTEL_METRICS_ENABLED
222+
223+
const otelParams = createOtelParams();
224+
225+
expect(otelParams.logsEnabled).toBe(false);
226+
expect(otelParams.metricsEnabled).toBe(false);
227+
});
228+
});
229+
});

0 commit comments

Comments
 (0)