Skip to content

Commit

Permalink
Remove global metrics level, use level passed in create settings (ope…
Browse files Browse the repository at this point in the history
…n-telemetry#5831)

Signed-off-by: Bogdan <[email protected]>
  • Loading branch information
bogdandrutu authored Aug 5, 2022
1 parent ba6fe5a commit 33cbb92
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 29 deletions.
9 changes: 7 additions & 2 deletions exporter/exporterhelper/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/configtelemetry"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/internal/obsreportconfig/obsmetrics"
Expand Down Expand Up @@ -107,7 +108,9 @@ func TestLogsExporter_Default_ReturnError(t *testing.T) {
}

func TestLogsExporter_WithRecordLogs(t *testing.T) {
le, err := NewLogsExporter(&fakeLogsExporterConfig, componenttest.NewNopExporterCreateSettings(), newPushLogsData(nil))
set := componenttest.NewNopExporterCreateSettings()
set.TelemetrySettings.MetricsLevel = configtelemetry.LevelNormal
le, err := NewLogsExporter(&fakeLogsExporterConfig, set, newPushLogsData(nil))
require.NoError(t, err)
require.NotNil(t, le)

Expand All @@ -116,7 +119,9 @@ func TestLogsExporter_WithRecordLogs(t *testing.T) {

func TestLogsExporter_WithRecordLogs_ReturnError(t *testing.T) {
want := errors.New("my_error")
le, err := NewLogsExporter(&fakeLogsExporterConfig, componenttest.NewNopExporterCreateSettings(), newPushLogsData(want))
set := componenttest.NewNopExporterCreateSettings()
set.TelemetrySettings.MetricsLevel = configtelemetry.LevelNormal
le, err := NewLogsExporter(&fakeLogsExporterConfig, set, newPushLogsData(want))
require.Nil(t, err)
require.NotNil(t, le)

Expand Down
9 changes: 7 additions & 2 deletions exporter/exporterhelper/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/configtelemetry"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/internal/obsreportconfig/obsmetrics"
Expand Down Expand Up @@ -106,7 +107,9 @@ func TestMetricsExporter_Default_ReturnError(t *testing.T) {
}

func TestMetricsExporter_WithRecordMetrics(t *testing.T) {
me, err := NewMetricsExporter(&fakeMetricsExporterConfig, componenttest.NewNopExporterCreateSettings(), newPushMetricsData(nil))
set := componenttest.NewNopExporterCreateSettings()
set.TelemetrySettings.MetricsLevel = configtelemetry.LevelNormal
me, err := NewMetricsExporter(&fakeMetricsExporterConfig, set, newPushMetricsData(nil))
require.NoError(t, err)
require.NotNil(t, me)

Expand All @@ -115,7 +118,9 @@ func TestMetricsExporter_WithRecordMetrics(t *testing.T) {

func TestMetricsExporter_WithRecordMetrics_ReturnError(t *testing.T) {
want := errors.New("my_error")
me, err := NewMetricsExporter(&fakeMetricsExporterConfig, componenttest.NewNopExporterCreateSettings(), newPushMetricsData(want))
set := componenttest.NewNopExporterCreateSettings()
set.TelemetrySettings.MetricsLevel = configtelemetry.LevelNormal
me, err := NewMetricsExporter(&fakeMetricsExporterConfig, set, newPushMetricsData(want))
require.NoError(t, err)
require.NotNil(t, me)

Expand Down
9 changes: 7 additions & 2 deletions exporter/exporterhelper/traces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/configtelemetry"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/internal/obsreportconfig/obsmetrics"
Expand Down Expand Up @@ -104,7 +105,9 @@ func TestTracesExporter_Default_ReturnError(t *testing.T) {
}

func TestTracesExporter_WithRecordMetrics(t *testing.T) {
te, err := NewTracesExporter(&fakeTracesExporterConfig, componenttest.NewNopExporterCreateSettings(), newTraceDataPusher(nil))
set := componenttest.NewNopExporterCreateSettings()
set.TelemetrySettings.MetricsLevel = configtelemetry.LevelNormal
te, err := NewTracesExporter(&fakeTracesExporterConfig, set, newTraceDataPusher(nil))
require.NoError(t, err)
require.NotNil(t, te)

Expand All @@ -113,7 +116,9 @@ func TestTracesExporter_WithRecordMetrics(t *testing.T) {

func TestTracesExporter_WithRecordMetrics_ReturnError(t *testing.T) {
want := errors.New("my_error")
te, err := NewTracesExporter(&fakeTracesExporterConfig, componenttest.NewNopExporterCreateSettings(), newTraceDataPusher(want))
set := componenttest.NewNopExporterCreateSettings()
set.TelemetrySettings.MetricsLevel = configtelemetry.LevelNormal
te, err := NewTracesExporter(&fakeTracesExporterConfig, set, newTraceDataPusher(want))
require.NoError(t, err)
require.NotNil(t, te)

Expand Down
22 changes: 5 additions & 17 deletions internal/obsreportconfig/obsreportconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,11 @@ import (
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"
"go.uber.org/atomic"

"go.opentelemetry.io/collector/config/configtelemetry"
"go.opentelemetry.io/collector/internal/obsreportconfig/obsmetrics"
)

var (
globalLevel = atomic.NewInt32(int32(configtelemetry.LevelBasic))
)

// ObsMetrics wraps OpenCensus View for Collector observability metrics
type ObsMetrics struct {
Views []*view.View
Expand All @@ -36,20 +31,13 @@ type ObsMetrics struct {
// Configure is used to control the settings that will be used by the obsreport
// package.
func Configure(level configtelemetry.Level) *ObsMetrics {
globalLevel.Store(int32(level))

var views []*view.View
if Level() != configtelemetry.LevelNone {
views = allViews()
ret := &ObsMetrics{}
if level == configtelemetry.LevelNone {
return ret
}

return &ObsMetrics{
Views: views,
}
}

func Level() configtelemetry.Level {
return configtelemetry.Level(globalLevel.Load())
ret.Views = allViews()
return ret
}

// allViews return the list of all views that needs to be configured.
Expand Down
5 changes: 3 additions & 2 deletions obsreport/obsreport_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/configtelemetry"
"go.opentelemetry.io/collector/internal/obsreportconfig"
"go.opentelemetry.io/collector/internal/obsreportconfig/obsmetrics"
)

// Exporter is a helper to add observability to a component.Exporter.
type Exporter struct {
level configtelemetry.Level
spanNamePrefix string
mutators []tag.Mutator
tracer trace.Tracer
Expand All @@ -47,6 +47,7 @@ type ExporterSettings struct {
// NewExporter creates a new Exporter.
func NewExporter(cfg ExporterSettings) *Exporter {
return &Exporter{
level: cfg.ExporterCreateSettings.TelemetrySettings.MetricsLevel,
spanNamePrefix: obsmetrics.ExporterPrefix + cfg.ExporterID.String(),
mutators: []tag.Mutator{tag.Upsert(obsmetrics.TagKeyExporter, cfg.ExporterID.String(), tag.WithTTL(tag.TTLNoPropagation))},
tracer: cfg.ExporterCreateSettings.TracerProvider.Tracer(cfg.ExporterID.String()),
Expand Down Expand Up @@ -105,7 +106,7 @@ func (exp *Exporter) startOp(ctx context.Context, operationSuffix string) contex
}

func (exp *Exporter) recordMetrics(ctx context.Context, numSent, numFailedToSend int64, sentMeasure, failedToSendMeasure *stats.Int64Measure) {
if obsreportconfig.Level() == configtelemetry.LevelNone {
if exp.level == configtelemetry.LevelNone {
return
}
// Ignore the error for now. This should not happen.
Expand Down
5 changes: 3 additions & 2 deletions obsreport/obsreport_receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/configtelemetry"
"go.opentelemetry.io/collector/internal/obsreportconfig"
"go.opentelemetry.io/collector/internal/obsreportconfig/obsmetrics"
)

// Receiver is a helper to add observability to a component.Receiver.
type Receiver struct {
level configtelemetry.Level
spanNamePrefix string
transport string
longLivedCtx bool
Expand All @@ -54,6 +54,7 @@ type ReceiverSettings struct {
// NewReceiver creates a new Receiver.
func NewReceiver(cfg ReceiverSettings) *Receiver {
return &Receiver{
level: cfg.ReceiverCreateSettings.TelemetrySettings.MetricsLevel,
spanNamePrefix: obsmetrics.ReceiverPrefix + cfg.ReceiverID.String(),
transport: cfg.Transport,
longLivedCtx: cfg.LongLivedCtx,
Expand Down Expand Up @@ -161,7 +162,7 @@ func (rec *Receiver) endOp(

span := trace.SpanFromContext(receiverCtx)

if obsreportconfig.Level() != configtelemetry.LevelNone {
if rec.level != configtelemetry.LevelNone {
var acceptedMeasure, refusedMeasure *stats.Int64Measure
switch dataType {
case config.TracesDataType:
Expand Down
5 changes: 3 additions & 2 deletions obsreport/obsreport_scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/configtelemetry"
"go.opentelemetry.io/collector/internal/obsreportconfig"
"go.opentelemetry.io/collector/internal/obsreportconfig/obsmetrics"
"go.opentelemetry.io/collector/receiver/scrapererror"
)

// Scraper is a helper to add observability to a component.Scraper.
type Scraper struct {
level configtelemetry.Level
receiverID config.ComponentID
scraper config.ComponentID
mutators []tag.Mutator
Expand All @@ -49,6 +49,7 @@ type ScraperSettings struct {
// NewScraper creates a new Scraper.
func NewScraper(cfg ScraperSettings) *Scraper {
return &Scraper{
level: cfg.ReceiverCreateSettings.TelemetrySettings.MetricsLevel,
receiverID: cfg.ReceiverID,
scraper: cfg.Scraper,
mutators: []tag.Mutator{
Expand Down Expand Up @@ -89,7 +90,7 @@ func (s *Scraper) EndMetricsOp(

span := trace.SpanFromContext(scraperCtx)

if obsreportconfig.Level() != configtelemetry.LevelNone {
if s.level != configtelemetry.LevelNone {
stats.Record(
scraperCtx,
obsmetrics.ScraperScrapedMetricPoints.M(int64(numScrapedMetrics)),
Expand Down
2 changes: 2 additions & 0 deletions receiver/otlpreceiver/otlp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
"go.opentelemetry.io/collector/config/configgrpc"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/config/confignet"
"go.opentelemetry.io/collector/config/configtelemetry"
"go.opentelemetry.io/collector/config/configtls"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/consumer/consumertest"
Expand Down Expand Up @@ -859,6 +860,7 @@ func newHTTPReceiver(t *testing.T, endpoint string, tc consumer.Traces, mc consu

func newReceiver(t *testing.T, factory component.ReceiverFactory, cfg *Config, tc consumer.Traces, mc consumer.Metrics) component.Component {
set := componenttest.NewNopReceiverCreateSettings()
set.TelemetrySettings.MetricsLevel = configtelemetry.LevelNormal
var r component.Component
var err error
if tc != nil {
Expand Down

0 comments on commit 33cbb92

Please sign in to comment.