@thotischner/observability-mcp 1.1.1 → 1.1.2
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/connectors/loki.d.ts +4 -0
- package/dist/connectors/loki.js +58 -11
- package/package.json +1 -1
|
@@ -7,6 +7,8 @@ export declare class LokiConnector implements ObservabilityConnector {
|
|
|
7
7
|
private baseUrl;
|
|
8
8
|
private auth?;
|
|
9
9
|
private tlsAgent?;
|
|
10
|
+
private serviceLabels;
|
|
11
|
+
private labelValuesCache;
|
|
10
12
|
connect(config: SourceConfig): Promise<void>;
|
|
11
13
|
getDefaultMetrics(): MetricDefinition[];
|
|
12
14
|
getMetrics(): MetricDefinition[];
|
|
@@ -15,6 +17,8 @@ export declare class LokiConnector implements ObservabilityConnector {
|
|
|
15
17
|
disconnect(): Promise<void>;
|
|
16
18
|
listServices(): Promise<ServiceInfo[]>;
|
|
17
19
|
queryLogs(params: LogQuery): Promise<LogResult>;
|
|
20
|
+
private getLabelValues;
|
|
21
|
+
private resolveServiceLabel;
|
|
18
22
|
private parseLine;
|
|
19
23
|
private extractTopPatterns;
|
|
20
24
|
private parseTimeRange;
|
package/dist/connectors/loki.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { buildTlsAgent } from "./tls.js";
|
|
2
|
+
const DEFAULT_SERVICE_LABELS = ["service_name", "service", "job", "app", "container"];
|
|
3
|
+
const LABEL_CACHE_TTL_MS = 60_000;
|
|
2
4
|
export class LokiConnector {
|
|
3
5
|
type = "loki";
|
|
4
6
|
signalType = "logs";
|
|
@@ -6,11 +8,17 @@ export class LokiConnector {
|
|
|
6
8
|
baseUrl = "";
|
|
7
9
|
auth;
|
|
8
10
|
tlsAgent;
|
|
11
|
+
serviceLabels = DEFAULT_SERVICE_LABELS;
|
|
12
|
+
labelValuesCache = new Map();
|
|
9
13
|
async connect(config) {
|
|
10
14
|
this.name = config.name;
|
|
11
15
|
this.baseUrl = config.url.replace(/\/$/, "");
|
|
12
16
|
this.auth = config.auth;
|
|
13
17
|
this.tlsAgent = buildTlsAgent(config);
|
|
18
|
+
const envLabels = process.env.LOKI_SERVICE_LABELS;
|
|
19
|
+
if (envLabels) {
|
|
20
|
+
this.serviceLabels = envLabels.split(",").map((s) => s.trim()).filter(Boolean);
|
|
21
|
+
}
|
|
14
22
|
}
|
|
15
23
|
getDefaultMetrics() {
|
|
16
24
|
// Loki is a log backend — no metric definitions by default
|
|
@@ -46,23 +54,35 @@ export class LokiConnector {
|
|
|
46
54
|
}
|
|
47
55
|
async disconnect() { }
|
|
48
56
|
async listServices() {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
57
|
+
// Probe each candidate label and merge values. Loki streams may identify
|
|
58
|
+
// services via service_name, service, job, app, or container depending on
|
|
59
|
+
// the shipper configuration. Walking all candidates ensures historical
|
|
60
|
+
// streams remain reachable when label conventions change over time.
|
|
61
|
+
const seen = new Map();
|
|
62
|
+
for (const label of this.serviceLabels) {
|
|
63
|
+
const values = await this.getLabelValues(label);
|
|
64
|
+
for (const name of values) {
|
|
65
|
+
if (!seen.has(name)) {
|
|
66
|
+
seen.set(name, {
|
|
67
|
+
name,
|
|
68
|
+
source: this.name,
|
|
69
|
+
signalType: "logs",
|
|
70
|
+
labels: { discoveredVia: label },
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
}
|
|
59
74
|
}
|
|
75
|
+
return Array.from(seen.values());
|
|
60
76
|
}
|
|
61
77
|
async queryLogs(params) {
|
|
62
78
|
const { start, end } = this.parseTimeRange(params.duration);
|
|
63
79
|
const limit = Math.min(Math.max(params.limit || 100, 1), 1000);
|
|
80
|
+
// Resolve which label this service identifier lives under. Falls back to
|
|
81
|
+
// the first configured label when no exact match is found, preserving
|
|
82
|
+
// legacy behavior for callers passing labels that aren't in the cache yet.
|
|
83
|
+
const matchedLabel = await this.resolveServiceLabel(params.service);
|
|
64
84
|
const service = this.escapeLogQLValue(params.service);
|
|
65
|
-
let logql = `{
|
|
85
|
+
let logql = `{${matchedLabel}="${service}"}`;
|
|
66
86
|
if (params.level) {
|
|
67
87
|
const level = this.escapeLogQLValue(params.level);
|
|
68
88
|
logql += ` | json | level="${level}"`;
|
|
@@ -109,6 +129,33 @@ export class LokiConnector {
|
|
|
109
129
|
};
|
|
110
130
|
}
|
|
111
131
|
// --- Private helpers ---
|
|
132
|
+
async getLabelValues(label) {
|
|
133
|
+
const cached = this.labelValuesCache.get(label);
|
|
134
|
+
if (cached && cached.expiresAt > Date.now()) {
|
|
135
|
+
return cached.values;
|
|
136
|
+
}
|
|
137
|
+
try {
|
|
138
|
+
const data = await this.apiGet(`/loki/api/v1/label/${encodeURIComponent(label)}/values`);
|
|
139
|
+
const values = data?.data || [];
|
|
140
|
+
this.labelValuesCache.set(label, {
|
|
141
|
+
values,
|
|
142
|
+
expiresAt: Date.now() + LABEL_CACHE_TTL_MS,
|
|
143
|
+
});
|
|
144
|
+
return values;
|
|
145
|
+
}
|
|
146
|
+
catch {
|
|
147
|
+
this.labelValuesCache.set(label, { values: [], expiresAt: Date.now() + LABEL_CACHE_TTL_MS });
|
|
148
|
+
return [];
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
async resolveServiceLabel(service) {
|
|
152
|
+
for (const label of this.serviceLabels) {
|
|
153
|
+
const values = await this.getLabelValues(label);
|
|
154
|
+
if (values.includes(service))
|
|
155
|
+
return label;
|
|
156
|
+
}
|
|
157
|
+
return this.serviceLabels[0] || "service_name";
|
|
158
|
+
}
|
|
112
159
|
parseLine(line) {
|
|
113
160
|
try {
|
|
114
161
|
return JSON.parse(line);
|
package/package.json
CHANGED