1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-09-16 09:26:25 +02:00

Remove tests

The lack of programmatic enablement of retries means testing is going to
be flaky or cumbersome.
This commit is contained in:
Tyler Yahn
2020-05-29 10:23:01 -07:00
parent 45a679730d
commit 2b1998f1b4
4 changed files with 13 additions and 393 deletions

View File

@@ -23,7 +23,6 @@ import (
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
metricpb "github.com/open-telemetry/opentelemetry-proto/gen/go/metrics/v1"
@@ -31,42 +30,14 @@ import (
"go.opentelemetry.io/otel/api/metric"
metricapi "go.opentelemetry.io/otel/api/metric"
"go.opentelemetry.io/otel/exporters/otlp"
otlp_testing "go.opentelemetry.io/otel/exporters/otlp/testing"
exporttrace "go.opentelemetry.io/otel/sdk/export/trace"
"go.opentelemetry.io/otel/sdk/metric/controller/push"
integrator "go.opentelemetry.io/otel/sdk/metric/integrator/simple"
"go.opentelemetry.io/otel/sdk/metric/selector/simple"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
)
func TestIntegrationTestSuite(t *testing.T) {
/*
standard := &TraceIntegrationTestSuite{}
suite.Run(t, standard)
*/
// Default retry policy should ensure these succeed.
retry := &TraceIntegrationTestSuite{
TraceSuite: otlp_testing.TraceSuite{
ServerSuite: otlp_testing.ServerSuite{
FailureModulo: 5,
},
},
}
suite.Run(t, retry)
}
type TraceIntegrationTestSuite struct {
otlp_testing.TraceSuite
}
func (s *TraceIntegrationTestSuite) TestSpanExport() {
tracer := s.TraceProvider.Tracer("test")
_, span := tracer.Start(context.Background(), "test/span")
span.End()
s.Len(s.GetResourceSpans(), 1)
}
func TestNewExporter_endToEnd(t *testing.T) {
tests := []struct {
name string
@@ -85,7 +56,7 @@ func TestNewExporter_endToEnd(t *testing.T) {
}
func newExporterEndToEndTest(t *testing.T, additionalOpts []otlp.ExporterOption) {
mc := runMockColAtAddr(t, "127.0.0.1:56561")
mc := runMockColAtAddr(t, "localhost:56561")
defer func() {
_ = mc.stop()
@@ -116,13 +87,17 @@ func newExporterEndToEndTest(t *testing.T, additionalOpts []otlp.ExporterOption)
),
}
tp1, err := sdktrace.NewProvider(append(pOpts,
sdktrace.WithResourceAttributes(kv.String("rk1", "rv11)"),
kv.Int64("rk2", 5)))...)
sdktrace.WithResource(resource.New(
kv.String("rk1", "rv11)"),
kv.Int64("rk2", 5),
)))...)
assert.NoError(t, err)
tp2, err := sdktrace.NewProvider(append(pOpts,
sdktrace.WithResourceAttributes(kv.String("rk1", "rv12)"),
kv.Float32("rk3", 6.5)))...)
sdktrace.WithResource(resource.New(
kv.String("rk1", "rv12)"),
kv.Float32("rk3", 6.5),
)))...)
assert.NoError(t, err)
tr1 := tp1.Tracer("test-tracer1")
@@ -187,12 +162,12 @@ func newExporterEndToEndTest(t *testing.T, additionalOpts []otlp.ExporterOption)
callback := func(v int64) metricapi.Int64ObserverCallback {
return metricapi.Int64ObserverCallback(func(_ context.Context, result metricapi.Int64ObserverResult) { result.Observe(v, labels...) })
}(data.val)
metricapi.Must(meter).RegisterInt64ValueObserver(name, callback)
metricapi.Must(meter).NewInt64ValueObserver(name, callback)
case metricapi.Float64NumberKind:
callback := func(v float64) metricapi.Float64ObserverCallback {
return metricapi.Float64ObserverCallback(func(_ context.Context, result metricapi.Float64ObserverResult) { result.Observe(v, labels...) })
}(float64(data.val))
metricapi.Must(meter).RegisterFloat64ValueObserver(name, callback)
metricapi.Must(meter).NewFloat64ValueObserver(name, callback)
default:
assert.Failf(t, "unsupported number testing kind", data.nKind.String())
}
@@ -334,9 +309,7 @@ func TestNewExporter_collectorConnectionDiesThenReconnects(t *testing.T) {
reconnectionPeriod := 20 * time.Millisecond
exp, err := otlp.NewExporter(otlp.WithInsecure(),
otlp.WithAddress(mc.address),
otlp.WithReconnectionPeriod(reconnectionPeriod),
otlp.WithGRPCServiceConfig(""),
)
otlp.WithReconnectionPeriod(reconnectionPeriod))
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

View File

