Skip to content

Commit e9be0ec

Browse files
committed
fix: make otel optional
Signed-off-by: namkyu1999 <[email protected]>
1 parent 803ab50 commit e9be0ec

File tree

10 files changed

+44
-28
lines changed

10 files changed

+44
-28
lines changed

bin/experiment/experiment.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"errors"
66
"flag"
7+
"os"
8+
79
// Uncomment to load all auth plugins
810
// _ "k8s.io/client-go/plugin/pkg/client/auth"
911

@@ -64,6 +66,7 @@ import (
6466
cli "github.com/litmuschaos/litmus-go/pkg/clients"
6567
"github.com/litmuschaos/litmus-go/pkg/log"
6668
"github.com/litmuschaos/litmus-go/pkg/telemetry"
69+
"github.com/litmuschaos/litmus-go/pkg/utils"
6770
"github.com/sirupsen/logrus"
6871
)
6972

@@ -78,17 +81,19 @@ func init() {
7881

7982
func main() {
8083
ctx := context.Background()
84+
8185
// Set up Observability.
82-
shutdown, err := telemetry.InitOTelSDK(ctx, true)
83-
if err != nil {
84-
return
86+
if otelExporterEndpoint := os.Getenv(utils.OTELExporterOTLPEndpoint); otelExporterEndpoint != "" {
87+
shutdown, err := telemetry.InitOTelSDK(ctx, true, otelExporterEndpoint)
88+
if err != nil {
89+
return
90+
}
91+
defer func() {
92+
err = errors.Join(err, shutdown(ctx))
93+
}()
94+
ctx = telemetry.GetTraceParentContext()
8595
}
86-
// Handle shutdown properly so nothing leaks.
87-
defer func() {
88-
err = errors.Join(err, shutdown(ctx))
89-
}()
9096

91-
ctx = telemetry.GetTraceParentContext()
9297
clients := cli.ClientSets{Context: ctx}
9398

9499
span := telemetry.StartTracing(clients, "ExecuteExperiment")

bin/helper/helper.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"errors"
66
"flag"
7+
"os"
8+
79
// Uncomment to load all auth plugins
810
// _ "k8s.io/client-go/plugin/pkg/client/auth"
911

@@ -20,6 +22,7 @@ import (
2022
dnsChaos "github.com/litmuschaos/litmus-go/chaoslib/litmus/pod-dns-chaos/helper"
2123
stressChaos "github.com/litmuschaos/litmus-go/chaoslib/litmus/stress-chaos/helper"
2224
"github.com/litmuschaos/litmus-go/pkg/telemetry"
25+
"github.com/litmuschaos/litmus-go/pkg/utils"
2326

2427
cli "github.com/litmuschaos/litmus-go/pkg/clients"
2528
"github.com/litmuschaos/litmus-go/pkg/log"
@@ -38,16 +41,17 @@ func init() {
3841
func main() {
3942
ctx := context.Background()
4043
// Set up Observability.
41-
shutdown, err := telemetry.InitOTelSDK(ctx, false)
42-
if err != nil {
43-
return
44+
if otelExporterEndpoint := os.Getenv(utils.OTELExporterOTLPEndpoint); otelExporterEndpoint != "" {
45+
shutdown, err := telemetry.InitOTelSDK(ctx, true, otelExporterEndpoint)
46+
if err != nil {
47+
return
48+
}
49+
defer func() {
50+
err = errors.Join(err, shutdown(ctx))
51+
}()
52+
ctx = telemetry.GetTraceParentContext()
4453
}
45-
// Handle shutdown properly so nothing leaks.
46-
defer func() {
47-
err = errors.Join(err, shutdown(ctx))
48-
}()
4954

50-
ctx = telemetry.GetTraceParentContext()
5155
clients := cli.ClientSets{Context: ctx}
5256

5357
span := telemetry.StartTracing(clients, "ExecuteExperimentHelper")

chaoslib/litmus/container-kill/lib/container-kill.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/litmuschaos/litmus-go/pkg/cerrors"
1111
"github.com/litmuschaos/litmus-go/pkg/telemetry"
12+
"github.com/litmuschaos/litmus-go/pkg/utils"
1213
"github.com/palantir/stacktrace"
1314

1415
"github.com/litmuschaos/litmus-go/pkg/clients"
@@ -281,7 +282,7 @@ func getPodEnv(ctx context.Context, experimentsDetails *experimentTypes.Experime
281282
SetEnv("STATUS_CHECK_TIMEOUT", strconv.Itoa(experimentsDetails.Timeout)).
282283
SetEnv("EXPERIMENT_NAME", experimentsDetails.ExperimentName).
283284
SetEnv("INSTANCE_ID", experimentsDetails.InstanceID).
284-
SetEnv("OTEL_EXPORTER_OTLP_ENDPOINT", os.Getenv(telemetry.OTELExporterOTLPEndpoint)).
285+
SetEnv("OTEL_EXPORTER_OTLP_ENDPOINT", os.Getenv(utils.OTELExporterOTLPEndpoint)).
285286
SetEnv("TRACE_PARENT", telemetry.GetMarshalledSpanFromContext(ctx)).
286287
SetEnvFromDownwardAPI("v1", "metadata.name")
287288

chaoslib/litmus/disk-fill/lib/disk-fill.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/litmuschaos/litmus-go/pkg/cerrors"
1111
"github.com/litmuschaos/litmus-go/pkg/telemetry"
12+
"github.com/litmuschaos/litmus-go/pkg/utils"
1213
"github.com/palantir/stacktrace"
1314

1415
"github.com/litmuschaos/litmus-go/pkg/clients"
@@ -289,7 +290,7 @@ func getPodEnv(ctx context.Context, experimentsDetails *experimentTypes.Experime
289290
SetEnv("INSTANCE_ID", experimentsDetails.InstanceID).
290291
SetEnv("SOCKET_PATH", experimentsDetails.SocketPath).
291292
SetEnv("CONTAINER_RUNTIME", experimentsDetails.ContainerRuntime).
292-
SetEnv("OTEL_EXPORTER_OTLP_ENDPOINT", os.Getenv(telemetry.OTELExporterOTLPEndpoint)).
293+
SetEnv("OTEL_EXPORTER_OTLP_ENDPOINT", os.Getenv(utils.OTELExporterOTLPEndpoint)).
293294
SetEnv("TRACE_PARENT", telemetry.GetMarshalledSpanFromContext(ctx)).
294295
SetEnvFromDownwardAPI("v1", "metadata.name")
295296

chaoslib/litmus/http-chaos/lib/http-chaos.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/litmuschaos/litmus-go/pkg/cerrors"
1111
"github.com/litmuschaos/litmus-go/pkg/telemetry"
12+
"github.com/litmuschaos/litmus-go/pkg/utils"
1213
"github.com/palantir/stacktrace"
1314

1415
"github.com/litmuschaos/litmus-go/pkg/clients"
@@ -283,7 +284,7 @@ func getPodEnv(ctx context.Context, experimentsDetails *experimentTypes.Experime
283284
SetEnv("TARGET_SERVICE_PORT", strconv.Itoa(experimentsDetails.TargetServicePort)).
284285
SetEnv("PROXY_PORT", strconv.Itoa(experimentsDetails.ProxyPort)).
285286
SetEnv("TOXICITY", strconv.Itoa(experimentsDetails.Toxicity)).
286-
SetEnv("OTEL_EXPORTER_OTLP_ENDPOINT", os.Getenv(telemetry.OTELExporterOTLPEndpoint)).
287+
SetEnv("OTEL_EXPORTER_OTLP_ENDPOINT", os.Getenv(utils.OTELExporterOTLPEndpoint)).
287288
SetEnv("TRACE_PARENT", telemetry.GetMarshalledSpanFromContext(ctx)).
288289
SetEnvFromDownwardAPI("v1", "metadata.name")
289290

chaoslib/litmus/network-chaos/lib/network-chaos.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/litmuschaos/litmus-go/pkg/cerrors"
1212
"github.com/litmuschaos/litmus-go/pkg/telemetry"
13+
"github.com/litmuschaos/litmus-go/pkg/utils"
1314
"github.com/palantir/stacktrace"
1415
k8serrors "k8s.io/apimachinery/pkg/api/errors"
1516

@@ -295,7 +296,7 @@ func getPodEnv(ctx context.Context, experimentsDetails *experimentTypes.Experime
295296
SetEnv("DESTINATION_IPS_SERVICE_MESH", destIpsSvcMesh).
296297
SetEnv("SOURCE_PORTS", experimentsDetails.SourcePorts).
297298
SetEnv("DESTINATION_PORTS", experimentsDetails.DestinationPorts).
298-
SetEnv("OTEL_EXPORTER_OTLP_ENDPOINT", os.Getenv(telemetry.OTELExporterOTLPEndpoint)).
299+
SetEnv("OTEL_EXPORTER_OTLP_ENDPOINT", os.Getenv(utils.OTELExporterOTLPEndpoint)).
299300
SetEnv("TRACE_PARENT", telemetry.GetMarshalledSpanFromContext(ctx)).
300301
SetEnvFromDownwardAPI("v1", "metadata.name")
301302

chaoslib/litmus/pod-dns-chaos/lib/pod-dns-chaos.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/litmuschaos/litmus-go/pkg/cerrors"
1111
"github.com/litmuschaos/litmus-go/pkg/telemetry"
12+
"github.com/litmuschaos/litmus-go/pkg/utils"
1213
"github.com/palantir/stacktrace"
1314

1415
"github.com/litmuschaos/litmus-go/pkg/clients"
@@ -283,7 +284,7 @@ func getPodEnv(ctx context.Context, experimentsDetails *experimentTypes.Experime
283284
SetEnv("MATCH_SCHEME", experimentsDetails.MatchScheme).
284285
SetEnv("CHAOS_TYPE", experimentsDetails.ChaosType).
285286
SetEnv("INSTANCE_ID", experimentsDetails.InstanceID).
286-
SetEnv("OTEL_EXPORTER_OTLP_ENDPOINT", os.Getenv(telemetry.OTELExporterOTLPEndpoint)).
287+
SetEnv("OTEL_EXPORTER_OTLP_ENDPOINT", os.Getenv(utils.OTELExporterOTLPEndpoint)).
287288
SetEnv("TRACE_PARENT", telemetry.GetMarshalledSpanFromContext(ctx)).
288289
SetEnvFromDownwardAPI("v1", "metadata.name")
289290

chaoslib/litmus/stress-chaos/lib/stress-chaos.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/litmuschaos/litmus-go/pkg/cerrors"
1111
"github.com/litmuschaos/litmus-go/pkg/telemetry"
12+
"github.com/litmuschaos/litmus-go/pkg/utils"
1213
"github.com/palantir/stacktrace"
1314

1415
"github.com/litmuschaos/litmus-go/pkg/clients"
@@ -328,7 +329,7 @@ func getPodEnv(ctx context.Context, experimentsDetails *experimentTypes.Experime
328329
SetEnv("VOLUME_MOUNT_PATH", experimentsDetails.VolumeMountPath).
329330
SetEnv("STRESS_TYPE", experimentsDetails.StressType).
330331
SetEnv("INSTANCE_ID", experimentsDetails.InstanceID).
331-
SetEnv("OTEL_EXPORTER_OTLP_ENDPOINT", os.Getenv(telemetry.OTELExporterOTLPEndpoint)).
332+
SetEnv("OTEL_EXPORTER_OTLP_ENDPOINT", os.Getenv(utils.OTELExporterOTLPEndpoint)).
332333
SetEnv("TRACE_PARENT", telemetry.GetMarshalledSpanFromContext(ctx)).
333334
SetEnvFromDownwardAPI("v1", "metadata.name")
334335

pkg/telemetry/otel.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package telemetry
33
import (
44
"context"
55
"errors"
6-
"os"
76

87
"go.opentelemetry.io/otel"
98
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
@@ -15,12 +14,11 @@ import (
1514
)
1615

1716
const (
18-
OTELExporterOTLPEndpoint = "OTEL_EXPORTER_OTLP_ENDPOINT"
1917
OTELExperimentJobServiceName = "chaos_experiment_job"
2018
OTELExperimentJobHelperServiceName = "chaos_experiment_job_helper"
2119
)
2220

23-
func InitOTelSDK(ctx context.Context, isExperiment bool) (shutdown func(context.Context) error, err error) {
21+
func InitOTelSDK(ctx context.Context, isExperiment bool, endpoint string) (shutdown func(context.Context) error, err error) {
2422
var shutdownFuncs []func(context.Context) error
2523

2624
shutdown = func(ctx context.Context) error {
@@ -36,7 +34,7 @@ func InitOTelSDK(ctx context.Context, isExperiment bool) (shutdown func(context.
3634
err = errors.Join(inErr, shutdown(ctx))
3735
}
3836

39-
tracerProvider, err := newTracerProvider(ctx, isExperiment)
37+
tracerProvider, err := newTracerProvider(ctx, isExperiment, endpoint)
4038
if err != nil {
4139
handleErr(err)
4240
return
@@ -59,8 +57,7 @@ func newPropagator() propagation.TextMapPropagator {
5957
)
6058
}
6159

62-
func newTracerProvider(ctx context.Context, isExperiment bool) (*trace.TracerProvider, error) {
63-
endpoint := os.Getenv(OTELExporterOTLPEndpoint)
60+
func newTracerProvider(ctx context.Context, isExperiment bool, endpoint string) (*trace.TracerProvider, error) {
6461
serviceName := OTELExperimentJobHelperServiceName
6562
if isExperiment {
6663
serviceName = OTELExperimentJobServiceName

pkg/utils/utils.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ package utils
22

33
import "net/url"
44

5+
const (
6+
OTELExporterOTLPEndpoint = "OTEL_EXPORTER_OTLP_ENDPOINT"
7+
)
8+
59
func HttpTimeout(err error) bool {
610
httpErr := err.(*url.Error)
711
if httpErr != nil {

0 commit comments

Comments
 (0)