A TypeScript package for integrating metrics and tracing into your applications, leveraging OpenTelemetry for standardized observability.
The @brokerize/telemetry
package provides tools to monitor application performance and behavior through metrics (for numerical data like counters, gauges, histograms, and summaries) and tracing (for tracking request flows across distributed systems). It simplifies instrumentation with annotation-based and manual approaches, making it easy to integrate into existing TypeScript projects.
Install the package via npm:
npm install @brokerize/telemetry
Before using metrics or tracing, initialize the OpenTelemetry instrumentation with the initInstrumentation
function. This sets up the exporter for sending telemetry data to an OpenTelemetry collector.
import { initInstrumentation } from '@brokerize/telemetry';
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
initInstrumentation({
serviceName: 'my-service',
url: 'http://your-otel-collector:4318/v1/traces',
localDebugging: false,
instrumentations: [new HttpInstrumentation()]
});
Note: Call
initInstrumentation
at the start of your application, e.g., in aninstrumentation.ts
file. See Initializing Instrumentation for more details.]
The Metrics
class and metrics
wrapper allow you to create and manage metrics such as counters, gauges, histograms, and summaries. Use annotations for automatic metric creation or manual methods for fine-grained control.
import { Metrics } from '@brokerize/telemetry';
class ExampleService {
@Metrics.counter({
metricName: 'http_requests_total',
help: 'Total number of HTTP requests',
labels: { method: 'GET' }
})
handleRequest() {
// Implementation
}
}
import { metrics, MetricType } from '@brokerize/telemetry';
metrics.createMetric(MetricType.Counter, {
name: 'http_requests_total',
help: 'Total number of HTTP requests',
labelNames: ['method', 'status']
});
metrics.incrementCounter('http_requests_total', { method: 'GET', status: '200' });
For detailed usage, see Metrics.
The Traces
class enables tracing of operations using OpenTelemetry spans. Use the @Trace
annotation for automatic span creation or manual methods for custom tracing logic.
import { Traces } from '@brokerize/telemetry';
class ExampleService {
@Traces.trace({
spanName: 'fetch-data',
attributes: { endpoint: '/api/data' }
})
async fetchData() {
// Implementation
return { status: 'success' };
}
}
import { Traces, SpanStatusCode } from '@brokerize/telemetry';
async function processRequest() {
const { span, createdSpan } = Traces.getCurrentSpanOrCreateNew('process-request', {
attributes: { operation: 'process' }
});
try {
// Implementation
Traces.setStatus({ code: SpanStatusCode.OK });
} catch (error) {
Traces.setStatus({ code: SpanStatusCode.ERROR, message: error.message });
throw error;
} finally {
if (createdSpan) span.end();
}
}
For detailed usage, see Tracing.
- Metrics: Learn how to define and use metrics in Metrics.md.
- Tracing: Understand tracing and span management in Tracing.md.