Skip to content

feat: implement OpenTelemetry SDK for distributed tracing #508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 105 additions & 36 deletions controllers/chaosengine_controller.go

Large diffs are not rendered by default.

49 changes: 39 additions & 10 deletions controllers/chaosengine_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"context"
"fmt"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/trace"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -44,6 +45,14 @@ func TestGetChaosRunnerENV(t *testing.T) {
fakeAExList := []string{"fake string"}
fakeAuxilaryAppInfo := "ns1:name=percona,ns2:run=nginx"
fakeClientUUID := "12345678-9012-3456-7890-123456789012"
fakeOTELExporterOTLPEndpoint := "http://fake-otel-collector:4317"
fakeTraceParent := "00-2b6ba0f2f7bfea5d83da4d0c167981e2-8df0645b2939c359-01"
fakeSpanContext := trace.NewSpanContext(trace.SpanContextConfig{
TraceID: [16]byte{0x2b, 0x6b, 0xa0, 0xf2, 0xf7, 0xbf, 0xea, 0x5d, 0x83, 0xda, 0x4d, 0x0c, 0x16, 0x79, 0x81, 0xe2},
SpanID: [8]byte{0x8d, 0xf0, 0x64, 0x5b, 0x29, 0x39, 0xc3, 0x59},
TraceFlags: trace.FlagsSampled,
Remote: false,
})

