|
| 1 | +// ignore_for_file: avoid_print, omit_obvious_local_variable_types |
| 2 | + |
| 3 | +import 'dart:async'; |
| 4 | + |
| 5 | +import 'package:observability/observability.dart'; |
| 6 | + |
| 7 | +/// Simple example that wires up custom metric and tracing recorders and emits |
| 8 | +/// a few demo signals. In a production setting these recorders would forward |
| 9 | +/// data to systems such as OpenTelemetry, Prometheus, or Honeycomb. |
| 10 | +Future<void> main() async { |
| 11 | + // Install global registries so framework code can locate the recorders. |
| 12 | + GlobalMetrics.install(MetricsRegistry(recorder: _PrintMetricRecorder())); |
| 13 | + GlobalTracer.install(TracerRegistry(tracer: _PrintTracer())); |
| 14 | + |
| 15 | + final MetricRecorder recorder = GlobalMetrics.instance.recorder; |
| 16 | + |
| 17 | + // Create structured helpers that attach consistent attributes. |
| 18 | + final StructuredCounter requestCounter = StructuredCounter( |
| 19 | + recorder: recorder, |
| 20 | + name: 'example_requests_total', |
| 21 | + description: 'Number of demo requests by method.', |
| 22 | + ); |
| 23 | + |
| 24 | + final StructuredCounter cacheCounter = StructuredCounter( |
| 25 | + recorder: recorder, |
| 26 | + name: 'example_cache_outcome_total', |
| 27 | + description: 'Cache results grouped by outcome.', |
| 28 | + ); |
| 29 | + |
| 30 | + final Histogram latencyHistogram = recorder.histogram( |
| 31 | + 'example_request_latency_ms', |
| 32 | + description: 'Latency for demo request handling in milliseconds.', |
| 33 | + ); |
| 34 | + |
| 35 | + final Tracer tracer = GlobalTracer.instance.tracer; |
| 36 | + |
| 37 | + // Emit observability signals around some fake work. |
| 38 | + await tracer.trace<void>( |
| 39 | + name: 'example.request', |
| 40 | + attributes: {'route': '/hello', 'method': 'GET'}, |
| 41 | + run: (Span span) async { |
| 42 | + requestCounter.increment(attributes: {'method': 'GET'}); |
| 43 | + |
| 44 | + await recordLatency( |
| 45 | + histogram: latencyHistogram, |
| 46 | + attributes: {'route': '/hello'}, |
| 47 | + run: () async { |
| 48 | + span.addEvent('work.started'); |
| 49 | + await Future<void>.delayed(const Duration(milliseconds: 12)); |
| 50 | + span.addEvent('work.finished'); |
| 51 | + }, |
| 52 | + ); |
| 53 | + |
| 54 | + cacheCounter.increment(attributes: {'outcome': 'miss'}); |
| 55 | + |
| 56 | + span.setStatus(SpanStatus.ok); |
| 57 | + }, |
| 58 | + ); |
| 59 | +} |
| 60 | + |
| 61 | +class _PrintMetricRecorder implements MetricRecorder { |
| 62 | + @override |
| 63 | + Counter counter(String name, {String? description}) { |
| 64 | + return _PrintCounter(name, description: description); |
| 65 | + } |
| 66 | + |
| 67 | + @override |
| 68 | + Histogram histogram(String name, {String? description}) { |
| 69 | + return _PrintHistogram(name, description: description); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +class _PrintCounter implements Counter { |
| 74 | + _PrintCounter(this.name, {this.description}); |
| 75 | + |
| 76 | + final String name; |
| 77 | + final String? description; |
| 78 | + |
| 79 | + @override |
| 80 | + void add(num value, {Map<String, String>? attributes}) { |
| 81 | + print( |
| 82 | + 'counter<$name>${description != null ? ' ($description)' : ''}: ' |
| 83 | + 'value=$value attributes=$attributes', |
| 84 | + ); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +class _PrintHistogram implements Histogram { |
| 89 | + _PrintHistogram(this.name, {this.description}); |
| 90 | + |
| 91 | + final String name; |
| 92 | + final String? description; |
| 93 | + |
| 94 | + @override |
| 95 | + void record(num value, {Map<String, String>? attributes}) { |
| 96 | + print( |
| 97 | + 'histogram<$name>${description != null ? ' ($description)' : ''}: ' |
| 98 | + 'value=$value attributes=$attributes', |
| 99 | + ); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +class _PrintTracer extends Tracer { |
| 104 | + @override |
| 105 | + Span startSpan( |
| 106 | + String name, { |
| 107 | + SpanContext? parent, |
| 108 | + Map<String, Object?>? attributes, |
| 109 | + }) { |
| 110 | + print('span<$name> start attributes=$attributes'); |
| 111 | + return _PrintSpan(name, parent: parent); |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +class _PrintSpan implements Span { |
| 116 | + _PrintSpan(this.name, {SpanContext? parent}) : _context = _derive(parent); |
| 117 | + |
| 118 | + final String name; |
| 119 | + final SpanContext _context; |
| 120 | + SpanStatus _status = SpanStatus.unset; |
| 121 | + |
| 122 | + @override |
| 123 | + SpanContext get context => _context; |
| 124 | + |
| 125 | + @override |
| 126 | + void addEvent(String eventName, {Map<String, Object?>? attributes}) { |
| 127 | + print('span<$name> event=$eventName attributes=$attributes'); |
| 128 | + } |
| 129 | + |
| 130 | + @override |
| 131 | + void end({SpanStatus status = SpanStatus.unset}) { |
| 132 | + _status = status == SpanStatus.unset ? _status : status; |
| 133 | + print('span<$name> end status=$_status'); |
| 134 | + } |
| 135 | + |
| 136 | + @override |
| 137 | + void recordError(Object error, StackTrace stackTrace) { |
| 138 | + print('span<$name> error=$error stackTrace=$stackTrace'); |
| 139 | + _status = SpanStatus.error; |
| 140 | + } |
| 141 | + |
| 142 | + @override |
| 143 | + void setAttribute(String key, Object? value) { |
| 144 | + print('span<$name> attr $key=$value'); |
| 145 | + } |
| 146 | + |
| 147 | + @override |
| 148 | + void setStatus(SpanStatus status, {String? description}) { |
| 149 | + _status = status; |
| 150 | + print('span<$name> status=$status description=$description'); |
| 151 | + } |
| 152 | + |
| 153 | + static SpanContext _derive(SpanContext? parent) { |
| 154 | + final String traceId = parent?.traceId ?? 'example-trace'; |
| 155 | + final String spanId = DateTime.now().microsecondsSinceEpoch.toString(); |
| 156 | + return SpanContext(traceId: traceId, spanId: spanId); |
| 157 | + } |
| 158 | +} |
0 commit comments