testing library
Test helpers for the Dartastic OpenTelemetry SDK.
Import this library from test/ only:
import 'package:dartastic_opentelemetry/testing.dart';
It is intentionally not exported from the main
package:dartastic_opentelemetry/dartastic_opentelemetry.dart
barrel so production bundles don't carry the in-memory exporter
classes.
Provides:
- InMemorySpanExporter — collects exported spans into a list with
query helpers (
findSpanByName,findSpansByName,findSpansStartingWith,clear). - InMemoryLogExporter — same idea for log records.
- InMemoryMetricExporter — same idea for metrics; pair with OnDemandMetricReader.
- OnDemandMetricReader — a metric reader that never fires on a
timer; tests drive
collect()explicitly via TestHarness.collectMetrics. - TestHarness — bundles the three exporters and the reader,
plus a
clear()to reset between tests. - maybeInitializeOtelForTest — singleton initializer designed
for
setUpAll. Brings up the real OpenTelemetry SDK pointed at the in-memory exporters. Safe to call from multiple test files in the same process; returns the same harness on subsequent calls.
The shape mirrors the test harness used in the OTel-Dart reference demo at https://github.com/MindfulSoftwareLLC/dart-otel-reference-demo so wrappers, customer apps, and the reference demo all use the same scaffolding.
Classes
- InMemoryLogExporter
- Log-record exporter that buffers records in memory for inspection.
- InMemoryMetricExporter
- Metric exporter that buffers exported metric snapshots in memory.
- InMemorySpanExporter
- Span exporter that buffers spans in memory for inspection.
- OnDemandMetricReader
-
Metric reader for tests. Never fires on a timer; tests drive
collect()explicitly via TestHarness.collectMetrics so assertions don't race against a periodic export. - TestHarness
-
Bundles the three in-memory exporters and the on-demand metric
reader so tests have one handle to
clear()between cases.
Functions
-
maybeInitializeOtelForTest(
{String serviceName = 'otel-test', String endpoint = 'http://localhost:4317'}) → Future< TestHarness> - Initializes the OpenTelemetry SDK once per test process with in-memory exporters for spans / logs / metrics, and returns the same TestHarness on subsequent calls.