github.com/observiq/bindplane-agent@v1.51.0/internal/throughputwrapper/trace_consumer.go (about) 1 // Copyright observIQ, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package throughputwrapper 16 17 import ( 18 "context" 19 20 "go.opencensus.io/stats" 21 "go.opencensus.io/tag" 22 "go.opentelemetry.io/collector/consumer" 23 "go.opentelemetry.io/collector/pdata/ptrace" 24 "go.uber.org/zap" 25 ) 26 27 var _ consumer.Traces = (*traceConsumer)(nil) 28 29 type traceConsumer struct { 30 logger *zap.Logger 31 mutators []tag.Mutator 32 tracesSizer ptrace.MarshalSizer 33 baseConsumer consumer.Traces 34 } 35 36 func newTraceConsumer(logger *zap.Logger, componentID string, baseConsumer consumer.Traces) *traceConsumer { 37 return &traceConsumer{ 38 logger: logger, 39 mutators: []tag.Mutator{tag.Upsert(componentTagKey, componentID, tag.WithTTL(tag.TTLNoPropagation))}, 40 tracesSizer: &ptrace.ProtoMarshaler{}, 41 baseConsumer: baseConsumer, 42 } 43 } 44 45 // ConsumeTraces measures the ptrace.Traces size before passing it onto the baseConsumer 46 func (t *traceConsumer) ConsumeTraces(ctx context.Context, td ptrace.Traces) error { 47 if err := stats.RecordWithTags( 48 ctx, 49 t.mutators, 50 traceThroughputSize.M(int64(t.tracesSizer.TracesSize(td))), 51 ); err != nil { 52 t.logger.Warn("Error while measuring receiver trace throughput", zap.Error(err)) 53 } 54 return t.baseConsumer.ConsumeTraces(ctx, td) 55 } 56 57 // Capabilities returns the baseConsumer's capabilities 58 func (t *traceConsumer) Capabilities() consumer.Capabilities { 59 return t.baseConsumer.Capabilities() 60 }