Skip to content

Commit

Permalink
Making access atomic to the collector's level (#4549)
Browse files Browse the repository at this point in the history
* Making access atomic to the collector's level

In order to allow for more tests to run concurrently without triggering
the race detector, Level needs to be guarded by some atomic method.
This change makes access to the collector's Level managable via sync
method which may lead to eventual consistent.

* Update obsreport/obsreport_test.go

Remove this change
  • Loading branch information
MovieStoreGuy committed Dec 14, 2021
1 parent 2998864 commit 5514ecb
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion config/configtelemetry/configtelemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func Flags(flags *flag.FlagSet) {

// Level is the level of internal telemetry (metrics, logs, traces about the component itself)
// that every component should generate.
type Level int8
type Level int32

var _ flag.Value = (*Level)(nil)

Expand Down
13 changes: 10 additions & 3 deletions internal/obsreportconfig/obsreportconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package obsreportconfig // import "go.opentelemetry.io/collector/internal/obsreportconfig"

import (
"sync/atomic"

"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"
Expand All @@ -24,7 +26,7 @@ import (
)

var (
Level = configtelemetry.LevelBasic
globalLevel = int32(configtelemetry.LevelBasic)
)

// ObsMetrics wraps OpenCensus View for Collector observability metrics
Expand All @@ -35,10 +37,11 @@ type ObsMetrics struct {
// Configure is used to control the settings that will be used by the obsreport
// package.
func Configure(level configtelemetry.Level) *ObsMetrics {
Level = level
atomic.StoreInt32(&globalLevel, int32(level))

var views []*view.View

if Level != configtelemetry.LevelNone {
if Level() != configtelemetry.LevelNone {
obsMetricViews := allViews()
views = append(views, obsMetricViews.Views...)
}
Expand All @@ -48,6 +51,10 @@ func Configure(level configtelemetry.Level) *ObsMetrics {
}
}

func Level() configtelemetry.Level {
return configtelemetry.Level(atomic.LoadInt32(&globalLevel))
}

// allViews return the list of all views that needs to be configured.
func allViews() *ObsMetrics {
var views []*view.View
Expand Down
2 changes: 1 addition & 1 deletion obsreport/obsreport_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,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 obsreportconfig.Level() == configtelemetry.LevelNone {
return
}
// Ignore the error for now. This should not happen.
Expand Down
2 changes: 1 addition & 1 deletion obsreport/obsreport_receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (rec *Receiver) endOp(

span := trace.SpanFromContext(receiverCtx)

if obsreportconfig.Level != configtelemetry.LevelNone {
if obsreportconfig.Level() != configtelemetry.LevelNone {
var acceptedMeasure, refusedMeasure *stats.Int64Measure
switch dataType {
case config.TracesDataType:
Expand Down
2 changes: 1 addition & 1 deletion obsreport/obsreport_scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (s *Scraper) EndMetricsOp(

span := trace.SpanFromContext(scraperCtx)

if obsreportconfig.Level != configtelemetry.LevelNone {
if obsreportconfig.Level() != configtelemetry.LevelNone {
stats.Record(
scraperCtx,
obsmetrics.ScraperScrapedMetricPoints.M(int64(numScrapedMetrics)),
Expand Down

0 comments on commit 5514ecb

Please sign in to comment.