Skip to content

Commit

Permalink
Hide OpenCensus reference from public APIs in obsreport package
Browse files Browse the repository at this point in the history
  • Loading branch information
mxiamxia committed May 21, 2021
1 parent fe57963 commit ea7fcbb
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 22 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
## 🛑 Breaking changes 🛑

- Remove unused logstest package (#3222)
- Remove the OpenCensus references for the following public APIs in `obsreport` package (#3253)
- `obsreport.Config`
- `obsreport.AllViews`
- `obsreport.ProcessorMetricViews`


## v0.27.0 Beta

Expand Down
22 changes: 17 additions & 5 deletions obsreport/obsreport.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ var (
okStatus = trace.Status{Code: trace.StatusCodeOK}
)

// ObsMetrics wraps OpenCensus View for Collector observability metrics
type ObsMetrics struct {
Views []*view.View
}

// setParentLink tries to retrieve a span from parentCtx and if one exists
// sets its SpanID, TraceID as a link to the given child Span.
// It returns true only if it retrieved a parent span from the context.
Expand All @@ -62,15 +67,19 @@ func setParentLink(parentCtx context.Context, childSpan *trace.Span) bool {

// Configure is used to control the settings that will be used by the obsreport
// package.
func Configure(level configtelemetry.Level) (views []*view.View) {
func Configure(level configtelemetry.Level) *ObsMetrics {
gLevel = level
var views []*view.View

if gLevel != configtelemetry.LevelNone {
gProcessor.level = level
views = append(views, AllViews()...)
obsMetricViews := AllViews()
views = append(views, obsMetricViews.Views...)
}

return views
return &ObsMetrics{
Views: views,
}
}

func buildComponentPrefix(componentPrefix, configType string) string {
Expand All @@ -84,7 +93,8 @@ func buildComponentPrefix(componentPrefix, configType string) string {
}

// AllViews return the list of all views that needs to be configured.
func AllViews() (views []*view.View) {
func AllViews() *ObsMetrics {
var views []*view.View
// Receiver views.
measures := []*stats.Int64Measure{
mReceiverAcceptedSpans,
Expand Down Expand Up @@ -134,7 +144,9 @@ func AllViews() (views []*view.View) {
tagKeys = []tag.Key{tagKeyProcessor}
views = append(views, genViews(measures, tagKeys, view.Sum())...)

return views
return &ObsMetrics{
Views: views,
}
}

func genViews(
Expand Down
8 changes: 5 additions & 3 deletions obsreport/obsreport_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ func BuildProcessorCustomMetricName(configType, metric string) string {
}

// ProcessorMetricViews builds the metric views for custom metrics of processors.
func ProcessorMetricViews(configType string, legacyViews []*view.View) []*view.View {
func ProcessorMetricViews(configType string, legacyViews *ObsMetrics) *ObsMetrics {
var allViews []*view.View
if gLevel != configtelemetry.LevelNone {
for _, legacyView := range legacyViews {
for _, legacyView := range legacyViews.Views {
// Ignore any nil entry and views without measure or aggregation.
// These can't be registered but some code registering legacy views may
// ignore the errors.
Expand All @@ -112,7 +112,9 @@ func ProcessorMetricViews(configType string, legacyViews []*view.View) []*view.V
}
}

return allViews
return &ObsMetrics{
Views: allViews,
}
}

var gProcessor = &Processor{level: configtelemetry.LevelNone}
Expand Down
12 changes: 6 additions & 6 deletions obsreport/obsreport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,23 @@ func TestConfigure(t *testing.T) {
{
name: "basic",
level: configtelemetry.LevelBasic,
wantViews: obsreport.AllViews(),
wantViews: obsreport.AllViews().Views,
},
{
name: "normal",
level: configtelemetry.LevelNormal,
wantViews: obsreport.AllViews(),
wantViews: obsreport.AllViews().Views,
},
{
name: "detailed",
level: configtelemetry.LevelDetailed,
wantViews: obsreport.AllViews(),
wantViews: obsreport.AllViews().Views,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotViews := obsreport.Configure(tt.level)
assert.Equal(t, tt.wantViews, gotViews)
assert.Equal(t, tt.wantViews, gotViews.Views)
})
}
}
Expand Down Expand Up @@ -624,8 +624,8 @@ func TestProcessorMetricViews(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
obsreport.Configure(tt.level)
got := obsreport.ProcessorMetricViews("test_type", legacyViews)
assert.Equal(t, tt.want, got)
got := obsreport.ProcessorMetricViews("test_type", &obsreport.ObsMetrics{Views: legacyViews})
assert.Equal(t, tt.want, got.Views)
})
}
}
Expand Down
3 changes: 2 additions & 1 deletion obsreport/obsreporttest/obsreporttest.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ var (
// SetupRecordedMetricsTest does setup the testing environment to check the metrics recorded by receivers, producers or exporters.
// The returned function should be deferred.
func SetupRecordedMetricsTest() (func(), error) {
views := obsreport.Configure(configtelemetry.LevelNormal)
obsMetrics := obsreport.Configure(configtelemetry.LevelNormal)
views := obsMetrics.Views
err := view.Register(views...)
if err != nil {
return nil, err
Expand Down
14 changes: 8 additions & 6 deletions processor/batchprocessor/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,14 @@ func MetricViews() []*view.View {
1000_000, 2000_000, 3000_000, 4000_000, 5000_000, 6000_000, 7000_000, 8000_000, 9000_000),
}

legacyViews := []*view.View{
countBatchSizeTriggerSendView,
countTimeoutTriggerSendView,
distributionBatchSendSizeView,
distributionBatchSendSizeBytesView,
legacyViews := &obsreport.ObsMetrics{
Views: []*view.View{
countBatchSizeTriggerSendView,
countTimeoutTriggerSendView,
distributionBatchSendSizeView,
distributionBatchSendSizeBytesView,
},
}

return obsreport.ProcessorMetricViews(typeStr, legacyViews)
return obsreport.ProcessorMetricViews(typeStr, legacyViews).Views
}
3 changes: 2 additions & 1 deletion service/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@ func (tel *appTelemetry) init(asyncErrorChannel chan<- error, ballastSizeBytes u
}

var views []*view.View
obsMetrics := obsreport.Configure(level)
views = append(views, batchprocessor.MetricViews()...)
views = append(views, jaegerexporter.MetricViews()...)
views = append(views, kafkareceiver.MetricViews()...)
views = append(views, obsreport.Configure(level)...)
views = append(views, obsMetrics.Views...)
views = append(views, processMetricsViews.Views()...)

tel.views = views
Expand Down

0 comments on commit ea7fcbb

Please sign in to comment.