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