What is @opencensus/core?
The @opencensus/core package is a set of libraries for collecting, processing, and exporting telemetry data (metrics and traces) for analysis to improve the performance and reliability of applications. It is part of the OpenCensus project, which aims to provide a single, high-quality telemetry collection framework across multiple languages.
What are @opencensus/core's main functionalities?
Tracing
This feature allows the collection and export of trace data, which helps in understanding the flow of requests through various services and in identifying bottlenecks and latency issues.
const { Tracing } = require('@opencensus/core');
const tracing = Tracing.instance;
// Configure tracing
tracing.start({samplingRate: 1});
// Create a custom span
const rootSpan = tracing.tracer.startRootSpan({name: 'main'}, rootSpan => {
// Do work within the span
rootSpan.end(); // End the span
});
Metrics
This feature enables the collection and aggregation of metrics data, such as counts or latencies, which can be used for monitoring application performance and health.
const { Metrics, MeasureUnit } = require('@opencensus/core');
const metrics = Metrics.instance;
// Create a measure
const requestCountMeasure = metrics.createMeasureInt64('request_count', MeasureUnit.UNIT, 'Count of requests received');
// Create and register a view to aggregate the data
metrics.createView('request_count_view', requestCountMeasure, 'count', [], 'The count of requests', []);
// Record data
metrics.record([{measure: requestCountMeasure, value: 1}]);
Other packages similar to @opencensus/core
opentelemetry-api
OpenTelemetry is the successor to OpenCensus and provides a single set of APIs, libraries, agents, and instrumentation to capture distributed traces and metrics from your application. It aims to make telemetry data (metrics, logs, and traces) a built-in feature of cloud-native software applications. Compared to @opencensus/core, OpenTelemetry offers broader community support and more active development, with a focus on compatibility and integration with a wide range of observability tools.
prom-client
The prom-client package is specifically designed for creating metrics in the Prometheus format. It focuses solely on metrics collection and exposition, unlike @opencensus/core, which supports both tracing and metrics. This makes prom-client a good choice if you are specifically looking for Prometheus support, but it lacks the tracing capabilities of @opencensus/core.
zipkin-js
Zipkin-js is a library for sending traces to Zipkin, a distributed tracing system. It focuses on tracing support and integrates with various JavaScript frameworks and libraries. Compared to @opencensus/core, zipkin-js is more specialized in tracing and does not include metrics collection functionality. It's a good option if your primary need is distributed tracing with Zipkin.