@@ -1,85 +0,0 @@
// 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 otlp_testing
import (
"context"
"sync"
colmetricpb "github.com/open-telemetry/opentelemetry-proto/gen/go/collector/metrics/v1"
metricpb "github.com/open-telemetry/opentelemetry-proto/gen/go/metrics/v1"
"go.opentelemetry.io/otel/api/metric"
"go.opentelemetry.io/otel/exporters/otlp"
"go.opentelemetry.io/otel/sdk/metric/controller/push"
integrator "go.opentelemetry.io/otel/sdk/metric/integrator/simple"
"go.opentelemetry.io/otel/sdk/metric/selector/simple"
)
type MetricSuite struct {
ServerSuite
MetricProvider metric.Provider
metricController *push.Controller
mu sync.RWMutex
metrics []*metricpb.Metric
}
func (ms *MetricSuite) SetupSuite() {
ms.ServerSuite.SetupSuite()
colmetricpb.RegisterMetricsServiceServer(ms.Server, ms)
ms.ServerSuite.StartServer()
if ms.MetricProvider == nil {
ms.metricController = ms.NewPushController(ms.Exporter, nil)
ms.metricController.SetErrorHandler(func(err error) {
ms.T().Errorf("testing push controller: %w", err)
})
ms.metricController.Start()
ms.MetricProvider = ms.metricController.Provider()
}
}
func (ms *MetricSuite) NewPushController(exp *otlp.Exporter, opts []push.Option) *push.Controller {
integrator := integrator.New(simple.NewWithExactDistribution(), true)
pusher := push.New(integrator, exp, opts...)
return pusher
}
func (ms *MetricSuite) GetMetrics() []*metricpb.Metric {
// copy in order to not change.
m := make([]*metricpb.Metric, 0, len(ms.metrics))
ms.mu.RLock()
defer ms.mu.RUnlock()
return append(m, ms.metrics...)
}
func (ms *MetricSuite) Export(ctx context.Context, exp *colmetricpb.ExportMetricsServiceRequest) (*colmetricpb.ExportMetricsServiceResponse, error) {
ms.mu.Lock()
for _, rm := range exp.GetResourceMetrics() {
// TODO (rghetia) handle multiple resource and library info.
if len(rm.InstrumentationLibraryMetrics) > 0 {
ms.metrics = append(ms.metrics, rm.InstrumentationLibraryMetrics[0].Metrics...)
}
}
ms.mu.Unlock()
return &colmetricpb.ExportMetricsServiceResponse{}, nil
}
func (ms *MetricSuite) TearDownSuite() {
ms.metricController.Stop()
ms.ServerSuite.TearDownSuite()
}

View File

