Skip to content

Commit

Permalink
fix prometheus example
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua MacDonald committed May 5, 2022
1 parent b149c3b commit 838203f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 61 deletions.
25 changes: 12 additions & 13 deletions example/prometheus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@ import (
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/instrument"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/views"
"go.opentelemetry.io/otel/sdk/metric/view"
"go.opentelemetry.io/otel/sdk/resource"
)

var (
lemonsKey = attribute.Key("ex.com/lemons")

// TODO Bring back Global package
meterProvider metric.MeterProvider
)

Expand All @@ -45,20 +44,20 @@ func initMeter() metric.MeterProvider {
}

sdk := sdkmetric.New(
sdkmetric.WithReader(exporter),
sdkmetric.WithResource(resource.NewSchemaless(attribute.String("resource", "etc"))),
sdkmetric.WithViews(
views.New(
views.MatchInstrumentName("ex.com.one"),
views.WithName("example_one"),
sdkmetric.WithReader(
exporter,
view.WithClause(
view.MatchInstrumentName("ex.com.one"),
view.WithName("example_one"),
),
views.New(
views.MatchInstrumentName("ex.com.two"),
views.WithName("example_two"),
view.WithClause(
view.MatchInstrumentName("ex.com.two"),
view.WithName("example_two"),
),
views.New(
views.MatchInstrumentName("ex.com.three"),
views.WithName("example_three"),
view.WithClause(
view.MatchInstrumentName("ex.com.three"),
view.WithName("example_three"),
),
),
)
Expand Down
14 changes: 7 additions & 7 deletions exporters/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ import (

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/aggregator/aggregation"
"go.opentelemetry.io/otel/sdk/metric/number"
"go.opentelemetry.io/otel/sdk/metric/reader"
"go.opentelemetry.io/otel/sdk/metric/sdkinstrument"
"go.opentelemetry.io/otel/sdk/resource"
)
Expand All @@ -44,10 +44,10 @@ type Exporter struct {
gatherer prometheus.Gatherer

lock sync.Mutex
producer reader.Producer
producer metric.Producer
}

var _ reader.Reader = &Exporter{}
var _ metric.Reader = &Exporter{}
var _ http.Handler = &Exporter{}

// ErrUnsupportedAggregator is returned for unrepresentable aggregator
Expand Down Expand Up @@ -109,23 +109,23 @@ func (e *Exporter) String() string {
return "prometheus"
}

func (e *Exporter) Register(p reader.Producer) {
func (e *Exporter) Register(p metric.Producer) {
e.lock.Lock()
defer e.lock.Unlock()
e.producer = p
}

func (e *Exporter) getProducer() reader.Producer {
func (e *Exporter) getProducer() metric.Producer {
e.lock.Lock()
defer e.lock.Unlock()
return e.producer
}

func (e *Exporter) Flush(ctx context.Context) error {
func (e *Exporter) ForceFlush(_ context.Context) error {
return nil
}

func (e *Exporter) Shutdown(ctx context.Context) error {
func (e *Exporter) Shutdown(_ context.Context) error {
return nil
}

Expand Down
80 changes: 39 additions & 41 deletions sdk/metric/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,48 +20,46 @@ import (
"go.opentelemetry.io/otel/sdk/metric/data"
)

type (
// Reader is the interface used between the SDK and an
// exporter. Control flow is bi-directional through the
// Reader, since the SDK initiates ForceFlush and Shutdown
// while the initiates collection. The Register() method here
// informs the Reader that it can begin reading, signaling the
// start of bi-directional control flow.
//
// Typically, push-based exporters that are periodic will
// implement PeroidicExporter themselves and construct a
// PeriodicReader to satisfy this interface.
//
// Pull-based exporters will typically implement Register
// themselves, since they read on demand.
Reader interface {
// String describes this reader.
String() string
// Reader is the interface used between the SDK and an
// exporter. Control flow is bi-directional through the
// Reader, since the SDK initiates ForceFlush and Shutdown
// while the initiates collection. The Register() method here
// informs the Reader that it can begin reading, signaling the
// start of bi-directional control flow.
//
// Typically, push-based exporters that are periodic will
// implement PeroidicExporter themselves and construct a
// PeriodicReader to satisfy this interface.
//
// Pull-based exporters will typically implement Register
// themselves, since they read on demand.
type Reader interface {
// String describes this reader.
String() string

// Register is called when the SDK is fully
// configured. The Producer passed allows the
// Reader to begin collecting metrics using its
// Produce() method.
Register(Producer)
// Register is called when the SDK is fully
// configured. The Producer passed allows the
// Reader to begin collecting metrics using its
// Produce() method.
Register(Producer)

// ForceFlush is called when MeterProvider.ForceFlush() is called.
ForceFlush(context.Context) error
// ForceFlush is called when MeterProvider.ForceFlush() is called.
ForceFlush(context.Context) error

// Shutdown is called when MeterProvider.Shutdown() is called.
Shutdown(context.Context) error
}
// Shutdown is called when MeterProvider.Shutdown() is called.
Shutdown(context.Context) error
}

// Producer is the interface used to perform collection by the reader.
Producer interface {
// Produce returns metrics from a single collection.
//
// Produce may be called concurrently,
//
// The `in` parameter supports re-use of memory from
// one collection to the next. Callers that pass `in`
// will write metrics into the same slices and structs.
//
// When `in` is nil, a new Metrics object is returned.
Produce(in *data.Metrics) data.Metrics
}
)
// Producer is the interface used to perform collection by the reader.
type Producer interface {
// Produce returns metrics from a single collection.
//
// Produce may be called concurrently,
//
// The `in` parameter supports re-use of memory from
// one collection to the next. Callers that pass `in`
// will write metrics into the same slices and structs.
//
// When `in` is nil, a new Metrics object is returned.
Produce(in *data.Metrics) data.Metrics
}

0 comments on commit 838203f

Please sign in to comment.