This repository is the Dart implementation of the OpenTelemetry project. All contributions and designs should follow the OpenTelemetry specification.
Signal | Status |
---|---|
Traces | Beta |
Metrics | Alpha |
Logs | Unimplemented |
This section will show you how to initialize the OpenTelemetry SDK, capture a span, and propagate context.
import 'package:opentelemetry/sdk.dart'
show
BatchSpanProcessor,
CollectorExporter,
ConsoleExporter,
SimpleSpanProcessor,
TracerProviderBase;
import 'package:opentelemetry/api.dart'
show registerGlobalTracerProvider, globalTracerProvider;
void main(List<String> args) {
final tracerProvider = TracerProviderBase(processors: [
BatchSpanProcessor(
CollectorExporter(Uri.parse('https://my-collector.com/v1/traces'))),
SimpleSpanProcessor(ConsoleExporter())
]);
registerGlobalTracerProvider(tracerProvider);
final tracer = globalTracerProvider.getTracer('instrumentation-name');
}
import 'package:opentelemetry/api.dart' show StatusCode, globalTracerProvider;
void main(List<String> args) {
final tracer = globalTracerProvider.getTracer('instrumentation-name');
final span = tracer.startSpan('main');
try {
// do some work
span.addEvent('some work');
} catch (e, s) {
span
..setStatus(StatusCode.error, e.toString())
..recordException(e, stackTrace: s);
rethrow;
} finally {
span.end();
}
}
In order to parent spans, context must be propagated. Propagation can be achieved by manually passing an instance of Context
or by using Dart Zones
.
See the noop context manager example and zone context manager example for more information.
In order to parent spans between processes, context can be serialized and deserialized using a TextMapPropagator
, TextMapSetter
, and TextMapGetter
.
See the W3C context propagation example for more information.
A tracer provider can register a web-specific time provider that uses the browser's performance API instead of DateTime when recording timestamps for a span's start timestamp, end timestamp, and span events.
import 'package:opentelemetry/web_sdk.dart' as web_sdk;
final tracerProvider =
web_sdk.WebTracerProvider(timeProvider: web_sdk.WebTimeProvider());
Important Note: Span timestamps may be inaccurate if the executing system is suspended for sleep. See open-telemetry/opentelemetry-js#852 for more information.
In order to generate protobuf definitions, you must have protoc installed and available in your path.
Only Workiva maintainers can publish new versions of opentelemetry-dart. See Publishing opentelemetry-dart