@@ -1,128 +0,0 @@
// 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 otlp_testing
import (
"net"
"sync/atomic"
"time"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"go.opentelemetry.io/otel/exporters/otlp"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// Let the system define the port
const defaultServerAddr = "127.0.0.1:0"
type ServerSuite struct {
suite.Suite
ServerOpts []grpc.ServerOption
serverAddr string
ServerListener net.Listener
Server *grpc.Server
requestCount uint64
FailureModulo uint64
FailureCodes []codes.Code
ExporterOpts []otlp.ExporterOption
Exporter *otlp.Exporter
}
func (s *ServerSuite) SetupSuite() {
s.serverAddr = defaultServerAddr
var err error
s.ServerListener, err = net.Listen("tcp", s.serverAddr)
s.serverAddr = s.ServerListener.Addr().String()
require.NoError(s.T(), err, "failed to allocate a port for server")
s.Server = grpc.NewServer(s.ServerOpts...)
}
func (s *ServerSuite) StartServer() {
go func() {
s.Server.Serve(s.ServerListener)
}()
s.T().Logf("started grpc.Server at: %v", s.ServerAddr())
if s.Exporter == nil {
s.Exporter = s.NewExporter()
}
}
func (s *ServerSuite) ServerAddr() string {
return s.serverAddr
}
func (s *ServerSuite) RequestError() error {
if s.FailureModulo <= 0 {
return nil
}
count := atomic.AddUint64(&s.requestCount, 1)
if count%s.FailureModulo == 0 {
return nil
}
var c codes.Code
if n := len(s.FailureCodes); n > 0 {
/* Example to understand the indexing:
* - s.FailureModulo = 3
* - len(s.Codes) 5
*
* count - 1 | count / s.FailureModulo | index (mod 5)
* ===================================================
* 0 | 0 | 0
* 1 | 0 | 1
* 2 | 1 | n/a (2)
* 3 | 1 | 2
* 4 | 1 | 3
* 5 | 2 | n/a (3)
* 6 | 2 | 4
* 7 | 2 | 0
* 8 | 3 | n/a (0)
* 9 | 3 | 1
*/
c = s.FailureCodes[(count-1-(count/s.FailureModulo))%uint64(n)]
} else {
c = codes.Unavailable
}
return status.Errorf(c, "artificial error: count %d, modulo %d", count, s.FailureModulo)
}
func (s *ServerSuite) NewExporter() *otlp.Exporter {
opts := []otlp.ExporterOption{
otlp.WithInsecure(),
otlp.WithAddress(s.serverAddr),
otlp.WithReconnectionPeriod(10 * time.Millisecond),
}
exp, err := otlp.NewExporter(append(opts, s.ExporterOpts...)...)
require.NoError(s.T(), err, "failed to create exporter")
return exp
}
func (s *ServerSuite) TearDownSuite() {
if s.ServerListener != nil {
s.Server.GracefulStop()
s.T().Logf("stopped grpc.Server at: %v", s.ServerAddr())
}
s.Exporter.Stop()
}

View File

@@ -1,140 +0,0 @@
// 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 otlp_testing
import (
"context"
"sort"
"sync"
coltracepb "github.com/open-telemetry/opentelemetry-proto/gen/go/collector/trace/v1"
commonpb "github.com/open-telemetry/opentelemetry-proto/gen/go/common/v1"
resourcepb "github.com/open-telemetry/opentelemetry-proto/gen/go/resource/v1"
tracepb "github.com/open-telemetry/opentelemetry-proto/gen/go/trace/v1"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/exporters/otlp"
"go.opentelemetry.io/otel/sdk/trace"
)
type TraceSuite struct {
ServerSuite
TraceProvider *trace.Provider
mu sync.RWMutex
resourceSpanMap map[string]*tracepb.ResourceSpans
}
func (ts *TraceSuite) SetupSuite() {
ts.ServerSuite.SetupSuite()
ts.Reset()
coltracepb.RegisterTraceServiceServer(ts.Server, ts)
ts.ServerSuite.StartServer()
if ts.TraceProvider == nil {
ts.TraceProvider = ts.NewTraceProvider(ts.Exporter, nil)
}
}
func (ts *TraceSuite) NewTraceProvider(exp *otlp.Exporter, opts []trace.ProviderOption) *trace.Provider {
defaultOpts := []trace.ProviderOption{
trace.WithConfig(
trace.Config{
DefaultSampler: trace.AlwaysSample(),
},
),
trace.WithSyncer(exp),
}
p, err := trace.NewProvider(append(defaultOpts, opts...)...)
require.NoError(ts.T(), err, "failed to create trace provider")
return p
}
func (ts *TraceSuite) Reset() {
ts.resourceSpanMap = map[string]*tracepb.ResourceSpans{}
}
func (ts *TraceSuite) GetSpans() []*tracepb.Span {
ts.mu.RLock()
defer ts.mu.RUnlock()
spans := []*tracepb.Span{}
for _, rs := range ts.resourceSpanMap {
for _, ils := range rs.InstrumentationLibrarySpans {
spans = append(spans, ils.Spans...)
}
}
return spans
}
func (ts *TraceSuite) GetResourceSpans() []*tracepb.ResourceSpans {
ts.mu.RLock()
defer ts.mu.RUnlock()
rss := make([]*tracepb.ResourceSpans, 0, len(ts.resourceSpanMap))
for _, rs := range ts.resourceSpanMap {
rss = append(rss, rs)
}
return rss
}
func (ts *TraceSuite) Export(ctx context.Context, req *coltracepb.ExportTraceServiceRequest) (*coltracepb.ExportTraceServiceResponse, error) {
if err := ts.RequestError(); err != nil {
return nil, err
}
ts.mu.Lock()
defer ts.mu.Unlock()
rss := req.GetResourceSpans()
for _, rs := range rss {
rstr := resourceString(rs.Resource)
existingRs, ok := ts.resourceSpanMap[rstr]
if !ok {
ts.resourceSpanMap[rstr] = rs
// TODO (rghetia): Add support for library Info.
if len(rs.InstrumentationLibrarySpans) == 0 {
rs.InstrumentationLibrarySpans = []*tracepb.InstrumentationLibrarySpans{
{
Spans: []*tracepb.Span{},
},
}
}
} else {
if len(rs.InstrumentationLibrarySpans) > 0 {
existingRs.InstrumentationLibrarySpans[0].Spans =
append(existingRs.InstrumentationLibrarySpans[0].Spans,
rs.InstrumentationLibrarySpans[0].GetSpans()...)
}
}
}
return &coltracepb.ExportTraceServiceResponse{}, nil
}
func resourceString(res *resourcepb.Resource) string {
sAttrs := sortedAttributes(res.GetAttributes())
rstr := ""
for _, attr := range sAttrs {
rstr = rstr + attr.String()
}
return rstr
}
func sortedAttributes(attrs []*commonpb.AttributeKeyValue) []*commonpb.AttributeKeyValue {
sort.Slice(attrs[:], func(i, j int) bool {
return attrs[i].Key < attrs[j].Key
})
return attrs
}