Skip to content

Commit

Permalink
first opentelemtry tracing implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Surax98 <giacomo.surace@gmail.com>
  • Loading branch information
Surax98 committed Aug 29, 2024
1 parent 37d0a3b commit f51dc9b
Show file tree
Hide file tree
Showing 9 changed files with 391 additions and 134 deletions.
97 changes: 91 additions & 6 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,83 @@ package main

import (
"context"
"crypto/tls"
"fmt"
"net/http"
"os"
"strconv"
"time"

"github.com/sirupsen/logrus"
"github.com/virtual-kubelet/virtual-kubelet/log"
logruslogger "github.com/virtual-kubelet/virtual-kubelet/log/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"

slurm "github.com/intertwin-eu/interlink-slurm-plugin/pkg/slurm"

"github.com/virtual-kubelet/virtual-kubelet/trace"
"github.com/virtual-kubelet/virtual-kubelet/trace/opentelemetry"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
)

func initProvider(ctx context.Context) (func(context.Context) error, error) {
res, err := resource.New(ctx,
resource.WithAttributes(
// the service name used to display traces in backends
semconv.ServiceName("InterLink-SLURM-plugin"),
),
)
if err != nil {
return nil, fmt.Errorf("failed to create resource: %w", err)
}

ctx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()

otlpEndpoint := os.Getenv("TELEMETRY_ENDPOINT")

if otlpEndpoint == "" {
otlpEndpoint = "localhost:4317"
}

fmt.Println("TELEMETRY_ENDPOINT: ", otlpEndpoint)

conn := &grpc.ClientConn{}
creds := credentials.NewTLS(&tls.Config{InsecureSkipVerify: true})
conn, err = grpc.DialContext(ctx, otlpEndpoint, grpc.WithTransportCredentials(creds), grpc.WithBlock())

if err != nil {
return nil, fmt.Errorf("failed to create gRPC connection to collector: %w", err)
}

// Set up a trace exporter
traceExporter, err := otlptracegrpc.New(ctx, otlptracegrpc.WithGRPCConn(conn))
if err != nil {
return nil, fmt.Errorf("failed to create trace exporter: %w", err)
}

// Register the trace exporter with a TracerProvider, using a batch
// span processor to aggregate spans before export.
bsp := sdktrace.NewBatchSpanProcessor(traceExporter)
tracerProvider := sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithResource(res),
sdktrace.WithSpanProcessor(bsp),
)
otel.SetTracerProvider(tracerProvider)

// set global propagator to tracecontext (the default is no-op).
otel.SetTextMapPropagator(propagation.TraceContext{})

return tracerProvider.Shutdown, nil
}

func main() {
logger := logrus.StandardLogger()

Expand All @@ -31,14 +98,32 @@ func main() {
log.L = logruslogger.FromLogrus(logrus.NewEntry(logger))

JobIDs := make(map[string]*slurm.JidStruct)
Ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
log.G(Ctx).Debug("Debug level: " + strconv.FormatBool(slurmConfig.VerboseLogging))

if os.Getenv("ENABLE_TRACING") == "1" {
shutdown, err := initProvider(ctx)
if err != nil {
log.G(ctx).Fatal(err)
}
defer func() {
if err = shutdown(ctx); err != nil {
log.G(ctx).Fatal("failed to shutdown TracerProvider: %w", err)
}
}()

log.G(ctx).Info("Tracer setup succeeded")

// TODO: disable this through options
trace.T = opentelemetry.Adapter{}
}

log.G(ctx).Debug("Debug level: " + strconv.FormatBool(slurmConfig.VerboseLogging))

SidecarAPIs := slurm.SidecarHandler{
Config: slurmConfig,
JIDs: &JobIDs,
Ctx: Ctx,
Ctx: ctx,
}

mutex := http.NewServeMux()
Expand All @@ -47,11 +132,11 @@ func main() {
mutex.HandleFunc("/delete", SidecarAPIs.StopHandler)
mutex.HandleFunc("/getLogs", SidecarAPIs.GetLogsHandler)

slurm.CreateDirectories(slurmConfig)
slurm.LoadJIDs(Ctx, slurmConfig, &JobIDs)
SidecarAPIs.CreateDirectories()
SidecarAPIs.LoadJIDs()

err = http.ListenAndServe(":"+slurmConfig.Sidecarport, mutex)
if err != nil {
log.G(Ctx).Fatal(err)
log.G(ctx).Fatal(err)
}
}
36 changes: 24 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,47 +1,59 @@
module github.com/intertwin-eu/interlink-slurm-plugin

go 1.21
go 1.22

toolchain go1.21.3
toolchain go1.22.4

require (
github.com/alexellis/go-execute v0.6.0
github.com/containerd/containerd v1.7.6
github.com/intertwin-eu/interlink v0.0.0-20240523154644-820ca4bd6fac
github.com/intertwin-eu/interlink v0.0.0-20240829090340-24c45973f3ec
github.com/sirupsen/logrus v1.9.3
github.com/virtual-kubelet/virtual-kubelet v1.11.0
go.opentelemetry.io/otel v1.27.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.27.0
go.opentelemetry.io/otel/sdk v1.27.0
go.opentelemetry.io/otel/trace v1.27.0
google.golang.org/grpc v1.64.0
gopkg.in/yaml.v2 v2.4.0
k8s.io/api v0.29.1
k8s.io/apimachinery v0.29.1
k8s.io/client-go v0.29.1
)

require (
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/jsonpointer v0.20.0 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.4 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/oauth2 v0.16.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/term v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.27.0 // indirect
go.opentelemetry.io/otel/metric v1.27.0 // indirect
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/oauth2 v0.20.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/term v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/protobuf v1.31.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240520151616-dc85e6b867a5 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240515191416-fc5f0ca64291 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/klog/v2 v2.110.1 // indirect
Expand Down
Loading

0 comments on commit f51dc9b

Please sign in to comment.