Skip to content

Commit

Permalink
fix(runtime): create metrics lazily (#979)
Browse files Browse the repository at this point in the history
  • Loading branch information
rnons authored Sep 25, 2024
1 parent 1dbf9e1 commit ed81e91
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 63 deletions.
24 changes: 17 additions & 7 deletions packages/runtime/src/metrics.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { Attributes, Counter, metrics, Gauge } from '@opentelemetry/api'

const meter = metrics.getMeter('processor')
const getMeter = () => metrics.getMeter('processor')

class C {
private counter: Counter<Attributes>
private _counter: Counter<Attributes>
private value: number = 0

constructor(name: string) {
this.counter = meter.createCounter(name)
constructor(private name: string) {}

get counter(): Counter<Attributes> {
if (!this._counter) {
this._counter = getMeter().createCounter(this.name)
}
return this._counter
}

add(value: number, attributes?: Attributes) {
Expand All @@ -21,11 +26,16 @@ class C {
}

class G {
private gauge: Gauge<Attributes>
private _gauge: Gauge<Attributes>
private value: number = 0

constructor(name: string) {
this.gauge = meter.createGauge(name)
constructor(private name: string) {}

get gauge(): Gauge<Attributes> {
if (!this._gauge) {
this._gauge = getMeter().createGauge(this.name)
}
return this._gauge
}

record(value: number, attributes?: Attributes) {
Expand Down
11 changes: 9 additions & 2 deletions packages/runtime/src/otlp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ import { PrometheusExporter } from '@opentelemetry/exporter-prometheus'
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node'
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-grpc'
import { BatchSpanProcessor } from '@opentelemetry/sdk-trace-base'
import { metrics, trace, ProxyTracerProvider } from '@opentelemetry/api'
import { diag, DiagConsoleLogger, DiagLogLevel, metrics, trace, ProxyTracerProvider } from '@opentelemetry/api'

export async function setupOTLP(debug?: boolean) {
if (debug) {
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG)
}

export async function setupOTLP() {
const resource = await envDetector.detect()

const meterProvider = new MeterProvider({
Expand All @@ -29,12 +33,15 @@ export async function setupOTLP() {
const exporter = new OTLPTraceExporter() // new ConsoleSpanExporter();
const processor = new BatchSpanProcessor(exporter)
traceProvider.addSpanProcessor(processor)
traceProvider.register()

metrics.setGlobalMeterProvider(meterProvider)
trace.setGlobalTracerProvider(traceProvider)
;['SIGINT', 'SIGTERM'].forEach((signal) => {
process.on(signal as any, () => shutdownProvider())
})

metrics.getMeter('processor').createGauge('up').record(1)
}

export async function shutdownProvider() {
Expand Down
56 changes: 2 additions & 54 deletions packages/runtime/src/processor-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,7 @@ import { FullProcessorServiceImpl } from './full-service.js'
import { ChainConfig } from './chain-config.js'
import { setupLogger } from './logger.js'

// import { NodeSDK } from '@opentelemetry/sdk-node'
import { envDetector } from '@opentelemetry/resources'
import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-grpc'
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-grpc'
import { PeriodicExportingMetricReader, MeterProvider } from '@opentelemetry/sdk-metrics'
import { BatchSpanProcessor } from '@opentelemetry/sdk-trace-base'
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node'
import { diag, DiagConsoleLogger, DiagLogLevel, metrics, trace } from '@opentelemetry/api'
import { PrometheusExporter } from '@opentelemetry/exporter-prometheus'
import { setupOTLP } from './otlp.js'

// const mergedRegistry = Registry.merge([globalRegistry, niceGrpcRegistry])

Expand Down Expand Up @@ -58,51 +50,7 @@ const logLevel = process.env['LOG_LEVEL']?.toUpperCase()
setupLogger(options['log-format'] === 'json', logLevel === 'debug' ? true : options.debug)
console.debug('Starting with', options.target)

if (options.debug) {
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG)
}

const resource = await envDetector.detect()

const meterProvider = new MeterProvider({
resource,
readers: [
new PeriodicExportingMetricReader({
exporter: new OTLPMetricExporter()
}),
new PrometheusExporter({
// http://localhost:4041/metrics
port: 4041
})
]
})

const traceProvider = new NodeTracerProvider({
resource: resource
})
const exporter = new OTLPTraceExporter() // new ConsoleSpanExporter();
const processor = new BatchSpanProcessor(exporter)
traceProvider.addSpanProcessor(processor)
traceProvider.register()

metrics.setGlobalMeterProvider(meterProvider)
trace.setGlobalTracerProvider(traceProvider)
;['SIGINT', 'SIGTERM'].forEach((signal) => {
process.on(signal as any, () => shutdownProvider())
})

export async function shutdownProvider() {
const traceProvider = trace.getTracerProvider()
if (traceProvider instanceof NodeTracerProvider) {
traceProvider.shutdown().catch(console.error)
}
const meterProvider = metrics.getMeterProvider()
if (meterProvider instanceof MeterProvider) {
meterProvider.shutdown().catch(console.error)
}
}

metrics.getMeter('processor').createGauge('up').record(1)
await setupOTLP(options.debug)

Error.stackTraceLimit = 20

Expand Down

0 comments on commit ed81e91

Please sign in to comment.