Skip to content

Commit c15644d

Browse files
mahendrabishnoi2pellareddmathieu
authored
stdoutmetric exporter observability (#7492)
Fixes #7014 This PR adds support for below self observability metrics for stdoutmetric exporter - otel.sdk.exporter.metric_data_point.inflight - otel.sdk.exporter.metric_data_point.exported - otel.sdk.exporter.operation.duration These metrics are experimental and hence behind a feature flag OTEL_GO_X_OBSERVABILITY. Definition of above metrics is available at https://github.com/open-telemetry/semantic-conventions/blob/v1.36.0/docs/otel/sdk-metrics.md <details> <summary>Observability Implementation Checklist</summary> ## Observability Implementation Checklist Based on the [project Observability guidelines](https://github.com/open-telemetry/opentelemetry-go/blob/e4ab3141123d0811125a69823dbbe4d9ec5a9b8f/CONTRIBUTING.md#observability), ensure the following are completed: ### Environment Variable Activation * [x] Observability features are disabled by default * [x] Features are activated through the `OTEL_GO_X_OBSERVABILITY` environment variable * [x] Use consistent pattern with `x.Observability.Enabled()` check [^1] * [x] Follow established experimental feature pattern [^2][^3] [^1]: https://github.com/open-telemetry/opentelemetry-go/blob/e4ab3141123d0811125a69823dbbe4d9ec5a9b8f/exporters/stdout/stdouttrace/internal/observ/instrumentation.go#L101-L103 [^2]: https://github.com/open-telemetry/opentelemetry-go/blob/e4ab3141123d0811125a69823dbbe4d9ec5a9b8f/exporters/stdout/stdouttrace/internal/x/x.go [^3]: https://github.com/open-telemetry/opentelemetry-go/blob/e4ab3141123d0811125a69823dbbe4d9ec5a9b8f/sdk/internal/x/x.go ### Encapsulation * [x] Instrumentation is encapsulated within a dedicated `struct` (e.g., [`Instrumentation`](https://github.com/open-telemetry/opentelemetry-go/blob/e4ab3141123d0811125a69823dbbe4d9ec5a9b8f/exporters/stdout/stdouttrace/internal/observ/instrumentation.go#L86-L94)) * [x] Instrumentation is not mixed into the instrumented component * [x] Instrumentation code is in its own file or package if complex/reused * [x] Instrumentation setup doesn't bloat the main component code ### Initialization * [x] Initialization is only done when observability is enabled * [x] Setup is explicit and side-effect free * [x] Return errors from initialization when appropriate * [x] Use the global Meter provider (e.g., `otel.GetMeterProvider()`) * [x] Include proper meter configuration with: * [x] Instrumentation package name is used for the Meter * [x] Instrumentation version (e.g. [`Version`](https://github.com/open-telemetry/opentelemetry-go/blob/e4ab3141123d0811125a69823dbbe4d9ec5a9b8f/exporters/stdout/stdouttrace/internal/observ/instrumentation.go#L40-L43)) * [x] Schema URL (e.g. [`SchemaURL`](https://github.com/open-telemetry/opentelemetry-go/blob/e4ab3141123d0811125a69823dbbe4d9ec5a9b8f/exporters/stdout/stdouttrace/internal/observ/instrumentation.go#L36-L38)) ### Performance * [x] Little to no overhead when observability is disabled * [x] Expensive operations are only executed when observability is enabled * [x] When enabled, instrumentation code paths are optimized to reduce allocation/computation overhead #### Attribute and Option Allocation Management * [x] Use `sync.Pool` for attribute slices and options with dynamic attributes * [x] Pool objects are properly reset before returning to pool * [x] Pools are scoped for maximum efficiency while ensuring correctness #### Caching * [x] Static attribute sets known at compile time are pre-computed and cached * [x] Common attribute combinations use lookup tables/maps #### Benchmarking * [x] Benchmarks provided for all instrumentation code * [ ] Benchmark scenarios include both enabled and disabled observability * [x] Benchmark results show impact on allocs/op, B/op, and ns/op (use `b.ReportAllocs()` in benchmarks) ### Error Handling and Robustness * [x] Errors are reported back to caller when possible * [x] Partial failures are handled gracefully * [x] Use partially initialized components when available * [x] Return errors to caller instead of only using `otel.Handle()` * [x] Use `otel.Handle()` only when component cannot report error to user ### Context Propagation * [x] Observability measurements receive the context from the function being measured (don't break context propagation by using `context.Background()`) ### Semantic Conventions Compliance * [x] All metrics follow [OpenTelemetry Semantic Conventions](https://github.com/open-telemetry/semantic-conventions/blob/5ee549b1ce30fe11fcb9b7e3bd35ebfb363f467f/docs/otel/sdk-metrics.md) * [x] Use the [`otelconv`](https://pkg.go.dev/go.opentelemetry.io/[email protected]/semconv/v1.37.0/otelconv) convenience package for metric semantic conventions * [x] Component names follow semantic conventions * [x] Use package path scope type as stable identifier for non-standard components * [x] Component names are stable unique identifiers * [x] Use global counter for uniqueness if necessary * [x] Component ID counter is resettable for deterministic testing ### Testing * [x] Use deterministic testing with isolated state * [x] Restore previous state after tests (`t.Cleanup()`) * [x] Isolate meter provider for testing * [x] Use `t.Setenv()` for environment variable testing * [x] Reset component ID counter for deterministic component names * [x] Test order doesn't affect results </details> --------- Co-authored-by: Robert Pająk <[email protected]> Co-authored-by: Damien Mathieu <[email protected]>
1 parent 483fc35 commit c15644d

File tree

16 files changed

+1135
-4
lines changed

16 files changed

+1135
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1313
- `Exporter` in `go.opentelemetry.io/otel/exporter/prometheus` ignores metrics with the scope `go.opentelemetry.io/contrib/bridges/prometheus`.
1414
This prevents scrape failures when the Prometheus exporter is misconfigured to get data from the Prometheus bridge. (#7688)
1515
- Improve performance of concurrent histogram measurements in `go.opentelemetry.io/otel/sdk/metric`. (#7474)
16+
- Add experimental observability metrics in `go.opentelemetry.io/otel/exporters/stdout/stdoutmetric`. (#7492)
1617

1718
<!-- Released section -->
1819
<!-- Don't change this section unless doing release -->

exporters/stdout/stdoutmetric/exporter.go

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"sync"
1111
"sync/atomic"
1212

13+
"go.opentelemetry.io/otel/exporters/stdout/stdoutmetric/internal/counter"
14+
"go.opentelemetry.io/otel/exporters/stdout/stdoutmetric/internal/observ"
1315
"go.opentelemetry.io/otel/internal/global"
1416
"go.opentelemetry.io/otel/sdk/metric"
1517
"go.opentelemetry.io/otel/sdk/metric/metricdata"
@@ -25,6 +27,8 @@ type exporter struct {
2527
aggregationSelector metric.AggregationSelector
2628

2729
redactTimestamps bool
30+
31+
inst *observ.Instrumentation
2832
}
2933

3034
// New returns a configured metric exporter.
@@ -39,7 +43,9 @@ func New(options ...Option) (metric.Exporter, error) {
3943
redactTimestamps: cfg.redactTimestamps,
4044
}
4145
exp.encVal.Store(*cfg.encoder)
42-
return exp, nil
46+
var err error
47+
exp.inst, err = observ.NewInstrumentation(counter.NextExporterID())
48+
return exp, err
4349
}
4450

4551
func (e *exporter) Temporality(k metric.InstrumentKind) metricdata.Temporality {
@@ -50,8 +56,13 @@ func (e *exporter) Aggregation(k metric.InstrumentKind) metric.Aggregation {
5056
return e.aggregationSelector(k)
5157
}
5258

53-
func (e *exporter) Export(ctx context.Context, data *metricdata.ResourceMetrics) error {
54-
if err := ctx.Err(); err != nil {
59+
func (e *exporter) Export(ctx context.Context, data *metricdata.ResourceMetrics) (err error) {
60+
if e.inst != nil {
61+
op := e.inst.ExportMetrics(ctx, countDataPoints(data))
62+
defer func() { op.End(err) }()
63+
}
64+
err = ctx.Err()
65+
if err != nil {
5566
return err
5667
}
5768
if e.redactTimestamps {
@@ -159,3 +170,37 @@ func redactDataPointTimestamps[T int64 | float64](sdp []metricdata.DataPoint[T])
159170
}
160171
return out
161172
}
173+
174+
// countDataPoints counts the total number of data points in a ResourceMetrics.
175+
func countDataPoints(rm *metricdata.ResourceMetrics) int64 {
176+
if rm == nil {
177+
return 0
178+
}
179+
180+
var total int64
181+
for _, sm := range rm.ScopeMetrics {
182+
for _, m := range sm.Metrics {
183+
switch data := m.Data.(type) {
184+
case metricdata.Gauge[int64]:
185+
total += int64(len(data.DataPoints))
186+
case metricdata.Gauge[float64]:
187+
total += int64(len(data.DataPoints))
188+
case metricdata.Sum[int64]:
189+
total += int64(len(data.DataPoints))
190+
case metricdata.Sum[float64]:
191+
total += int64(len(data.DataPoints))
192+
case metricdata.Histogram[int64]:
193+
total += int64(len(data.DataPoints))
194+
case metricdata.Histogram[float64]:
195+
total += int64(len(data.DataPoints))
196+
case metricdata.ExponentialHistogram[int64]:
197+
total += int64(len(data.DataPoints))
198+
case metricdata.ExponentialHistogram[float64]:
199+
total += int64(len(data.DataPoints))
200+
case metricdata.Summary:
201+
total += int64(len(data.DataPoints))
202+
}
203+
}
204+
}
205+
return total
206+
}

0 commit comments

Comments
 (0)