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

Change batch processor to use otel instead of oc #4489

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 37 additions & 19 deletions processor/batchprocessor/batch_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,18 @@ import (
"sync"
"time"

"go.opencensus.io/stats"
"go.opencensus.io/tag"
"go.uber.org/zap"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configtelemetry"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/internal/obsreportconfig/obsmetrics"
"go.opentelemetry.io/collector/model/otlp"
"go.opentelemetry.io/collector/model/pdata"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/unit"
"go.opentelemetry.io/otel/trace"
)

// batch_processor is a component that accepts spans and metrics, places them
Expand All @@ -40,8 +43,6 @@ import (
// - batch size reaches cfg.SendBatchSize
// - cfg.Timeout is elapsed since the timestamp when the previous batch was sent out.
type batchProcessor struct {
logger *zap.Logger
exportCtx context.Context
timer *time.Timer
timeout time.Duration
sendBatchSize int
Expand All @@ -53,7 +54,16 @@ type batchProcessor struct {
shutdownC chan struct{}
goroutines sync.WaitGroup

telemetryLevel configtelemetry.Level
exportCtx context.Context
logger *zap.Logger
meter metric.MeterMust
tracer trace.Tracer
telemetryLevel configtelemetry.Level
telemetryLabels []attribute.KeyValue
batchSizeTriggerSend metric.Int64Counter
timeoutTriggerSend metric.Int64Counter
batchSendSize metric.Int64Histogram
batchSendSizeBytes metric.Int64Histogram
}

type batch interface {
Expand All @@ -75,21 +85,28 @@ var _ consumer.Metrics = (*batchProcessor)(nil)
var _ consumer.Logs = (*batchProcessor)(nil)

func newBatchProcessor(set component.ProcessorCreateSettings, cfg *Config, batch batch, telemetryLevel configtelemetry.Level) (*batchProcessor, error) {
exportCtx, err := tag.New(context.Background(), tag.Insert(processorTagKey, cfg.ID().String()))
if err != nil {
return nil, err
}
return &batchProcessor{
logger: set.Logger,
exportCtx: exportCtx,
telemetryLevel: telemetryLevel,
processorTagKey := attribute.Key(obsmetrics.ProcessorKey)
logger := set.Logger
meter := metric.Must(set.MeterProvider.Meter(cfg.ID().Name()))
tracer := set.TracerProvider.Tracer(cfg.ID().Name())

return &batchProcessor{
sendBatchSize: int(cfg.SendBatchSize),
sendBatchMaxSize: int(cfg.SendBatchMaxSize),
timeout: cfg.Timeout,
newItem: make(chan interface{}, runtime.NumCPU()),
batch: batch,
shutdownC: make(chan struct{}, 1),

logger: logger,
meter: meter,
tracer: tracer,
telemetryLevel: telemetryLevel,
telemetryLabels: []attribute.KeyValue{processorTagKey.String(cfg.ID().String())},
batchSizeTriggerSend: meter.NewInt64Counter("batch_size_trigger_send", metric.WithDescription("Number of times the batch was sent due to a size trigger")),
timeoutTriggerSend: meter.NewInt64Counter("timeout_trigger_send", metric.WithDescription("Number of times the batch was sent due to a timeout trigger")),
batchSendSize: meter.NewInt64Histogram("batch_send_size", metric.WithDescription("Number of units in the batch")),
batchSendSizeBytes: meter.NewInt64Histogram("batch_send_size_bytes", metric.WithDescription("Number of bytes in batch that was sent"), metric.WithUnit(unit.Bytes)),
}, nil
}

Expand Down Expand Up @@ -132,7 +149,7 @@ func (bp *batchProcessor) startProcessingCycle() {
if bp.batch.itemCount() > 0 {
// TODO: Set a timeout on sendTraces or
// make it cancellable using the context that Shutdown gets as a parameter
bp.sendItems(statTimeoutTriggerSend)
bp.sendItems(bp.timeoutTriggerSend)
}
return
case item := <-bp.newItem:
Expand All @@ -142,7 +159,7 @@ func (bp *batchProcessor) startProcessingCycle() {
bp.processItem(item)
case <-bp.timer.C:
if bp.batch.itemCount() > 0 {
bp.sendItems(statTimeoutTriggerSend)
bp.sendItems(bp.timeoutTriggerSend)
}
bp.resetTimer()
}
Expand All @@ -154,7 +171,7 @@ func (bp *batchProcessor) processItem(item interface{}) {
sent := false
for bp.batch.itemCount() >= bp.sendBatchSize {
sent = true
bp.sendItems(statBatchSizeTriggerSend)
bp.sendItems(bp.batchSizeTriggerSend)
}

if sent {
Expand All @@ -173,12 +190,13 @@ func (bp *batchProcessor) resetTimer() {
bp.timer.Reset(bp.timeout)
}

func (bp *batchProcessor) sendItems(triggerMeasure *stats.Int64Measure) {
func (bp *batchProcessor) sendItems(triggerMeasure metric.Int64Counter) {
// Add that it came form the trace pipeline?
stats.Record(bp.exportCtx, triggerMeasure.M(1), statBatchSendSize.M(int64(bp.batch.itemCount())))
triggerMeasure.Add(bp.exportCtx, int64(1))
bp.batchSendSize.Record(bp.exportCtx, int64(bp.batch.itemCount()))

if bp.telemetryLevel == configtelemetry.LevelDetailed {
stats.Record(bp.exportCtx, statBatchSendSizeBytes.M(int64(bp.batch.size())))
bp.batchSendSizeBytes.Record(bp.exportCtx, int64(bp.batch.size()))
}

if err := bp.batch.export(bp.exportCtx, bp.sendBatchMaxSize); err != nil {
Expand Down
3 changes: 0 additions & 3 deletions processor/batchprocessor/batch_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,6 @@ func TestBatchProcessorSpansDeliveredEnforceBatchSize(t *testing.T) {

func TestBatchProcessorSentBySize(t *testing.T) {
sizer := otlp.NewProtobufTracesMarshaler().(pdata.TracesSizer)
views := MetricViews()
require.NoError(t, view.Register(views...))
defer view.Unregister(views...)

sink := new(consumertest.TracesSink)
cfg := createDefaultConfig().(*Config)
Expand Down
78 changes: 0 additions & 78 deletions processor/batchprocessor/metrics.go

This file was deleted.

34 changes: 0 additions & 34 deletions processor/batchprocessor/metrics_test.go

This file was deleted.

2 changes: 0 additions & 2 deletions service/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (
"go.opentelemetry.io/collector/internal/obsreportconfig"
"go.opentelemetry.io/collector/internal/version"
semconv "go.opentelemetry.io/collector/model/semconv/v1.5.0"
"go.opentelemetry.io/collector/processor/batchprocessor"
telemetry2 "go.opentelemetry.io/collector/service/internal/telemetry"
)

Expand Down Expand Up @@ -147,7 +146,6 @@ func (tel *colTelemetry) initOpenCensus(level configtelemetry.Level, instanceID

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

Expand Down