-
Notifications
You must be signed in to change notification settings - Fork 423
Expand file tree
/
Copy pathopampController.ts
More file actions
532 lines (507 loc) · 15.8 KB
/
Copy pathopampController.ts
File metadata and controls
532 lines (507 loc) · 15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
import { Request, Response } from 'express';
import * as config from '@/config';
import { getAllTeams } from '@/controllers/team';
import type { ITeam } from '@/models/team';
import { agentService } from '@/opamp/services/agentService';
import {
createRemoteConfig,
decodeAgentToServer,
encodeServerToAgent,
serverCapabilities,
} from '@/opamp/utils/protobuf';
import {
getCounter,
SpanKind,
SpanStatusCode,
withSpan,
} from '@/utils/instrumentation';
import logger from '@/utils/logger';
// OpAMP messages come from collector agents, not authenticated users, so there
// is no team/user context to attach. We instead track delivery outcomes with a
// low-cardinality `outcome` enum (see agent_docs/observability.md).
const opampMessagesCounter = getCounter('hyperdx.opamp.messages', {
description:
'Count of OpAMP AgentToServer messages handled, labeled by outcome (processed, unsupported_media_type, error).',
});
const opampRemoteConfigsCounter = getCounter('hyperdx.opamp.remote_configs', {
description:
'Count of OpAMP remote collector configs sent back to agents in a ServerToAgent response.',
});
type CollectorConfig = {
extensions: Record<string, any>;
receivers: {
'otlp/hyperdx'?: {
protocols: {
grpc: {
endpoint: string;
include_metadata: boolean;
auth?: {
authenticator: string;
};
};
http: {
endpoint: string;
cors: {
allowed_origins: string[];
allowed_headers: string[];
};
include_metadata: boolean;
auth?: {
authenticator: string;
};
};
};
};
prometheus?: {
config: {
scrape_configs: Array<{
job_name: string;
scrape_interval: string;
static_configs: Array<{
targets: string[];
}>;
}>;
};
};
fluentforward?: {
endpoint: string;
};
nop?: null;
'routing/logs'?: string[];
};
connectors?: {
'routing/logs'?: {
default_pipelines: string[];
error_mode: string;
table: Array<{
context: string;
statement: string;
pipelines: string[];
}>;
};
span_metrics?: {
histogram: { unit: string; explicit: { buckets: string[] } };
dimensions: Array<{ name: string }>;
exemplars: { enabled: boolean };
metrics_flush_interval: string;
namespace?: string;
};
};
exporters?: {
nop?: null;
debug?: {
verbosity: string;
sampling_initial: number;
sampling_thereafter: number;
};
'clickhouse/rrweb'?: {
endpoint: string;
database: string;
username: string;
password: string;
ttl: string;
logs_table_name: string;
timeout: string;
create_schema: string;
json: string;
retry_on_failure: {
enabled: boolean;
initial_interval: string;
max_interval: string;
max_elapsed_time: string;
};
};
clickhouse?: {
endpoint: string;
database: string;
username: string;
password: string;
ttl: string;
timeout: string;
create_schema: string;
json: string;
retry_on_failure: {
enabled: boolean;
initial_interval: string;
max_interval: string;
max_elapsed_time: string;
};
};
prometheusremotewrite?: {
endpoint: string;
tls: {
insecure: boolean;
};
resource_to_telemetry_conversion: {
enabled: boolean;
};
};
'prometheusremotewrite/spanmetrics'?: {
endpoint: string;
tls: {
insecure: boolean;
};
resource_to_telemetry_conversion: {
enabled: boolean;
};
};
};
service: {
extensions: string[];
pipelines: {
[key: string]: {
receivers: string[];
processors?: string[];
exporters: string[];
};
};
};
};
export const buildOtelCollectorConfig = (
teams: Pick<ITeam, 'apiKey' | 'collectorAuthenticationEnforced'>[],
): CollectorConfig => {
const apiKeys = teams.filter(team => team.apiKey).map(team => team.apiKey);
if (config.IS_ALL_IN_ONE_IMAGE || config.IS_LOCAL_APP_MODE || config.IS_DEV) {
// Only allow INGESTION_API_KEY for dev or all-in-one images for security reasons
if (config.INGESTION_API_KEY) {
apiKeys.push(config.INGESTION_API_KEY);
}
}
const collectorAuthenticationEnforced =
teams[0]?.collectorAuthenticationEnforced;
const otelCollectorConfig: CollectorConfig = {
extensions: {},
receivers: {
nop: null,
'otlp/hyperdx': {
protocols: {
grpc: {
endpoint: '0.0.0.0:4317',
include_metadata: true,
},
http: {
endpoint: '0.0.0.0:4318',
cors: {
allowed_origins: ['*'],
allowed_headers: ['*'],
},
include_metadata: true,
},
},
},
prometheus: {
config: {
scrape_configs: [
{
job_name: 'otelcol',
scrape_interval: '30s',
static_configs: [
{
targets: [
'0.0.0.0:8888',
'${env:CLICKHOUSE_PROMETHEUS_METRICS_ENDPOINT}',
],
},
],
},
],
},
},
fluentforward: {
endpoint: '0.0.0.0:24225',
},
},
connectors: {
'routing/logs': {
default_pipelines: ['logs/out-default'],
error_mode: 'ignore',
table: [
{
context: 'log',
statement:
'route() where IsMatch(attributes["rr-web.event"], ".*")',
pipelines: ['logs/out-rrweb'],
},
],
},
},
exporters: {
nop: null,
debug: {
verbosity: 'detailed',
sampling_initial: 5,
sampling_thereafter: 200,
},
'clickhouse/rrweb': {
endpoint: '${env:CLICKHOUSE_ENDPOINT}',
database: '${env:HYPERDX_OTEL_EXPORTER_CLICKHOUSE_DATABASE}',
username: '${env:CLICKHOUSE_USER}',
password: '${env:CLICKHOUSE_PASSWORD}',
ttl: '${env:HYPERDX_OTEL_EXPORTER_TABLES_TTL:-720h}',
logs_table_name: 'hyperdx_sessions',
timeout: '5s',
create_schema:
'${env:HYPERDX_OTEL_EXPORTER_CREATE_LEGACY_SCHEMA:-false}',
json: '${env:HYPERDX_OTEL_EXPORTER_CLICKHOUSE_JSON_ENABLE:-false}',
retry_on_failure: {
enabled: true,
initial_interval: '5s',
max_interval: '30s',
max_elapsed_time: '300s',
},
},
clickhouse: {
endpoint: '${env:CLICKHOUSE_ENDPOINT}',
database: '${env:HYPERDX_OTEL_EXPORTER_CLICKHOUSE_DATABASE}',
username: '${env:CLICKHOUSE_USER}',
password: '${env:CLICKHOUSE_PASSWORD}',
ttl: '${env:HYPERDX_OTEL_EXPORTER_TABLES_TTL:-720h}',
timeout: '5s',
create_schema:
'${env:HYPERDX_OTEL_EXPORTER_CREATE_LEGACY_SCHEMA:-false}',
json: '${env:HYPERDX_OTEL_EXPORTER_CLICKHOUSE_JSON_ENABLE:-false}',
retry_on_failure: {
enabled: true,
initial_interval: '5s',
max_interval: '30s',
max_elapsed_time: '300s',
},
},
},
service: {
extensions: [],
// The pipeline `processors:` lists are intentionally declared in the
// bootstrap config (docker/otel-collector/config.yaml) instead of here,
// so that users can swap them via CUSTOM_OTELCOL_CONFIG_FILE. See
// https://github.com/hyperdxio/hyperdx/pull/2351: when the OpAMP
// remote config sets `processors:` on a pipeline, it overwrites the
// bootstrap+custom merge, which prevents users from substituting
// their own processor (e.g. a memory_limiter with limit_percentage
// instead of limit_mib).
pipelines: {
traces: {
receivers: ['nop'],
exporters: ['clickhouse'],
},
metrics: {
// TODO: prometheus needs to be authenticated
receivers: ['prometheus'],
exporters: ['clickhouse'],
},
'logs/in': {
// TODO: fluentforward needs to be authenticated
receivers: ['fluentforward'],
exporters: ['routing/logs'],
},
'logs/out-default': {
receivers: ['routing/logs'],
exporters: ['clickhouse'],
},
'logs/out-rrweb': {
receivers: ['routing/logs'],
exporters: ['clickhouse/rrweb'],
},
},
},
};
if (apiKeys && apiKeys.length > 0) {
// attach otlp/hyperdx receiver
otelCollectorConfig.service.pipelines.traces.receivers.push('otlp/hyperdx');
otelCollectorConfig.service.pipelines.metrics.receivers.push(
'otlp/hyperdx',
);
otelCollectorConfig.service.pipelines['logs/in'].receivers.push(
'otlp/hyperdx',
);
if (config.IS_PROMQL_ENABLED && otelCollectorConfig.exporters) {
otelCollectorConfig.exporters.prometheusremotewrite = {
endpoint: 'http://${env:CLICKHOUSE_PROMETHEUS_METRICS_ENDPOINT}/write',
tls: {
insecure: true,
},
resource_to_telemetry_conversion: {
enabled: true,
},
};
otelCollectorConfig.service.pipelines['metrics/promql'] = {
receivers: ['otlp/hyperdx'],
processors: ['memory_limiter', 'batch'],
exporters: ['prometheusremotewrite'],
};
}
if (
config.IS_SPAN_METRICS_ENABLED &&
otelCollectorConfig.connectors &&
otelCollectorConfig.exporters
) {
// Derive request metrics (with trace exemplars) from spans. The connector
// consumes the traces pipeline and feeds a dedicated metrics pipeline, so
// the resulting `traces.span.metrics.*` land in ClickHouse with
// `Exemplars.*` pointing back at the spans they were measured from.
otelCollectorConfig.connectors.span_metrics = {
histogram: {
unit: 'ms',
explicit: {
buckets: [
'2ms',
'5ms',
'10ms',
'25ms',
'50ms',
'100ms',
'250ms',
'500ms',
'1s',
'2.5s',
'5s',
'10s',
],
},
},
dimensions: [
{ name: 'http.route' },
{ name: 'http.method' },
{ name: 'host.region' },
{ name: 'app.tenant_id' },
{ name: 'http.status_code' },
],
exemplars: { enabled: true },
metrics_flush_interval: '15s',
};
otelCollectorConfig.service.pipelines.traces.exporters.push(
'span_metrics',
);
const spanMetricsExporters = ['clickhouse'];
// Optionally also remote-write the derived metrics (with exemplars) to a
// Prometheus endpoint, so the native Prometheus `query_exemplars` path can
// be exercised against the same real, generated data.
if (config.IS_SPAN_METRICS_PROM_RW_ENABLED) {
otelCollectorConfig.exporters['prometheusremotewrite/spanmetrics'] = {
// Guaranteed set by IS_SPAN_METRICS_PROM_RW_ENABLED above.
endpoint: config.SPAN_METRICS_PROM_RW_ENDPOINT!,
tls: { insecure: true },
resource_to_telemetry_conversion: { enabled: true },
};
spanMetricsExporters.push('prometheusremotewrite/spanmetrics');
}
otelCollectorConfig.service.pipelines['metrics/spanmetrics'] = {
receivers: ['span_metrics'],
processors: ['memory_limiter', 'batch'],
exporters: spanMetricsExporters,
};
}
if (collectorAuthenticationEnforced) {
if (otelCollectorConfig.receivers['otlp/hyperdx'] == null) {
// should never happen
throw new Error('otlp/hyperdx receiver not found');
}
otelCollectorConfig.extensions['bearertokenauth/hyperdx'] = {
scheme: '',
tokens: apiKeys,
};
otelCollectorConfig.receivers['otlp/hyperdx'].protocols.grpc.auth = {
authenticator: 'bearertokenauth/hyperdx',
};
otelCollectorConfig.receivers['otlp/hyperdx'].protocols.http.auth = {
authenticator: 'bearertokenauth/hyperdx',
};
otelCollectorConfig.service.extensions = ['bearertokenauth/hyperdx'];
}
}
return otelCollectorConfig;
};
export class OpampController {
/**
* Handle an OpAMP message from an agent
*/
public async handleOpampMessage(req: Request, res: Response): Promise<void> {
return withSpan(
'opamp.handle_message',
async span => {
try {
// Check content type
const contentType = req.get('Content-Type');
if (contentType !== 'application/x-protobuf') {
opampMessagesCounter.add(1, { outcome: 'unsupported_media_type' });
span.setStatus({ code: SpanStatusCode.OK });
res
.status(415)
.send(
'Unsupported Media Type: Content-Type must be application/x-protobuf',
);
return;
}
// Decode the AgentToServer message
const agentToServer = decodeAgentToServer(req.body);
logger.debug({ agentToServer }, 'agentToServer');
logger.debug(
// @ts-ignore
`Received message from agent: ${agentToServer.instanceUid?.toString(
'hex',
)}`,
);
// Process the agent status
const agent = agentService.processAgentStatus(agentToServer);
// Prepare the response
const serverToAgent: any = {
instanceUid: agent.instanceUid,
capabilities: serverCapabilities,
};
const acceptsRemoteConfig =
agentService.agentAcceptsRemoteConfig(agent);
span.setAttribute(
'opamp.agent.accepts_remote_config',
acceptsRemoteConfig,
);
// Check if we should send a remote configuration
if (acceptsRemoteConfig) {
const teams = await getAllTeams([
'apiKey',
'collectorAuthenticationEnforced',
]);
const otelCollectorConfig = buildOtelCollectorConfig(teams);
if (config.IS_DEV) {
logger.debug(JSON.stringify(otelCollectorConfig, null, 2));
}
const remoteConfig = createRemoteConfig(
new Map([
[
'config.json',
Buffer.from(JSON.stringify(otelCollectorConfig)),
],
]),
'application/json',
);
serverToAgent.remoteConfig = remoteConfig;
opampRemoteConfigsCounter.add(1);
logger.debug(
`Sending remote config to agent: ${agent.instanceUid.toString(
'hex',
)}`,
);
}
// Encode and send the response
const encodedResponse = encodeServerToAgent(serverToAgent);
opampMessagesCounter.add(1, { outcome: 'processed' });
span.setStatus({ code: SpanStatusCode.OK });
res.setHeader('Content-Type', 'application/x-protobuf');
res.send(encodedResponse);
} catch (error) {
opampMessagesCounter.add(1, { outcome: 'error' });
span.recordException(
error instanceof Error ? error : new Error(String(error)),
);
span.setStatus({
code: SpanStatusCode.ERROR,
message: error instanceof Error ? error.message : String(error),
});
logger.error({ err: error }, 'Error handling OpAMP message');
res.status(500).send('Internal Server Error');
}
},
{ kind: SpanKind.INTERNAL, recordOkStatus: false },
);
}
}
// Create a singleton instance
export const opampController = new OpampController();