github.com/observiq/bindplane-agent@v1.51.0/internal/throughputwrapper/log_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/plog" 24 "go.uber.org/zap" 25 ) 26 27 var _ consumer.Logs = (*logConsumer)(nil) 28 29 type logConsumer struct { 30 logger *zap.Logger 31 mutators []tag.Mutator 32 logsSizer plog.MarshalSizer 33 baseConsumer consumer.Logs 34 } 35 36 func newLogConsumer(logger *zap.Logger, componentID string, baseConsumer consumer.Logs) *logConsumer { 37 return &logConsumer{ 38 logger: logger, 39 mutators: []tag.Mutator{tag.Upsert(componentTagKey, componentID, tag.WithTTL(tag.TTLNoPropagation))}, 40 logsSizer: &plog.ProtoMarshaler{}, 41 baseConsumer: baseConsumer, 42 } 43 } 44 45 // ConsumeLogs measures the plog.Logs size before passing it onto the baseConsumer 46 func (l *logConsumer) ConsumeLogs(ctx context.Context, ld plog.Logs) error { 47 if err := stats.RecordWithTags( 48 ctx, 49 l.mutators, 50 logThroughputSize.M(int64(l.logsSizer.LogsSize(ld))), 51 ); err != nil { 52 l.logger.Warn("Error while measuring receiver log throughput", zap.Error(err)) 53 } 54 return l.baseConsumer.ConsumeLogs(ctx, ld) 55 } 56 57 // Capabilities returns the baseConsumer's capabilities 58 func (l *logConsumer) Capabilities() consumer.Capabilities { 59 return l.baseConsumer.Capabilities() 60 }