Skip to content

Commit

Permalink
Adding unit tests to opentelemetry tracing
Browse files Browse the repository at this point in the history
Refractor newExporter and newTracer, add unit tests to them
This PR is part of issue 7493

Signed-off-by: Tony Fang <[email protected]>
(cherry picked from commit 8472946)
Signed-off-by: Paweł Gronowski <[email protected]>
  • Loading branch information
fangn2 authored and vvoland committed Apr 19, 2024
1 parent c475b9f commit c80347e
Show file tree
Hide file tree
Showing 6 changed files with 489 additions and 19 deletions.
44 changes: 25 additions & 19 deletions tracing/plugin/otlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"net/url"
"time"

"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/log"
"github.com/containerd/containerd/plugin"
"go.opentelemetry.io/otel"
Expand Down Expand Up @@ -61,7 +62,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)
},
})
}
Expand Down Expand Up @@ -100,7 +120,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),
Expand All @@ -126,9 +146,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.WithAttributes(
Expand All @@ -145,21 +163,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 {
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...)
Expand All @@ -175,4 +180,5 @@ func newTracer(ic *plugin.InitContext) (io.Closer, error) {
}
return nil
}}, nil

}
118 changes: 118 additions & 0 deletions tracing/plugin/otlp_test.go
Original file line number Diff line number Diff line change
@@ -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:https://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:https://localhost:4318",
Protocol: "http/protobuf",
Insecure: false},
output: nil,
},
{
name: "Test invalid endpoint, expect ErrInvalidArgument error",
input: OTLPConfig{Endpoint: "http:https://localhost\n:4318",
Protocol: "http/protobuf",
Insecure: false},
output: errdefs.ErrInvalidArgument,
},
{
name: "Test default protocol, expect no error",
input: OTLPConfig{Endpoint: "http:https://localhost:4318",
Protocol: "",
Insecure: false},
output: nil,
},
{
name: "Test grpc protocol, expect no error",
input: OTLPConfig{Endpoint: "http:https://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:https://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()
}
85 changes: 85 additions & 0 deletions vendor/go.opentelemetry.io/otel/sdk/trace/tracetest/exporter.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

92 changes: 92 additions & 0 deletions vendor/go.opentelemetry.io/otel/sdk/trace/tracetest/recorder.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c80347e

Please sign in to comment.