2022-09-20 21:14:28 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2024-02-29 08:05:28 +02:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2022-09-20 21:14:28 +02:00
|
|
|
|
|
|
|
package opencensus // import "go.opentelemetry.io/otel/bridge/opencensus"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
ocmetricdata "go.opencensus.io/metric/metricdata"
|
2022-12-19 18:05:46 +02:00
|
|
|
"go.opencensus.io/metric/metricproducer"
|
2022-09-20 21:14:28 +02:00
|
|
|
|
|
|
|
internal "go.opentelemetry.io/otel/bridge/opencensus/internal/ocmetric"
|
|
|
|
"go.opentelemetry.io/otel/sdk/instrumentation"
|
|
|
|
"go.opentelemetry.io/otel/sdk/metric"
|
|
|
|
"go.opentelemetry.io/otel/sdk/metric/metricdata"
|
|
|
|
)
|
|
|
|
|
2023-10-09 16:04:32 +02:00
|
|
|
// MetricProducer implements the [go.opentelemetry.io/otel/sdk/metric.Producer] to provide metrics
|
|
|
|
// from OpenCensus to the OpenTelemetry SDK.
|
|
|
|
type MetricProducer struct {
|
2022-12-19 18:05:46 +02:00
|
|
|
manager *metricproducer.Manager
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMetricProducer returns a metric.Producer that fetches metrics from
|
|
|
|
// OpenCensus.
|
2023-10-09 16:04:32 +02:00
|
|
|
func NewMetricProducer(opts ...MetricOption) *MetricProducer {
|
|
|
|
return &MetricProducer{
|
2022-12-19 18:05:46 +02:00
|
|
|
manager: metricproducer.GlobalManager(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-09 16:04:32 +02:00
|
|
|
var _ metric.Producer = (*MetricProducer)(nil)
|
|
|
|
|
|
|
|
// Produce fetches metrics from the OpenCensus manager,
|
|
|
|
// translates them to OpenTelemetry's data model, and returns them.
|
|
|
|
func (p *MetricProducer) Produce(context.Context) ([]metricdata.ScopeMetrics, error) {
|
2022-12-19 18:05:46 +02:00
|
|
|
producers := p.manager.GetAll()
|
|
|
|
data := []*ocmetricdata.Metric{}
|
|
|
|
for _, ocProducer := range producers {
|
|
|
|
data = append(data, ocProducer.Read()...)
|
|
|
|
}
|
|
|
|
otelmetrics, err := internal.ConvertMetrics(data)
|
|
|
|
if len(otelmetrics) == 0 {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return []metricdata.ScopeMetrics{{
|
|
|
|
Scope: instrumentation.Scope{
|
2023-10-11 08:48:58 +02:00
|
|
|
Name: scopeName,
|
|
|
|
Version: Version(),
|
2022-12-19 18:05:46 +02:00
|
|
|
},
|
|
|
|
Metrics: otelmetrics,
|
|
|
|
}}, err
|
|
|
|
}
|