Skip to content

Commit

Permalink
add OTEL trace for clusterpedia-apiserver
Browse files Browse the repository at this point in the history
  • Loading branch information
KubeKyrie committed Nov 27, 2023
1 parent f3e50db commit 348e9a3
Show file tree
Hide file tree
Showing 35 changed files with 6,347 additions and 5 deletions.
17 changes: 17 additions & 0 deletions cmd/apiserver/app/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ import (
"k8s.io/component-base/logs"
logsapi "k8s.io/component-base/logs/api/v1"
"k8s.io/component-base/term"
"k8s.io/klog/v2"

"github.com/clusterpedia-io/clusterpedia/cmd/apiserver/app/options"
"github.com/clusterpedia-io/clusterpedia/pkg/constants"
"github.com/clusterpedia-io/clusterpedia/pkg/storage"
"github.com/clusterpedia-io/clusterpedia/pkg/utils/trace"
"github.com/clusterpedia-io/clusterpedia/pkg/version/verflag"
)

Expand Down Expand Up @@ -45,6 +48,20 @@ func NewClusterPediaServerCommand(ctx context.Context) *cobra.Command {
return err
}

if _, exist := os.LookupEnv(constants.OtelAgentAddrENV); exist {
closeTracer, err := trace.InitTracer(ctx)
if err != nil {
klog.Fatalf("failed to initialize tracing: %v", err)
}
defer func() {
err := closeTracer(ctx)
if err != nil {
klog.ErrorS(err, "failed to stop exporter: %v", err)
return
}
}()
}

if err := server.Run(ctx); err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ require (
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.3
go.opentelemetry.io/otel v1.10.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.10.0
go.opentelemetry.io/otel/sdk v1.10.0
go.uber.org/atomic v1.10.0
google.golang.org/grpc v1.54.0
gopkg.in/natefinch/lumberjack.v2 v2.2.1
gorm.io/datatypes v1.0.7
gorm.io/driver/mysql v1.4.4
Expand Down Expand Up @@ -124,12 +128,9 @@ require (
go.etcd.io/etcd/client/v3 v3.5.9 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.35.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.35.1 // indirect
go.opentelemetry.io/otel v1.10.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.10.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.10.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.10.0 // indirect
go.opentelemetry.io/otel/metric v0.31.0 // indirect
go.opentelemetry.io/otel/sdk v1.10.0 // indirect
go.opentelemetry.io/otel/trace v1.10.0 // indirect
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
Expand All @@ -149,7 +150,6 @@ require (
google.golang.org/genproto v0.0.0-20230526161137-0005af68ea54 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230525234035-dd9d682886f9 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 // indirect
google.golang.org/grpc v1.54.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
5 changes: 5 additions & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package constants

const (
OtelAgentAddrENV = "OTEL_EXPORTER_OTLP_ENDPOINT"
)
38 changes: 38 additions & 0 deletions pkg/utils/trace/trace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package trace

import (
"context"

"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.7.0"
"google.golang.org/grpc"
"k8s.io/klog/v2"
)

// InitTracer initializes the trace provider and the otel grpc exporter.
func InitTracer(ctx context.Context) (func(ctx context.Context) error, error) {
traceExporter, err := otlptracegrpc.New(ctx, otlptracegrpc.WithInsecure(), otlptracegrpc.WithDialOption(grpc.WithBlock()))
if err != nil {
return nil, err
}

tracerProvider := sdktrace.NewTracerProvider(
sdktrace.WithBatcher(traceExporter),
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithResource(resource.NewWithAttributes(semconv.SchemaURL)))

otel.SetTracerProvider(tracerProvider)
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))

return func(ctx context.Context) error {
if err := traceExporter.Shutdown(ctx); err != nil {
klog.V(2).ErrorS(err, "failed to stop exporter")
return err
}
return nil
}, nil
}
1 change: 0 additions & 1 deletion vendor/github.com/clusterpedia-io/api

This file was deleted.

6 changes: 6 additions & 0 deletions vendor/github.com/clusterpedia-io/api/cluster/v1alpha2/doc.go

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

51 changes: 51 additions & 0 deletions vendor/github.com/clusterpedia-io/api/cluster/v1alpha2/register.go

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

Loading

0 comments on commit 348e9a3

Please sign in to comment.