Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add info/debug logging to the metric SDK #4315

Merged
merged 6 commits into from Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -15,6 +15,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Add `Exporter` struct in `go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc`. (#4272)
- Add `Exporter` struct in `go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp`. (#4272)
- Add `WithoutCounterSuffixes` option in `go.opentelemetry.io/otel/exporters/prometheus` to disable addition of `_total` suffixes. (#4306)
- Add info and debug logging to the metric SDK. (#4315)

### Changed

Expand Down
10 changes: 9 additions & 1 deletion exporters/otlp/otlpmetric/otlpmetricgrpc/exporter.go
Expand Up @@ -18,6 +18,7 @@
"context"

ominternal "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/internal"
"go.opentelemetry.io/otel/internal/global"
"go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/aggregation"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
Expand All @@ -43,7 +44,9 @@
// This method returns an error if called after Shutdown.
// This method returns an error if the method is canceled by the passed context.
func (e *Exporter) Export(ctx context.Context, rm *metricdata.ResourceMetrics) error {
return e.wrapped.Export(ctx, rm)
err := e.wrapped.Export(ctx, rm)
global.Debug("OTLP/gRPC exporter export", "Data", rm)
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
return err
}

// ForceFlush flushes any metric data held by an exporter.
Expand All @@ -67,6 +70,11 @@
return e.wrapped.Shutdown(ctx)
}

// MarshalLog returns logging data about the Exporter.
func (e *Exporter) MarshalLog() interface{} {
return struct{ Type string }{Type: "OTLP/gRPC"}

Check warning on line 75 in exporters/otlp/otlpmetric/otlpmetricgrpc/exporter.go

View check run for this annotation

Codecov / codecov/patch

exporters/otlp/otlpmetric/otlpmetricgrpc/exporter.go#L74-L75

Added lines #L74 - L75 were not covered by tests
}

// New returns an OpenTelemetry metric Exporter. The Exporter can be used with
// a PeriodicReader to export OpenTelemetry metric data to an OTLP receiving
// endpoint using gRPC.
Expand Down
10 changes: 9 additions & 1 deletion exporters/otlp/otlpmetric/otlpmetrichttp/exporter.go
Expand Up @@ -18,6 +18,7 @@
"context"

ominternal "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/internal"
"go.opentelemetry.io/otel/internal/global"
"go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/aggregation"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
Expand All @@ -43,7 +44,9 @@
// This method returns an error if called after Shutdown.
// This method returns an error if the method is canceled by the passed context.
func (e *Exporter) Export(ctx context.Context, rm *metricdata.ResourceMetrics) error {
return e.wrapped.Export(ctx, rm)
err := e.wrapped.Export(ctx, rm)
global.Debug("OTLP/HTTP exporter export", "Data", rm)
return err
}

// ForceFlush flushes any metric data held by an exporter.
Expand All @@ -67,6 +70,11 @@
return e.wrapped.Shutdown(ctx)
}

// MarshalLog returns logging data about the Exporter.
func (e *Exporter) MarshalLog() interface{} {
return struct{ Type string }{Type: "OTLP/HTTP"}

Check warning on line 75 in exporters/otlp/otlpmetric/otlpmetrichttp/exporter.go

View check run for this annotation

Codecov / codecov/patch

exporters/otlp/otlpmetric/otlpmetrichttp/exporter.go#L74-L75

Added lines #L74 - L75 were not covered by tests
}

// New returns an OpenTelemetry metric Exporter. The Exporter can be used with
// a PeriodicReader to export OpenTelemetry metric data to an OTLP receiving
// endpoint using protobufs over HTTP.
Expand Down
21 changes: 21 additions & 0 deletions exporters/prometheus/exporter.go
Expand Up @@ -53,6 +53,25 @@
metric.Reader
}

// MarshalLog returns logging data about the Exporter.
func (e *Exporter) MarshalLog() interface{} {
const t = "Prometheus exporter"

if r, ok := e.Reader.(*metric.ManualReader); ok {
under := r.MarshalLog()
if data, ok := under.(struct {
Type string
Registered bool
Shutdown bool
}); ok {
data.Type = t
return data
}

Check warning on line 69 in exporters/prometheus/exporter.go

View check run for this annotation

Codecov / codecov/patch

exporters/prometheus/exporter.go#L57-L69

Added lines #L57 - L69 were not covered by tests
}

return struct{ Type string }{Type: t}

Check warning on line 72 in exporters/prometheus/exporter.go

View check run for this annotation

Codecov / codecov/patch

exporters/prometheus/exporter.go#L72

Added line #L72 was not covered by tests
}

var _ metric.Reader = &Exporter{}

// collector is used to implement prometheus.Collector.
Expand Down Expand Up @@ -129,6 +148,8 @@
}
}

global.Debug("Prometheus exporter export", "Data", metrics)

