Adding unit tests to opentelemetry tracing

Refractor newExporter and newTracer, add unit tests to them
This PR is part of issue 7493

Signed-off-by: Tony Fang <nenghui.fang@gmail.com>
This commit is contained in:
Tony Fang
2022-12-05 22:39:31 +00:00
parent 6918432780
commit 8472946799
6 changed files with 489 additions and 24 deletions

View File

@@ -23,6 +23,7 @@ import (
"net/url"
"time"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/log"
"github.com/containerd/containerd/plugin"
"github.com/containerd/containerd/tracing"
@@ -63,7 +64,26 @@ func init() {
Requires: []plugin.Type{plugin.TracingProcessorPlugin},
Config: &TraceConfig{ServiceName: "containerd", TraceSamplingRatio: 1.0},
InitFn: func(ic *plugin.InitContext) (interface{}, error) {
return newTracer(ic)
//get TracingProcessorPlugin which is a dependency
plugins, err := ic.GetByType(plugin.TracingProcessorPlugin)
if err != nil {
return nil, fmt.Errorf("failed to get tracing processors: %w", err)
}
procs := make([]sdktrace.SpanProcessor, 0, len(plugins))
for id, pctx := range plugins {
p, err := pctx.Instance()
if err != nil {
if plugin.IsSkipPlugin(err) {
log.G(ic.Context).WithError(err).Infof("skipping tracing processor initialization (no tracing plugin)")
} else {
log.G(ic.Context).WithError(err).Errorf("failed to initialize a tracing processor %q", id)
}
continue
}
proc := p.(sdktrace.SpanProcessor)
procs = append(procs, proc)
}
return newTracer(ic.Context, ic.Config.(*TraceConfig), procs)
},
})
@@ -105,7 +125,7 @@ func newExporter(ctx context.Context, cfg *OTLPConfig) (*otlptrace.Exporter, err
if cfg.Protocol == "http/protobuf" || cfg.Protocol == "" {
u, err := url.Parse(cfg.Endpoint)
if err != nil {
return nil, fmt.Errorf("OpenTelemetry endpoint %q is invalid: %w", cfg.Endpoint, err)
return nil, fmt.Errorf("OpenTelemetry endpoint %q %w : %v", cfg.Endpoint, errdefs.ErrInvalidArgument, err)
}
opts := []otlptracehttp.Option{
otlptracehttp.WithEndpoint(u.Host),
@@ -124,7 +144,7 @@ func newExporter(ctx context.Context, cfg *OTLPConfig) (*otlptrace.Exporter, err
return otlptracegrpc.New(ctx, opts...)
} else {
// Other protocols such as "http/json" are not supported.
return nil, fmt.Errorf("OpenTelemetry protocol %q is not supported", cfg.Protocol)
return nil, fmt.Errorf("OpenTelemetry protocol %q : %w", cfg.Protocol, errdefs.ErrNotImplemented)
}
}
@@ -132,9 +152,7 @@ func newExporter(ctx context.Context, cfg *OTLPConfig) (*otlptrace.Exporter, err
// its sampling ratio and returns io.Closer.
//
// Note that this function sets process-wide tracing configuration.
func newTracer(ic *plugin.InitContext) (io.Closer, error) {
ctx := ic.Context
config := ic.Config.(*TraceConfig)
func newTracer(ctx context.Context, config *TraceConfig, procs []sdktrace.SpanProcessor) (io.Closer, error) {
res, err := resource.New(ctx,
resource.WithHost(),
@@ -154,25 +172,8 @@ func newTracer(ic *plugin.InitContext) (io.Closer, error) {
sdktrace.WithResource(res),
}
ls, err := ic.GetByType(plugin.TracingProcessorPlugin)
if err != nil {
return nil, fmt.Errorf("failed to get tracing processors: %w", err)
}
procs := make([]sdktrace.SpanProcessor, 0, len(ls))
for id, pctx := range ls {
p, err := pctx.Instance()
if err != nil {
if plugin.IsSkipPlugin(err) {
log.G(ctx).WithError(err).Infof("skipping tracing processor initialization (no tracing plugin)")
} else {
log.G(ctx).WithError(err).Errorf("failed to initialize a tracing processor %q", id)
}
continue
}
proc := p.(sdktrace.SpanProcessor)
for _, proc := range procs {
opts = append(opts, sdktrace.WithSpanProcessor(proc))
procs = append(procs, proc)
}
provider := sdktrace.NewTracerProvider(opts...)
@@ -189,6 +190,7 @@ func newTracer(ic *plugin.InitContext) (io.Closer, error) {
}
return nil
}}, nil
}
// Returns a composite TestMap propagator

118
tracing/plugin/otlp_test.go Normal file
View File

@@ -0,0 +1,118 @@
/*
Copyright The containerd 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 plugin
import (
"context"
"errors"
"testing"
"github.com/containerd/containerd/errdefs"
"go.opentelemetry.io/otel/sdk/trace"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/sdk/trace/tracetest"
)
// TestNewExporter runs tests with different combinations of configuration for NewExporter function
func TestNewExporter(t *testing.T) {
for _, testcase := range []struct {
name string
input OTLPConfig
output error
}{
{
name: "Test http/protobuf protocol, expect no error",
input: OTLPConfig{Endpoint: "http://localhost:4318",
Protocol: "http/protobuf",
Insecure: false},
output: nil,
},
{
name: "Test invalid endpoint, expect ErrInvalidArgument error",
input: OTLPConfig{Endpoint: "http://localhost\n:4318",
Protocol: "http/protobuf",
Insecure: false},
output: errdefs.ErrInvalidArgument,
},
{
name: "Test default protocol, expect no error",
input: OTLPConfig{Endpoint: "http://localhost:4318",
Protocol: "",
Insecure: false},
output: nil,
},
{
name: "Test grpc protocol, expect no error",
input: OTLPConfig{Endpoint: "http://localhost:4317",
Protocol: "grpc",
Insecure: false},
output: nil,
},
{
name: "Test http/json protocol which is not supported, expect not implemented error",
input: OTLPConfig{Endpoint: "http://localhost:4318",
Protocol: "http/json",
Insecure: false},
output: errdefs.ErrNotImplemented,
},
} {
t.Run(testcase.name, func(t *testing.T) {
t.Logf("input: %v", testcase.input)
ctx := context.TODO()
exp, err := newExporter(ctx, &testcase.input)
t.Logf("output: %v", err)
if err == nil {
if err != testcase.output {
t.Fatalf("Expect to get error: %v, however no error got\n", testcase.output)
} else if exp == nil {
t.Fatalf("Something went wrong, Exporter not created as expected\n")
}
} else {
if !errors.Is(err, testcase.output) {
t.Fatalf("Expect to get error: %v, however error %v returned\n", testcase.output, err)
}
}
})
}
}
// TestNewTracer runs test for NewTracer function
func TestNewTracer(t *testing.T) {
config := &TraceConfig{ServiceName: "containerd", TraceSamplingRatio: 1.0}
t.Logf("config: %v", config)
procs := make([]sdktrace.SpanProcessor, 0, 1)
//Create a dummy in memory exporter for test
exp := tracetest.NewInMemoryExporter()
proc := trace.NewBatchSpanProcessor(exp)
procs = append(procs, proc)
ctx := context.TODO()
tracerCloser, err := newTracer(ctx, config, procs)
if err != nil {
t.Fatalf("Something went wrong, Tracer not created as expected\n")
}
defer tracerCloser.Close()
}