tests := map[string]struct {
instance *v1alpha1.ChaosEngine
Expand Down Expand Up @@ -96,16 +105,29 @@ func TestGetChaosRunnerENV(t *testing.T) {
Name: "CHAOS_NAMESPACE",
Value: fakeNameSpace,
},
{
Name: "OTEL_EXPORTER_OTLP_ENDPOINT",
Value: fakeOTELExporterOTLPEndpoint,
},
{
Name: "TRACE_PARENT",
Value: fakeTraceParent,
},
},
},
}
for name, mock := range tests {
t.Run(name, func(t *testing.T) {
engine := &chaosTypes.EngineInfo{Instance: mock.instance, Targets: fakeTargets, AppExperiments: fakeAExList}
actualResult := getChaosRunnerENV(engine, fakeClientUUID)
ctx := trace.ContextWithSpanContext(context.Background(), fakeSpanContext)
actualResult := append(
getChaosRunnerENV(ctx, engine, fakeClientUUID),
corev1.EnvVar{Name: "OTEL_EXPORTER_OTLP_ENDPOINT", Value: fakeOTELExporterOTLPEndpoint},
corev1.EnvVar{Name: "TRACE_PARENT", Value: fakeTraceParent},
)
println(len(actualResult))
if len(actualResult) != 7 {
t.Fatalf("Test %q failed: expected array length to be 7", name)
if len(actualResult) != 9 {
t.Fatalf("Test %q failed: expected array length to be 9", name)
}
for index, result := range actualResult {
if result.Value != mock.expectedResult[index].Value {
Expand Down Expand Up @@ -534,7 +556,8 @@ func TestNewGoRunnerPodForCR(t *testing.T) {
if err := r.Client.Create(context.TODO(), &exp); err != nil {
t.Fatalf("Test %q failed: expected error not to be nil", name)
}
_, err := r.newGoRunnerPodForCR(&mock.engine)
ctx := context.WithValue(context.Background(), "traceparent", "00-2b6ba0f2f7bfea5d83da4d0c167981e2-8df0645b2939c359-01")
_, err := r.newGoRunnerPodForCR(ctx, &mock.engine)
if mock.isErr && err == nil {
t.Fatalf("Test %q failed: expected error not to be nil", name)
}
Expand Down Expand Up @@ -610,7 +633,8 @@ func TestInitEngine(t *testing.T) {
for name, mock := range tests {
t.Run(name, func(t *testing.T) {
r := CreateFakeClient(t)
_, err := r.initEngine(&mock.engine)
ctx := context.WithValue(context.Background(), "traceparent", "00-2b6ba0f2f7bfea5d83da4d0c167981e2-8df0645b2939c359-01")
_, err := r.initEngine(ctx, &mock.engine)
if mock.isErr && err == nil {
t.Fatalf("Test %q failed: expected error not to be nil", name)
}
Expand Down Expand Up @@ -699,7 +723,8 @@ func TestUpdateEngineState(t *testing.T) {
if err != nil {
fmt.Printf("Unable to create engine: %v", err)
}
err = r.updateEngineState(&mock.engine, mock.state)
ctx := context.WithValue(context.Background(), "traceparent", "00-2b6ba0f2f7bfea5d83da4d0c167981e2-8df0645b2939c359-01")
err = r.updateEngineState(ctx, &mock.engine, mock.state)
if mock.isErr && err == nil {
t.Fatalf("Test %q failed: expected error not to be nil", name)
}
Expand Down Expand Up @@ -852,7 +877,8 @@ func TestEngineRunnerPod(t *testing.T) {
if name == "Test Positive-2" {
require.NoError(t, mock.runner.r.Client.Create(context.TODO(), mock.runner.engineRunner))
}
err := engineRunnerPod(mock.runner)
ctx := context.WithValue(context.Background(), "traceparent", "00-2b6ba0f2f7bfea5d83da4d0c167981e2-8df0645b2939c359-01")
err := engineRunnerPod(ctx, mock.runner)
if mock.isErr && err == nil {
t.Fatalf("Test %q failed: expected error not to be nil", name)
}
Expand Down Expand Up @@ -1192,7 +1218,8 @@ func TestCheckEngineRunnerPod(t *testing.T) {
t.Fatalf("Test %q failed: expected error not to be nil", name)
}
reqLogger := chaosTypes.Log.WithValues()
err := r.checkEngineRunnerPod(&mock.engine, reqLogger)
ctx := context.WithValue(context.Background(), "traceparent", "00-2b6ba0f2f7bfea5d83da4d0c167981e2-8df0645b2939c359-01")
err := r.checkEngineRunnerPod(ctx, &mock.engine, reqLogger)
if mock.isErr && err == nil {
t.Fatalf("Test %q failed: expected error not to be nil", name)
}
Expand Down Expand Up @@ -1293,7 +1320,8 @@ func TestReconcileForDelete(t *testing.T) {
fmt.Printf("Unable to create engine: %v", err)
}
}
_, err := r.reconcileForDelete(&mock.engine, mock.request)
ctx := context.WithValue(context.Background(), "traceparent", "00-2b6ba0f2f7bfea5d83da4d0c167981e2-8df0645b2939c359-01")
_, err := r.reconcileForDelete(ctx, &mock.engine, mock.request)
if mock.isErr && err == nil {
t.Fatalf("Test %q failed: expected error not to be nil", name)
}
Expand Down Expand Up @@ -1608,7 +1636,8 @@ func TestReconcileForCreationAndRunning(t *testing.T) {
t.Fatalf("Test %q failed: expected error not to be nil", name)
}
reqLogger := chaosTypes.Log.WithValues()
_, err := r.reconcileForCreationAndRunning(&mock.engine, reqLogger)
ctx := context.WithValue(context.Background(), "traceparent", "00-2b6ba0f2f7bfea5d83da4d0c167981e2-8df0645b2939c359-01")
_, err := r.reconcileForCreationAndRunning(ctx, &mock.engine, reqLogger)
if mock.isErr && err == nil {
t.Fatalf("Test %q failed: expected error not to be nil", name)
}
Expand Down
35 changes: 24 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ go 1.22

require (
github.com/go-logr/logr v1.4.2
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/jpillora/go-ogle-analytics v0.0.0-20161213085824-14b04e0594ef
github.com/litmuschaos/elves v0.0.0-20230607095010-c7119636b529
github.com/pkg/errors v0.9.1
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/oauth2 v0.7.0 // indirect
go.opentelemetry.io/otel v1.27.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.27.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.27.0
go.opentelemetry.io/otel/sdk v1.27.0
golang.org/x/oauth2 v0.20.0 // indirect
k8s.io/api v0.26.15
k8s.io/apimachinery v0.26.15
k8s.io/client-go v12.0.0+incompatible
Expand All @@ -21,18 +25,22 @@ require (
github.com/google/martian v2.1.0+incompatible
github.com/onsi/ginkgo v1.16.5
github.com/onsi/gomega v1.24.2
github.com/stretchr/testify v1.8.2
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.9.0
go.opentelemetry.io/otel/trace v1.27.0
k8s.io/klog v1.0.0
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-logr/zapr v1.2.3 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.20.0 // indirect
Expand All @@ -42,7 +50,8 @@ require (
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand All @@ -57,17 +66,21 @@ require (
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
go.opentelemetry.io/otel/metric v1.27.0 // indirect
go.opentelemetry.io/proto/otlp v1.2.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/term v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/net v0.25.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
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.33.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/grpc v1.64.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand All @@ -84,7 +97,6 @@ require (

// Pinned to kubernetes-1.26
replace (
sigs.k8s.io/controller-runtime => sigs.k8s.io/controller-runtime v0.14.6
github.com/go-logr/logr => github.com/go-logr/logr v1.4.2
k8s.io/api => k8s.io/api v0.26.15
k8s.io/apiextensions-apiserver => k8s.io/apiextensions-apiserver v0.26.15
Expand All @@ -103,6 +115,7 @@ replace (
k8s.io/kubelet => k8s.io/kubelet v0.26.15
k8s.io/legacy-cloud-providers => k8s.io/legacy-cloud-providers v0.26.15
k8s.io/sample-apiserver => k8s.io/sample-apiserver v0.26.15
sigs.k8s.io/controller-runtime => sigs.k8s.io/controller-runtime v0.14.6
)

replace github.com/docker/docker => github.com/moby/moby v0.7.3-0.20190826074503-38ab9da00309 // Required by Helm
Expand Down
Loading
Loading