@thotischner/observability-mcp 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/config/loader.d.ts +1 -0
- package/dist/config/loader.js +12 -1
- package/dist/config/loader.test.js +62 -0
- package/package.json +1 -1
package/dist/config/loader.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Config, GeneralSettings, HealthThresholds } from "../types.js";
|
|
2
2
|
export declare const DEFAULT_SETTINGS: GeneralSettings;
|
|
3
3
|
export declare const DEFAULT_HEALTH_THRESHOLDS: HealthThresholds;
|
|
4
|
+
export declare function substituteEnv(raw: string): string;
|
|
4
5
|
export declare function loadConfig(): Config;
|
|
5
6
|
export declare function saveConfig(config: Config): void;
|
package/dist/config/loader.js
CHANGED
|
@@ -23,10 +23,21 @@ export const DEFAULT_HEALTH_THRESHOLDS = {
|
|
|
23
23
|
logErrors: { good: 1, warn: 5, crit: 20 },
|
|
24
24
|
statusBoundaries: { healthy: 80, degraded: 50 },
|
|
25
25
|
};
|
|
26
|
+
export function substituteEnv(raw) {
|
|
27
|
+
return raw.replace(/\$\{([A-Z_][A-Z0-9_]*)(?::-([^}]*))?\}/gi, (_match, name, fallback) => {
|
|
28
|
+
const val = process.env[name];
|
|
29
|
+
if (val !== undefined)
|
|
30
|
+
return val;
|
|
31
|
+
if (fallback !== undefined)
|
|
32
|
+
return fallback;
|
|
33
|
+
console.warn(`[config] env var \${${name}} is undefined`);
|
|
34
|
+
return "";
|
|
35
|
+
});
|
|
36
|
+
}
|
|
26
37
|
export function loadConfig() {
|
|
27
38
|
try {
|
|
28
39
|
const raw = readFileSync(CONFIG_PATH, "utf-8");
|
|
29
|
-
const parsed = yaml.load(raw);
|
|
40
|
+
const parsed = yaml.load(substituteEnv(raw));
|
|
30
41
|
return {
|
|
31
42
|
sources: parsed?.sources || [],
|
|
32
43
|
settings: { ...DEFAULT_SETTINGS, ...parsed?.settings },
|
|
@@ -3,6 +3,7 @@ import assert from "node:assert/strict";
|
|
|
3
3
|
import { writeFileSync, mkdirSync, rmSync, existsSync } from "node:fs";
|
|
4
4
|
import { join } from "node:path";
|
|
5
5
|
import { tmpdir } from "node:os";
|
|
6
|
+
import { substituteEnv } from "./loader.js";
|
|
6
7
|
// We test the helper functions by importing the module fresh with different env vars.
|
|
7
8
|
// Since the config path is resolved at import time, we use dynamic imports.
|
|
8
9
|
const TMP_DIR = join(tmpdir(), "observability-mcp-test-" + Date.now());
|
|
@@ -129,6 +130,67 @@ sources:
|
|
|
129
130
|
assert.equal(config.healthThresholds.statusBoundaries.healthy, 80);
|
|
130
131
|
});
|
|
131
132
|
});
|
|
133
|
+
describe("substituteEnv", () => {
|
|
134
|
+
it("replaces ${VAR} with process.env value", () => {
|
|
135
|
+
process.env.TEST_FOO = "bar";
|
|
136
|
+
assert.equal(substituteEnv('value: "${TEST_FOO}"'), 'value: "bar"');
|
|
137
|
+
delete process.env.TEST_FOO;
|
|
138
|
+
});
|
|
139
|
+
it("uses default with ${VAR:-default} when unset", () => {
|
|
140
|
+
delete process.env.TEST_UNSET;
|
|
141
|
+
assert.equal(substituteEnv('value: "${TEST_UNSET:-fallback}"'), 'value: "fallback"');
|
|
142
|
+
});
|
|
143
|
+
it("prefers env value over default", () => {
|
|
144
|
+
process.env.TEST_SET = "real";
|
|
145
|
+
assert.equal(substituteEnv('value: "${TEST_SET:-fallback}"'), 'value: "real"');
|
|
146
|
+
delete process.env.TEST_SET;
|
|
147
|
+
});
|
|
148
|
+
it("returns empty string for undefined var without default", () => {
|
|
149
|
+
delete process.env.TEST_MISSING;
|
|
150
|
+
const origWarn = console.warn;
|
|
151
|
+
console.warn = () => { };
|
|
152
|
+
try {
|
|
153
|
+
assert.equal(substituteEnv('value: "${TEST_MISSING}"'), 'value: ""');
|
|
154
|
+
}
|
|
155
|
+
finally {
|
|
156
|
+
console.warn = origWarn;
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
it("leaves yaml without placeholders unchanged", () => {
|
|
160
|
+
const yaml = "sources:\n - name: prom\n url: http://localhost:9090\n";
|
|
161
|
+
assert.equal(substituteEnv(yaml), yaml);
|
|
162
|
+
});
|
|
163
|
+
it("handles multiple substitutions in one string", () => {
|
|
164
|
+
process.env.A = "1";
|
|
165
|
+
process.env.B = "2";
|
|
166
|
+
assert.equal(substituteEnv("${A}-${B}-${C:-3}"), "1-2-3");
|
|
167
|
+
delete process.env.A;
|
|
168
|
+
delete process.env.B;
|
|
169
|
+
});
|
|
170
|
+
it("substitutes inside loaded YAML config", async () => {
|
|
171
|
+
process.env.GRAFANA_USER = "12345";
|
|
172
|
+
process.env.GRAFANA_TOKEN = "secret-token";
|
|
173
|
+
const configPath = join(TMP_DIR, "envsubst.yaml");
|
|
174
|
+
writeFileSync(configPath, `
|
|
175
|
+
sources:
|
|
176
|
+
- name: grafana
|
|
177
|
+
type: prometheus
|
|
178
|
+
url: https://grafana.example.com
|
|
179
|
+
enabled: true
|
|
180
|
+
auth:
|
|
181
|
+
type: basic
|
|
182
|
+
username: "\${GRAFANA_USER}"
|
|
183
|
+
password: "\${GRAFANA_TOKEN}"
|
|
184
|
+
`);
|
|
185
|
+
process.env.CONFIG_PATH = configPath;
|
|
186
|
+
const mod = await import("./loader.js?" + Date.now());
|
|
187
|
+
const config = mod.loadConfig();
|
|
188
|
+
assert.equal(config.sources[0].auth?.username, "12345");
|
|
189
|
+
assert.equal(config.sources[0].auth?.password, "secret-token");
|
|
190
|
+
delete process.env.GRAFANA_USER;
|
|
191
|
+
delete process.env.GRAFANA_TOKEN;
|
|
192
|
+
});
|
|
193
|
+
});
|
|
132
194
|
describe("config merging", () => {
|
|
133
195
|
it("merges partial settings with defaults", async () => {
|
|
134
196
|
const configPath = join(TMP_DIR, "partial.yaml");
|
package/package.json
CHANGED