[otel-tracing] Initial opentelemetry support

Add basic intiialization of opentelemetry including minimum support to
be able to read open telemetry config from config.toml and initialize
exporter. Tracer is initialized and ready to be be used for creating
spans, sub spans etc. With no opentelemetry configuration enabled in
config file, this patch is a no-op.

Basic config stub to be added to use opentelemetry is to add following
in config.toml. We use otlp exporter with default port 4317.

[otel]
  exporter_name = "otlp"
  exporter_endpoint = "0.0.0.1:4317"

otel-collector binary needs to run listening at the same port.

Signed-off-by: Alakesh Haloi <alakeshh@amazon.com>
This commit is contained in:
Alakesh Haloi
2021-07-06 11:25:03 -07:00
parent 10824eaf2e
commit 3597ac859d
4 changed files with 166 additions and 2 deletions

View File

@@ -34,10 +34,12 @@ import (
"github.com/containerd/containerd/services/server"
srvconfig "github.com/containerd/containerd/services/server/config"
"github.com/containerd/containerd/sys"
"github.com/containerd/containerd/tracing"
"github.com/containerd/containerd/version"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"go.opentelemetry.io/otel"
"google.golang.org/grpc/grpclog"
)
@@ -130,6 +132,20 @@ can be used and modified as necessary as a custom configuration.`
return err
}
// Initialize OpenTelemetry tracing
shutdown, err := tracing.InitOpenTelemetry(config)
if err != nil {
errors.Wrap(err, "failed to initialize OpenTelemetry tracing")
}
if shutdown != nil {
defer shutdown()
}
// Get a tracer
ctrdTracer := otel.Tracer("containerd")
ctx, mainCtrdSpan := ctrdTracer.Start(ctx, "containerd-exporter")
defer mainCtrdSpan.End()
// Make sure top-level directories are created early.
if err := server.CreateTopLevelDirectories(config); err != nil {
return err
@@ -243,6 +259,9 @@ can be used and modified as necessary as a custom configuration.`
func serve(ctx gocontext.Context, l net.Listener, serveFunc func(net.Listener) error) {
path := l.Addr().String()
log.G(ctx).WithField("address", path).Info("serving...")
serveSpan, ctx := tracing.StartSpan(ctx, l.Addr().String())
defer tracing.StopSpan(serveSpan)
go func() {
defer l.Close()
if err := serveFunc(l); err != nil {