Skip to content

Commit

Permalink
Add OpenCensus reader bridge PoC
Browse files Browse the repository at this point in the history
  • Loading branch information
dashpole committed Sep 22, 2022
1 parent 038248b commit becf42b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 6 deletions.
43 changes: 42 additions & 1 deletion bridge/opencensus/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

ocmetricdata "go.opencensus.io/metric/metricdata"
"go.opencensus.io/metric/metricexport"
"go.opencensus.io/metric/metricproducer"

internal "go.opentelemetry.io/otel/bridge/opencensus/internal/ocmetric"
"go.opentelemetry.io/otel/sdk/instrumentation"
Expand All @@ -38,7 +39,7 @@ type exporter struct {
}

// NewMetricExporter returns an OpenCensus exporter that exports to an
// OpenTelemetry exporter.
// OpenTelemetry (push) exporter.
func NewMetricExporter(base metric.Exporter, res *resource.Resource) metricexport.Exporter {
return &exporter{base: base}
}
Expand All @@ -61,3 +62,43 @@ func (e *exporter) ExportMetrics(ctx context.Context, ocmetrics []*ocmetricdata.
},
}})
}

type reader struct {
// This reader doesn't actually matter. It is just there to satisfy the Reader interface.
metric.Reader
res *resource.Resource
manager *metricproducer.Manager
}

// NewMetricExporter returns an OpenTelemetry metric.Reader that can be used as
// a source of metrics for OpenTelemetry (pull) exporters.
func NewMetricReader(res *resource.Resource) metric.Reader {
return &reader{
Reader: metric.NewManualReader(),
res: res,
manager: metricproducer.GlobalManager(),
}
}

// Collect overrides the manual reader's collect method to retrieve metrics from OpenCensus.
func (r *reader) Collect(context.Context) (metricdata.ResourceMetrics, error) {
producers := r.manager.GetAll()
data := []*ocmetricdata.Metric{}
for _, ocProducer := range producers {
data = append(data, ocProducer.Read()...)
}
otelmetrics, err := internal.ConvertMetrics(data)
if err != nil {
return metricdata.ResourceMetrics{}, err
}
return metricdata.ResourceMetrics{
Resource: r.res,
ScopeMetrics: []metricdata.ScopeMetrics{
{
Scope: instrumentation.Scope{
Name: "go.opentelemetry.io/otel/bridge/opencensus",
},
Metrics: otelmetrics,
},
}}, nil
}
29 changes: 24 additions & 5 deletions exporters/prometheus/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,41 @@ type collector struct {
}

// config is added here to allow for options expansion in the future.
type config struct{}
type config struct {
reader metric.Reader
}

// Option may be used in the future to apply options to a Prometheus Exporter config.
type Option interface {
apply(config) config
}

type optionFunc func(config) config

func (fn optionFunc) apply(cfg config) config {
return fn(cfg)
}

// WithReader provides a Reader from which the prometheus exporter reads metrics.
func WithReader(r metric.Reader) Option {
return optionFunc(func(config config) config {
config.reader = r
return config
})
}

// New returns a Prometheus Exporter.
func New(_ ...Option) Exporter {
func New(opts ...Option) Exporter {
// this assumes that the default temporality selector will always return cumulative.
// we only support cumulative temporality, so building our own reader enforces this.
reader := metric.NewManualReader()
cfg := config{reader: metric.NewManualReader()}
for _, o := range opts {
cfg = o.apply(cfg)
}
e := Exporter{
Reader: reader,
Reader: cfg.reader,
Collector: &collector{
Reader: reader,
Reader: cfg.reader,
},
}
return e
Expand Down

0 comments on commit becf42b

Please sign in to comment.