2020-03-25 23:47:17 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2020-03-07 08:01:02 +02:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2021-01-12 05:55:24 +02:00
|
|
|
package otlpgrpc_test
|
2020-03-07 08:01:02 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2021-04-17 18:26:06 +02:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
2020-03-07 08:01:02 +02:00
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"google.golang.org/grpc"
|
2020-06-09 21:30:40 +02:00
|
|
|
metadata "google.golang.org/grpc/metadata"
|
2020-03-07 08:01:02 +02:00
|
|
|
|
2021-01-12 05:55:24 +02:00
|
|
|
"go.opentelemetry.io/otel/exporters/otlp/internal/otlptest"
|
2021-03-20 18:25:07 +02:00
|
|
|
collectormetricpb "go.opentelemetry.io/proto/otlp/collector/metrics/v1"
|
|
|
|
collectortracepb "go.opentelemetry.io/proto/otlp/collector/trace/v1"
|
|
|
|
metricpb "go.opentelemetry.io/proto/otlp/metrics/v1"
|
|
|
|
tracepb "go.opentelemetry.io/proto/otlp/trace/v1"
|
2020-03-07 08:01:02 +02:00
|
|
|
)
|
|
|
|
|
2021-04-29 20:07:21 +02:00
|
|
|
func makeMockCollector(t *testing.T, mockConfig *mockConfig) *mockCollector {
|
2020-12-29 21:15:57 +02:00
|
|
|
return &mockCollector{
|
2020-03-13 22:07:36 +02:00
|
|
|
t: t,
|
|
|
|
traceSvc: &mockTraceService{
|
2021-01-12 05:55:24 +02:00
|
|
|
storage: otlptest.NewSpansStorage(),
|
2021-04-29 20:07:21 +02:00
|
|
|
errors: mockConfig.errors,
|
2021-01-12 05:55:24 +02:00
|
|
|
},
|
|
|
|
metricSvc: &mockMetricService{
|
|
|
|
storage: otlptest.NewMetricsStorage(),
|
2020-03-13 22:07:36 +02:00
|
|
|
},
|
2020-03-13 20:42:20 +02:00
|
|
|
}
|
2020-03-07 08:01:02 +02:00
|
|
|
}
|
|
|
|
|
2020-03-13 20:42:20 +02:00
|
|
|
type mockTraceService struct {
|
2021-03-20 18:25:07 +02:00
|
|
|
collectortracepb.UnimplementedTraceServiceServer
|
|
|
|
|
2021-04-29 20:07:21 +02:00
|
|
|
errors []error
|
|
|
|
requests int
|
|
|
|
mu sync.RWMutex
|
|
|
|
storage otlptest.SpansStorage
|
|
|
|
headers metadata.MD
|
|
|
|
delay time.Duration
|
2020-06-09 21:30:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mts *mockTraceService) getHeaders() metadata.MD {
|
2021-01-12 05:55:24 +02:00
|
|
|
mts.mu.RLock()
|
|
|
|
defer mts.mu.RUnlock()
|
2020-06-09 21:30:40 +02:00
|
|
|
return mts.headers
|
2020-03-07 08:01:02 +02:00
|
|
|
}
|
|
|
|
|
2020-03-13 20:42:20 +02:00
|
|
|
func (mts *mockTraceService) getSpans() []*tracepb.Span {
|
|
|
|
mts.mu.RLock()
|
2020-03-13 22:07:36 +02:00
|
|
|
defer mts.mu.RUnlock()
|
2021-01-12 05:55:24 +02:00
|
|
|
return mts.storage.GetSpans()
|
2020-03-13 20:42:20 +02:00
|
|
|
}
|
2020-03-07 08:01:02 +02:00
|
|
|
|
2020-03-13 22:07:36 +02:00
|
|
|
func (mts *mockTraceService) getResourceSpans() []*tracepb.ResourceSpans {
|
|
|
|
mts.mu.RLock()
|
|
|
|
defer mts.mu.RUnlock()
|
2021-01-12 05:55:24 +02:00
|
|
|
return mts.storage.GetResourceSpans()
|
2020-03-13 22:07:36 +02:00
|
|
|
}
|
|
|
|
|
2020-12-29 21:15:57 +02:00
|
|
|
func (mts *mockTraceService) Export(ctx context.Context, exp *collectortracepb.ExportTraceServiceRequest) (*collectortracepb.ExportTraceServiceResponse, error) {
|
2021-04-20 19:02:02 +02:00
|
|
|
if mts.delay > 0 {
|
|
|
|
time.Sleep(mts.delay)
|
|
|
|
}
|
2021-04-29 20:07:21 +02:00
|
|
|
|
2020-03-13 20:42:20 +02:00
|
|
|
mts.mu.Lock()
|
2021-04-29 20:07:21 +02:00
|
|
|
defer func() {
|
|
|
|
mts.requests++
|
|
|
|
mts.mu.Unlock()
|
|
|
|
}()
|
|
|
|
|
|
|
|
reply := &collectortracepb.ExportTraceServiceResponse{}
|
|
|
|
if mts.requests < len(mts.errors) {
|
|
|
|
idx := mts.requests
|
|
|
|
return reply, mts.errors[idx]
|
|
|
|
}
|
|
|
|
|
2021-01-12 05:55:24 +02:00
|
|
|
mts.headers, _ = metadata.FromIncomingContext(ctx)
|
|
|
|
mts.storage.AddSpans(exp)
|
|
|
|
return reply, nil
|
2020-03-13 22:07:36 +02:00
|
|
|
}
|
|
|
|
|
2020-03-13 20:42:20 +02:00
|
|
|
type mockMetricService struct {
|
2021-03-20 18:25:07 +02:00
|
|
|
collectormetricpb.UnimplementedMetricsServiceServer
|
|
|
|
|
2020-03-13 20:42:20 +02:00
|
|
|
mu sync.RWMutex
|
2021-01-12 05:55:24 +02:00
|
|
|
storage otlptest.MetricsStorage
|
2021-04-20 19:02:02 +02:00
|
|
|
delay time.Duration
|
2020-03-13 20:42:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mms *mockMetricService) getMetrics() []*metricpb.Metric {
|
|
|
|
mms.mu.RLock()
|
|
|
|
defer mms.mu.RUnlock()
|
2021-01-12 05:55:24 +02:00
|
|
|
return mms.storage.GetMetrics()
|
2020-03-13 20:42:20 +02:00
|
|
|
}
|
|
|
|
|
2020-12-29 21:15:57 +02:00
|
|
|
func (mms *mockMetricService) Export(ctx context.Context, exp *collectormetricpb.ExportMetricsServiceRequest) (*collectormetricpb.ExportMetricsServiceResponse, error) {
|
2021-04-20 19:02:02 +02:00
|
|
|
if mms.delay > 0 {
|
|
|
|
time.Sleep(mms.delay)
|
|
|
|
}
|
2021-01-12 05:55:24 +02:00
|
|
|
reply := &collectormetricpb.ExportMetricsServiceResponse{}
|
2020-03-13 20:42:20 +02:00
|
|
|
mms.mu.Lock()
|
2021-01-12 05:55:24 +02:00
|
|
|
defer mms.mu.Unlock()
|
|
|
|
mms.storage.AddMetrics(exp)
|
|
|
|
return reply, nil
|
2020-03-13 20:42:20 +02:00
|
|
|
}
|
|
|
|
|
2020-12-29 21:15:57 +02:00
|
|
|
type mockCollector struct {
|
2020-03-13 20:42:20 +02:00
|
|
|
t *testing.T
|
|
|
|
|
|
|
|
traceSvc *mockTraceService
|
|
|
|
metricSvc *mockMetricService
|
|
|
|
|
2020-12-29 21:15:57 +02:00
|
|
|
endpoint string
|
2021-04-17 18:26:06 +02:00
|
|
|
ln *listener
|
|
|
|
stopFunc func()
|
2020-03-13 20:42:20 +02:00
|
|
|
stopOnce sync.Once
|
|
|
|
}
|
|
|
|
|
2021-04-29 20:07:21 +02:00
|
|
|
type mockConfig struct {
|
|
|
|
errors []error
|
|
|
|
endpoint string
|
|
|
|
}
|
|
|
|
|
2020-12-29 21:15:57 +02:00
|
|
|
var _ collectortracepb.TraceServiceServer = (*mockTraceService)(nil)
|
|
|
|
var _ collectormetricpb.MetricsServiceServer = (*mockMetricService)(nil)
|
2020-03-13 20:42:20 +02:00
|
|
|
|
2020-03-07 08:01:02 +02:00
|
|
|
var errAlreadyStopped = fmt.Errorf("already stopped")
|
|
|
|
|
2020-12-29 21:15:57 +02:00
|
|
|
func (mc *mockCollector) stop() error {
|
2020-03-07 08:01:02 +02:00
|
|
|
var err = errAlreadyStopped
|
|
|
|
mc.stopOnce.Do(func() {
|
2021-04-17 18:26:06 +02:00
|
|
|
err = nil
|
2020-03-07 08:01:02 +02:00
|
|
|
if mc.stopFunc != nil {
|
2021-04-17 18:26:06 +02:00
|
|
|
mc.stopFunc()
|
2020-03-07 08:01:02 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
// Give it sometime to shutdown.
|
|
|
|
<-time.After(160 * time.Millisecond)
|
2020-03-13 20:42:20 +02:00
|
|
|
|
|
|
|
// Wait for services to finish reading/writing.
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
// Getting the lock ensures the traceSvc is done flushing.
|
|
|
|
mc.traceSvc.mu.Lock()
|
|
|
|
defer mc.traceSvc.mu.Unlock()
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
// Getting the lock ensures the metricSvc is done flushing.
|
|
|
|
mc.metricSvc.mu.Lock()
|
|
|
|
defer mc.metricSvc.mu.Unlock()
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
wg.Wait()
|
2020-03-07 08:01:02 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-12 05:55:24 +02:00
|
|
|
func (mc *mockCollector) Stop() error {
|
|
|
|
return mc.stop()
|
|
|
|
}
|
|
|
|
|
2020-12-29 21:15:57 +02:00
|
|
|
func (mc *mockCollector) getSpans() []*tracepb.Span {
|
2020-03-13 20:42:20 +02:00
|
|
|
return mc.traceSvc.getSpans()
|
|
|
|
}
|
|
|
|
|
2020-12-29 21:15:57 +02:00
|
|
|
func (mc *mockCollector) getResourceSpans() []*tracepb.ResourceSpans {
|
2020-03-13 22:07:36 +02:00
|
|
|
return mc.traceSvc.getResourceSpans()
|
|
|
|
}
|
|
|
|
|
2021-01-12 05:55:24 +02:00
|
|
|
func (mc *mockCollector) GetResourceSpans() []*tracepb.ResourceSpans {
|
|
|
|
return mc.getResourceSpans()
|
|
|
|
}
|
|
|
|
|
2020-12-29 21:15:57 +02:00
|
|
|
func (mc *mockCollector) getHeaders() metadata.MD {
|
2020-06-09 21:30:40 +02:00
|
|
|
return mc.traceSvc.getHeaders()
|
|
|
|
}
|
|
|
|
|
2020-12-29 21:15:57 +02:00
|
|
|
func (mc *mockCollector) getMetrics() []*metricpb.Metric {
|
2020-03-13 20:42:20 +02:00
|
|
|
return mc.metricSvc.getMetrics()
|
|
|
|
}
|
|
|
|
|
2021-01-12 05:55:24 +02:00
|
|
|
func (mc *mockCollector) GetMetrics() []*metricpb.Metric {
|
|
|
|
return mc.getMetrics()
|
|
|
|
}
|
|
|
|
|
2020-12-29 21:15:57 +02:00
|
|
|
// runMockCollector is a helper function to create a mock Collector
|
|
|
|
func runMockCollector(t *testing.T) *mockCollector {
|
|
|
|
return runMockCollectorAtEndpoint(t, "localhost:0")
|
2020-03-07 08:01:02 +02:00
|
|
|
}
|
|
|
|
|
2020-12-29 21:15:57 +02:00
|
|
|
func runMockCollectorAtEndpoint(t *testing.T, endpoint string) *mockCollector {
|
2021-04-29 20:07:21 +02:00
|
|
|
return runMockCollectorWithConfig(t, &mockConfig{endpoint: endpoint})
|
|
|
|
}
|
|
|
|
|
|
|
|
func runMockCollectorWithConfig(t *testing.T, mockConfig *mockConfig) *mockCollector {
|
|
|
|
ln, err := net.Listen("tcp", mockConfig.endpoint)
|
2020-03-07 08:01:02 +02:00
|
|
|
if err != nil {
|
2020-12-29 21:15:57 +02:00
|
|
|
t.Fatalf("Failed to get an endpoint: %v", err)
|
2020-03-07 08:01:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
srv := grpc.NewServer()
|
2021-04-29 20:07:21 +02:00
|
|
|
mc := makeMockCollector(t, mockConfig)
|
2020-12-29 21:15:57 +02:00
|
|
|
collectortracepb.RegisterTraceServiceServer(srv, mc.traceSvc)
|
|
|
|
collectormetricpb.RegisterMetricsServiceServer(srv, mc.metricSvc)
|
2021-04-17 18:26:06 +02:00
|
|
|
mc.ln = newListener(ln)
|
2020-03-07 08:01:02 +02:00
|
|
|
go func() {
|
2021-04-17 18:26:06 +02:00
|
|
|
_ = srv.Serve((net.Listener)(mc.ln))
|
2020-03-07 08:01:02 +02:00
|
|
|
}()
|
|
|
|
|
2021-04-17 18:26:06 +02:00
|
|
|
mc.endpoint = ln.Addr().String()
|
|
|
|
// srv.Stop calls Close on mc.ln.
|
|
|
|
mc.stopFunc = srv.Stop
|
|
|
|
|
|
|
|
return mc
|
|
|
|
}
|
|
|
|
|
|
|
|
type listener struct {
|
|
|
|
closeOnce sync.Once
|
|
|
|
wrapped net.Listener
|
|
|
|
C chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newListener(wrapped net.Listener) *listener {
|
|
|
|
return &listener{
|
|
|
|
wrapped: wrapped,
|
|
|
|
C: make(chan struct{}, 1),
|
2020-03-07 08:01:02 +02:00
|
|
|
}
|
2021-04-17 18:26:06 +02:00
|
|
|
}
|
2020-03-07 08:01:02 +02:00
|
|
|
|
2021-04-17 18:26:06 +02:00
|
|
|
func (l *listener) Close() error { return l.wrapped.Close() }
|
2020-03-07 08:01:02 +02:00
|
|
|
|
2021-04-17 18:26:06 +02:00
|
|
|
func (l *listener) Addr() net.Addr { return l.wrapped.Addr() }
|
2020-03-07 08:01:02 +02:00
|
|
|
|
2021-04-17 18:26:06 +02:00
|
|
|
// Accept waits for and returns the next connection to the listener. It will
|
|
|
|
// send a signal on l.C that a connection has been made before returning.
|
|
|
|
func (l *listener) Accept() (net.Conn, error) {
|
|
|
|
conn, err := l.wrapped.Accept()
|
|
|
|
if err != nil {
|
|
|
|
// Go 1.16 exported net.ErrClosed that could clean up this check, but to
|
|
|
|
// remain backwards compatible with previous versions of Go that we
|
|
|
|
// support the following string evaluation is used instead to keep in line
|
|
|
|
// with the previously recommended way to check this:
|
|
|
|
// https://github.com/golang/go/issues/4373#issuecomment-353076799
|
|
|
|
if strings.Contains(err.Error(), "use of closed network connection") {
|
|
|
|
// If the listener has been closed, do not allow callers of
|
|
|
|
// WaitForConn to wait for a connection that will never come.
|
|
|
|
l.closeOnce.Do(func() { close(l.C) })
|
|
|
|
}
|
|
|
|
return conn, err
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case l.C <- struct{}{}:
|
|
|
|
default:
|
|
|
|
// If C is full, assume nobody is listening and move on.
|
|
|
|
}
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WaitForConn will wait indefintely for a connection to be estabilished with
|
|
|
|
// the listener before returning.
|
|
|
|
func (l *listener) WaitForConn() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-l.C:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
runtime.Gosched()
|
|
|
|
}
|
|
|
|
}
|
2020-03-07 08:01:02 +02:00
|
|
|
}
|