Skip to content

Commit

Permalink
feat(telemetry): configurable collection time (#9741)
Browse files Browse the repository at this point in the history
Makes metrics collection time configurable, + reduce collection time in
e2e tests with metrics collection enabled
  • Loading branch information
Maddiaa0 authored Nov 7, 2024
1 parent 58e75cd commit 48969ce
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 16 deletions.
3 changes: 3 additions & 0 deletions yarn-project/end-to-end/src/fixtures/with_telemetry_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ export function getEndToEndTestTelemetryConfig(metricsPort?: number, serviceName
telemetryConfig.metricsCollectorUrl = new URL(`http://127.0.0.1:${metricsPort}/v1/metrics`);
telemetryConfig.tracesCollectorUrl = new URL(`http://127.0.0.1:${metricsPort}/v1/traces`);
telemetryConfig.logsCollectorUrl = new URL(`http://127.0.0.1:${metricsPort}/v1/logs`);
// Set faster collection and export times for end-to-end tests
telemetryConfig.otelCollectIntervalMs = 5000;
telemetryConfig.otelExportTimeoutMs = 2500;
}
if (serviceName) {
telemetryConfig.serviceName = serviceName;
Expand Down
2 changes: 2 additions & 0 deletions yarn-project/foundation/src/config/env_var.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ export type EnvVar =
| 'OTEL_EXPORTER_OTLP_TRACES_ENDPOINT'
| 'OTEL_EXPORTER_OTLP_LOGS_ENDPOINT'
| 'OTEL_SERVICE_NAME'
| 'OTEL_COLLECT_INTERVAL_MS'
| 'OTEL_EXPORT_TIMEOUT_MS'
| 'OUTBOX_CONTRACT_ADDRESS'
| 'P2P_BLOCK_CHECK_INTERVAL_MS'
| 'P2P_BLOCK_REQUEST_BATCH_SIZE'
Expand Down
14 changes: 14 additions & 0 deletions yarn-project/telemetry-client/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export interface TelemetryClientConfig {
logsCollectorUrl?: URL;
serviceName: string;
networkName: string;
otelCollectIntervalMs: number;
otelExportTimeoutMs: number;
}

export const telemetryClientConfigMappings: ConfigMappingsType<TelemetryClientConfig> = {
Expand Down Expand Up @@ -34,6 +36,18 @@ export const telemetryClientConfigMappings: ConfigMappingsType<TelemetryClientCo
description: 'The network ID of the telemetry service',
defaultValue: 'local',
},
otelCollectIntervalMs: {
env: 'OTEL_COLLECT_INTERVAL_MS',
description: 'The interval at which to collect metrics',
defaultValue: 60000, // Default extracted from otel client
parseEnv: (val: string) => parseInt(val),
},
otelExportTimeoutMs: {
env: 'OTEL_EXPORT_TIMEOUT_MS',
description: 'The timeout for exporting metrics',
defaultValue: 30000, // Default extracted from otel client
parseEnv: (val: string) => parseInt(val),
},
};

export function getConfigEnvVars(): TelemetryClientConfig {
Expand Down
20 changes: 10 additions & 10 deletions yarn-project/telemetry-client/src/otel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { BatchSpanProcessor, NodeTracerProvider } from '@opentelemetry/sdk-trace
import { SEMRESATTRS_SERVICE_NAME, SEMRESATTRS_SERVICE_VERSION } from '@opentelemetry/semantic-conventions';

import { aztecDetector } from './aztec_resource_detector.js';
import { type TelemetryClientConfig } from './config.js';
import { registerOtelLoggerProvider } from './otelLoggerProvider.js';
import { type Gauge, type TelemetryClient } from './telemetry.js';

Expand Down Expand Up @@ -77,12 +78,7 @@ export class OpenTelemetryClient implements TelemetryClient {
await Promise.all([this.meterProvider.shutdown(), this.loggerProvider.shutdown()]);
}

public static async createAndStart(
metricsCollector: URL,
tracesCollector: URL | undefined,
logsCollector: URL | undefined,
log: DebugLogger,
): Promise<OpenTelemetryClient> {
public static async createAndStart(config: TelemetryClientConfig, log: DebugLogger): Promise<OpenTelemetryClient> {
const resource = detectResourcesSync({
detectors: [
osDetectorSync,
Expand All @@ -104,8 +100,10 @@ export class OpenTelemetryClient implements TelemetryClient {
});

// optionally push traces to an OTEL collector instance
if (tracesCollector) {
tracerProvider.addSpanProcessor(new BatchSpanProcessor(new OTLPTraceExporter({ url: tracesCollector.href })));
if (config.tracesCollectorUrl) {
tracerProvider.addSpanProcessor(
new BatchSpanProcessor(new OTLPTraceExporter({ url: config.tracesCollectorUrl.href })),
);
}

tracerProvider.register();
Expand All @@ -115,12 +113,14 @@ export class OpenTelemetryClient implements TelemetryClient {
readers: [
new PeriodicExportingMetricReader({
exporter: new OTLPMetricExporter({
url: metricsCollector.href,
url: config.metricsCollectorUrl!.href,
}),
exportIntervalMillis: config.otelCollectIntervalMs,
exportTimeoutMillis: config.otelExportTimeoutMs,
}),
],
});
const loggerProvider = registerOtelLoggerProvider(resource, logsCollector);
const loggerProvider = registerOtelLoggerProvider(resource, config.logsCollectorUrl);

const service = new OpenTelemetryClient(resource, meterProvider, tracerProvider, loggerProvider, log);
service.start();
Expand Down
7 changes: 1 addition & 6 deletions yarn-project/telemetry-client/src/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@ export async function createAndStartTelemetryClient(config: TelemetryClientConfi
const log = createDebugLogger('aztec:telemetry-client');
if (config.metricsCollectorUrl) {
log.info('Using OpenTelemetry client');
return await OpenTelemetryClient.createAndStart(
config.metricsCollectorUrl,
config.tracesCollectorUrl,
config.logsCollectorUrl,
log,
);
return await OpenTelemetryClient.createAndStart(config, log);
} else {
log.info('Using NoopTelemetryClient');
return new NoopTelemetryClient();
Expand Down

0 comments on commit 48969ce

Please sign in to comment.