You've already forked opentelemetry-go
							
							
				mirror of
				https://github.com/open-telemetry/opentelemetry-go.git
				synced 2025-10-31 00:07:40 +02:00 
			
		
		
		
	
		
			
				
	
	
		
			122 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			122 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| // Copyright The OpenTelemetry Authors
 | |
| //
 | |
| // Licensed under the Apache License, Version 2.0 (the "License");
 | |
| // you may not use this file except in compliance with the License.
 | |
| // You may obtain a copy of the License at
 | |
| //
 | |
| //     http://www.apache.org/licenses/LICENSE-2.0
 | |
| //
 | |
| // Unless required by applicable law or agreed to in writing, software
 | |
| // distributed under the License is distributed on an "AS IS" BASIS,
 | |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | |
| // See the License for the specific language governing permissions and
 | |
| // limitations under the License.
 | |
| 
 | |
| package test
 | |
| 
 | |
| import (
 | |
| 	"fmt"
 | |
| 	"strings"
 | |
| 
 | |
| 	"go.opentelemetry.io/otel/api/label"
 | |
| 	"go.opentelemetry.io/otel/api/metric"
 | |
| 	export "go.opentelemetry.io/otel/sdk/export/metric"
 | |
| 	"go.opentelemetry.io/otel/sdk/export/metric/aggregation"
 | |
| 	"go.opentelemetry.io/otel/sdk/metric/aggregator/array"
 | |
| 	"go.opentelemetry.io/otel/sdk/metric/aggregator/ddsketch"
 | |
| 	"go.opentelemetry.io/otel/sdk/metric/aggregator/histogram"
 | |
| 	"go.opentelemetry.io/otel/sdk/metric/aggregator/lastvalue"
 | |
| 	"go.opentelemetry.io/otel/sdk/metric/aggregator/minmaxsumcount"
 | |
| 	"go.opentelemetry.io/otel/sdk/metric/aggregator/sum"
 | |
| )
 | |
| 
 | |
| type (
 | |
| 	// Output collects distinct metric/label set outputs.
 | |
| 	Output struct {
 | |
| 		Map          map[string]float64
 | |
| 		labelEncoder label.Encoder
 | |
| 	}
 | |
| 
 | |
| 	// testAggregationSelector returns aggregators consistent with
 | |
| 	// the test variables below, needed for testing stateful
 | |
| 	// integrators, which clone Aggregators using AggregatorFor(desc).
 | |
| 	testAggregationSelector struct{}
 | |
| )
 | |
| 
 | |
| func NewOutput(labelEncoder label.Encoder) Output {
 | |
| 	return Output{
 | |
| 		Map:          make(map[string]float64),
 | |
| 		labelEncoder: labelEncoder,
 | |
| 	}
 | |
| }
 | |
| 
 | |
| // AggregationSelector returns a policy that is consistent with the
 | |
| // test descriptors above.  I.e., it returns sum.New() for counter
 | |
| // instruments and lastvalue.New() for lastValue instruments.
 | |
| func AggregationSelector() export.AggregationSelector {
 | |
| 	return testAggregationSelector{}
 | |
| }
 | |
| 
 | |
| func (testAggregationSelector) AggregatorFor(desc *metric.Descriptor, aggPtrs ...*export.Aggregator) {
 | |
| 
 | |
| 	switch {
 | |
| 	case strings.HasSuffix(desc.Name(), ".disabled"):
 | |
| 		for i := range aggPtrs {
 | |
| 			*aggPtrs[i] = nil
 | |
| 		}
 | |
| 	case strings.HasSuffix(desc.Name(), ".sum"):
 | |
| 		aggs := sum.New(len(aggPtrs))
 | |
| 		for i := range aggPtrs {
 | |
| 			*aggPtrs[i] = &aggs[i]
 | |
| 		}
 | |
| 	case strings.HasSuffix(desc.Name(), ".minmaxsumcount"):
 | |
| 		aggs := minmaxsumcount.New(len(aggPtrs), desc)
 | |
| 		for i := range aggPtrs {
 | |
| 			*aggPtrs[i] = &aggs[i]
 | |
| 		}
 | |
| 	case strings.HasSuffix(desc.Name(), ".lastvalue"):
 | |
| 		aggs := lastvalue.New(len(aggPtrs))
 | |
| 		for i := range aggPtrs {
 | |
| 			*aggPtrs[i] = &aggs[i]
 | |
| 		}
 | |
| 	case strings.HasSuffix(desc.Name(), ".sketch"):
 | |
| 		aggs := ddsketch.New(len(aggPtrs), desc, ddsketch.NewDefaultConfig())
 | |
| 		for i := range aggPtrs {
 | |
| 			*aggPtrs[i] = &aggs[i]
 | |
| 		}
 | |
| 	case strings.HasSuffix(desc.Name(), ".histogram"):
 | |
| 		aggs := histogram.New(len(aggPtrs), desc, nil)
 | |
| 		for i := range aggPtrs {
 | |
| 			*aggPtrs[i] = &aggs[i]
 | |
| 		}
 | |
| 	case strings.HasSuffix(desc.Name(), ".exact"):
 | |
| 		aggs := array.New(len(aggPtrs))
 | |
| 		for i := range aggPtrs {
 | |
| 			*aggPtrs[i] = &aggs[i]
 | |
| 		}
 | |
| 	default:
 | |
| 		panic(fmt.Sprint("Invalid instrument name for test AggregationSelector: ", desc.Name()))
 | |
| 	}
 | |
| }
 | |
| 
 | |
| // AddTo adds a name/label-encoding entry with the lastValue or counter
 | |
| // value to the output map.
 | |
| func (o Output) AddTo(rec export.Record) error {
 | |
| 	encoded := rec.Labels().Encoded(o.labelEncoder)
 | |
| 	rencoded := rec.Resource().Encoded(o.labelEncoder)
 | |
| 	key := fmt.Sprint(rec.Descriptor().Name(), "/", encoded, "/", rencoded)
 | |
| 	var value float64
 | |
| 
 | |
| 	if s, ok := rec.Aggregator().(aggregation.Sum); ok {
 | |
| 		sum, _ := s.Sum()
 | |
| 		value = sum.CoerceToFloat64(rec.Descriptor().NumberKind())
 | |
| 	} else if l, ok := rec.Aggregator().(aggregation.LastValue); ok {
 | |
| 		last, _, _ := l.LastValue()
 | |
| 		value = last.CoerceToFloat64(rec.Descriptor().NumberKind())
 | |
| 	} else {
 | |
| 		panic(fmt.Sprintf("Unhandled aggregator type: %T", rec.Aggregator()))
 | |
| 	}
 | |
| 	o.Map[key] = value
 | |
| 	return nil
 | |
| }
 |