// Initialize (once) targetInfo and disableTargetInfo.
func() {
c.mu.Lock()
Expand Down
7 changes: 7 additions & 0 deletions exporters/stdout/stdoutmetric/exporter.go
Expand Up @@ -73,6 +73,9 @@
if e.redactTimestamps {
redactTimestamps(data)
}

global.Debug("STDOUT exporter export", "Data", data)

return e.encVal.Load().(encoderHolder).Encode(data)
}

Expand All @@ -90,6 +93,10 @@
return ctx.Err()
}

func (e *exporter) MarshalLog() interface{} {
return struct{ Type string }{Type: "STDOUT"}

Check warning on line 97 in exporters/stdout/stdoutmetric/exporter.go

View check run for this annotation

Codecov / codecov/patch

exporters/stdout/stdoutmetric/exporter.go#L96-L97

Added lines #L96 - L97 were not covered by tests
}

func redactTimestamps(orig *metricdata.ResourceMetrics) {
for i, sm := range orig.ScopeMetrics {
metrics := sm.Metrics
Expand Down
19 changes: 19 additions & 0 deletions sdk/metric/manual_reader.go
Expand Up @@ -157,9 +157,28 @@
}
rm.ScopeMetrics = append(rm.ScopeMetrics, externalMetrics...)
}

global.Debug("ManualReader collection", "Data", rm)

return unifyErrors(errs)
}

// MarshalLog returns logging data about the ManualReader.
func (r *ManualReader) MarshalLog() interface{} {
r.mu.Lock()
down := r.isShutdown
r.mu.Unlock()
return struct {
Type string
Registered bool
Shutdown bool
}{
Type: "ManualReader",
Registered: r.sdkProducer.Load() != nil,
Shutdown: down,
}

Check warning on line 179 in sdk/metric/manual_reader.go

View check run for this annotation

Codecov / codecov/patch

sdk/metric/manual_reader.go#L167-L179

Added lines #L167 - L179 were not covered by tests
}

// manualReaderConfig contains configuration options for a ManualReader.
type manualReaderConfig struct {
temporalitySelector TemporalitySelector
Expand Down
27 changes: 27 additions & 0 deletions sdk/metric/periodic_reader.go
Expand Up @@ -115,6 +115,7 @@
conf := newPeriodicReaderConfig(options)
ctx, cancel := context.WithCancel(context.Background())
r := &PeriodicReader{
interval: conf.interval,
timeout: conf.timeout,
exporter: exporter,
flushCh: make(chan chan error),
Expand Down Expand Up @@ -144,6 +145,7 @@
isShutdown bool
externalProducers atomic.Value

interval time.Duration
timeout time.Duration
exporter Exporter
flushCh chan chan error
Expand Down Expand Up @@ -280,6 +282,9 @@
}
rm.ScopeMetrics = append(rm.ScopeMetrics, externalMetrics...)
}

global.Debug("PeriodicReader collection", "Data", rm)

return unifyErrors(errs)
}

Expand Down Expand Up @@ -350,3 +355,25 @@
})
return err
}

// MarshalLog returns logging data about the PeriodicReader.
func (r *PeriodicReader) MarshalLog() interface{} {
r.mu.Lock()
down := r.isShutdown
r.mu.Unlock()
return struct {
Type string
Exporter Exporter
Registered bool
Shutdown bool
Interval time.Duration
Timeout time.Duration
}{
Type: "PeriodicReader",
Exporter: r.exporter,
Registered: r.sdkProducer.Load() != nil,
Shutdown: down,
Interval: r.interval,
Timeout: r.timeout,
}

Check warning on line 378 in sdk/metric/periodic_reader.go

View check run for this annotation

Codecov / codecov/patch

sdk/metric/periodic_reader.go#L360-L378

Added lines #L360 - L378 were not covered by tests
}
17 changes: 16 additions & 1 deletion sdk/metric/provider.go
Expand Up @@ -51,11 +51,19 @@ var _ metric.MeterProvider = (*MeterProvider)(nil)
func NewMeterProvider(options ...Option) *MeterProvider {
conf := newConfig(options)
flush, sdown := conf.readerSignals()
return &MeterProvider{

mp := &MeterProvider{
pipes: newPipelines(conf.res, conf.readers, conf.views),
forceFlush: flush,
shutdown: sdown,
}
// Log after creation so all readers show correctly they are registered.
global.Info("MeterProvider created",
"Resource", conf.res,
"Readers", conf.readers,
"Views", len(conf.views),
)
return mp
}

// Meter returns a Meter with the given name and configured with options.
Expand Down Expand Up @@ -83,6 +91,13 @@ func (mp *MeterProvider) Meter(name string, options ...metric.MeterOption) metri
Version: c.InstrumentationVersion(),
SchemaURL: c.SchemaURL(),
}

global.Info("Meter created",
"Name", s.Name,
"Version", s.Version,
"SchemaURL", s.SchemaURL,
)

return mp.meters.Lookup(s, func() *meter {
return newMeter(s, mp.pipes)
})
Expand Down