provisionOTLP wires a MeterProvider that periodically reads the process-wide Prometheus registry and pushes the result via OTLP. The exporter and reader are autoconfigured from the standard OTEL_* environment variables, matching the ergonomics of the existing `tracing` directive. It is a no-op when
(ctx caddy.Context)
| 170 | // m.OTLP is false, and honors OTEL_METRICS_EXPORTER=none (autoexport |
| 171 | // short-circuits to a no-op reader in that case). |
| 172 | func (m *Metrics) provisionOTLP(ctx caddy.Context) error { |
| 173 | if !m.OTLP { |
| 174 | return nil |
| 175 | } |
| 176 | |
| 177 | // Register a Prometheus -> OpenTelemetry bridge against the process-wide |
| 178 | // Prometheus registry as the *default* source the NewMetricReader below |
| 179 | // will read from. |
| 180 | // |
| 181 | // NB: despite the "With*" naming, autoexport.WithFallbackMetricProducer is |
| 182 | // a package-level setter (it returns nothing) — it mutates autoexport's |
| 183 | // internal producer registry and takes effect on the very next call to |
| 184 | // NewMetricReader. It is NOT a MetricOption and must not be passed as one. |
| 185 | // Users can still override the source by setting OTEL_METRICS_PRODUCERS. |
| 186 | reg := ctx.GetMetricsRegistry() |
| 187 | autoexport.WithFallbackMetricProducer(func(context.Context) (sdkmetric.Producer, error) { |
| 188 | return otelprom.NewMetricProducer(otelprom.WithGatherer(reg)), nil |
| 189 | }) |
| 190 | |
| 191 | reader, err := autoexport.NewMetricReader(ctx) |
| 192 | if err != nil { |
| 193 | return fmt.Errorf("creating OTLP metric reader: %w", err) |
| 194 | } |
| 195 | |
| 196 | version, _ := caddy.Version() |
| 197 | res, err := resource.Merge(resource.Default(), resource.NewSchemaless( |
| 198 | semconv.WebEngineName(ServerHeader), |
| 199 | semconv.WebEngineVersion(version), |
| 200 | )) |
| 201 | if err != nil { |
| 202 | return fmt.Errorf("building OTLP metrics resource: %w", err) |
| 203 | } |
| 204 | |
| 205 | m.meterProvider = sdkmetric.NewMeterProvider( |
| 206 | sdkmetric.WithResource(res), |
| 207 | sdkmetric.WithReader(reader), |
| 208 | ) |
| 209 | |
| 210 | return nil |
| 211 | } |
| 212 | |
| 213 | // shutdown flushes and tears down the OTLP MeterProvider if one was provisioned. |
| 214 | // Both ForceFlush and Shutdown are always attempted so that a flush failure |