...

Source file src/go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc/internal/otlptracetest/collector.go

Documentation: go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc/internal/otlptracetest

     1  // Code created by gotmpl. DO NOT MODIFY.
     2  // source: internal/shared/otlp/otlptrace/otlptracetest/collector.go.tmpl
     3  
     4  // Copyright The OpenTelemetry Authors
     5  //
     6  // Licensed under the Apache License, Version 2.0 (the "License");
     7  // you may not use this file except in compliance with the License.
     8  // You may obtain a copy of the License at
     9  //
    10  //     http://www.apache.org/licenses/LICENSE-2.0
    11  //
    12  // Unless required by applicable law or agreed to in writing, software
    13  // distributed under the License is distributed on an "AS IS" BASIS,
    14  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15  // See the License for the specific language governing permissions and
    16  // limitations under the License.
    17  
    18  package otlptracetest // import "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc/internal/otlptracetest"
    19  
    20  import (
    21  	"sort"
    22  
    23  	collectortracepb "go.opentelemetry.io/proto/otlp/collector/trace/v1"
    24  	commonpb "go.opentelemetry.io/proto/otlp/common/v1"
    25  	resourcepb "go.opentelemetry.io/proto/otlp/resource/v1"
    26  	tracepb "go.opentelemetry.io/proto/otlp/trace/v1"
    27  )
    28  
    29  // TracesCollector mocks a collector for the end-to-end testing.
    30  type TracesCollector interface {
    31  	Stop() error
    32  	GetResourceSpans() []*tracepb.ResourceSpans
    33  }
    34  
    35  // SpansStorage stores the spans. Mock collectors can use it to
    36  // store spans they have received.
    37  type SpansStorage struct {
    38  	rsm       map[string]*tracepb.ResourceSpans
    39  	spanCount int
    40  }
    41  
    42  // NewSpansStorage creates a new spans storage.
    43  func NewSpansStorage() SpansStorage {
    44  	return SpansStorage{
    45  		rsm: make(map[string]*tracepb.ResourceSpans),
    46  	}
    47  }
    48  
    49  // AddSpans adds spans to the spans storage.
    50  func (s *SpansStorage) AddSpans(request *collectortracepb.ExportTraceServiceRequest) {
    51  	for _, rs := range request.GetResourceSpans() {
    52  		rstr := resourceString(rs.Resource)
    53  		if existingRs, ok := s.rsm[rstr]; !ok {
    54  			s.rsm[rstr] = rs
    55  			// TODO (rghetia): Add support for library Info.
    56  			if len(rs.ScopeSpans) == 0 {
    57  				rs.ScopeSpans = []*tracepb.ScopeSpans{
    58  					{
    59  						Spans: []*tracepb.Span{},
    60  					},
    61  				}
    62  			}
    63  			s.spanCount += len(rs.ScopeSpans[0].Spans)
    64  		} else {
    65  			if len(rs.ScopeSpans) > 0 {
    66  				newSpans := rs.ScopeSpans[0].GetSpans()
    67  				existingRs.ScopeSpans[0].Spans = append(existingRs.ScopeSpans[0].Spans, newSpans...)
    68  				s.spanCount += len(newSpans)
    69  			}
    70  		}
    71  	}
    72  }
    73  
    74  // GetSpans returns the stored spans.
    75  func (s *SpansStorage) GetSpans() []*tracepb.Span {
    76  	spans := make([]*tracepb.Span, 0, s.spanCount)
    77  	for _, rs := range s.rsm {
    78  		spans = append(spans, rs.ScopeSpans[0].Spans...)
    79  	}
    80  	return spans
    81  }
    82  
    83  // GetResourceSpans returns the stored resource spans.
    84  func (s *SpansStorage) GetResourceSpans() []*tracepb.ResourceSpans {
    85  	rss := make([]*tracepb.ResourceSpans, 0, len(s.rsm))
    86  	for _, rs := range s.rsm {
    87  		rss = append(rss, rs)
    88  	}
    89  	return rss
    90  }
    91  
    92  func resourceString(res *resourcepb.Resource) string {
    93  	sAttrs := sortedAttributes(res.GetAttributes())
    94  	rstr := ""
    95  	for _, attr := range sAttrs {
    96  		rstr = rstr + attr.String()
    97  	}
    98  	return rstr
    99  }
   100  
   101  func sortedAttributes(attrs []*commonpb.KeyValue) []*commonpb.KeyValue {
   102  	sort.Slice(attrs[:], func(i, j int) bool {
   103  		return attrs[i].Key < attrs[j].Key
   104  	})
   105  	return attrs
   106  }
   107  

View as plain text