From ae4e0887ce1b6284154c534dd7ba0942473de2c3 Mon Sep 17 00:00:00 2001 From: ale <ale@incal.net> Date: Sat, 13 Aug 2022 18:09:15 +0100 Subject: [PATCH] Add simple tests for the tracing pkg --- go.mod | 1 + tracing/tracing.go | 42 ++++++++++++++++++++--------------------- tracing/tracing_test.go | 41 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 21 deletions(-) create mode 100644 tracing/tracing_test.go diff --git a/go.mod b/go.mod index 2b63b14..9933b74 100644 --- a/go.mod +++ b/go.mod @@ -25,6 +25,7 @@ require ( go.opentelemetry.io/otel v1.9.1-0.20220810155701-d96e8d2912af go.opentelemetry.io/otel/exporters/zipkin v1.9.0 go.opentelemetry.io/otel/sdk v1.9.0 + go.opentelemetry.io/otel/trace v1.9.0 golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 ) diff --git a/tracing/tracing.go b/tracing/tracing.go index 0671df4..a20a672 100644 --- a/tracing/tracing.go +++ b/tracing/tracing.go @@ -15,12 +15,16 @@ import ( "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/exporters/zipkin" "go.opentelemetry.io/otel/sdk/trace" + apitrace "go.opentelemetry.io/otel/trace" ) var ( // Enabled reports whether tracing is globally enabled or not. Enabled bool + // Global Tracer instance. + Tracer apitrace.Tracer + // The active tracing configuration, if Enabled is true. config tracingConfig @@ -37,7 +41,7 @@ type tracingConfig struct { // Read the global tracing configuration file. Its location is // hardcoded, but it can be overriden using the TRACING_CONFIG // environment variable. -func readTracingConfig() error { +func readTracingConfig() (*tracingConfig, error) { // Read and decode configuration. cfgPath := globalTracingConfigPath if s := os.Getenv("TRACING_CONFIG"); s != "" { @@ -45,20 +49,21 @@ func readTracingConfig() error { } data, err := ioutil.ReadFile(cfgPath) if err != nil { - return err + return nil, err } + var config tracingConfig if err := json.Unmarshal(data, &config); err != nil { log.Printf("warning: error in tracing configuration: %v, tracing disabled", err) - return err + return nil, err } if config.ReportURL == "" { log.Printf("warning: tracing configuration contains no report_url, tracing disabled") - return errors.New("no report_url") + return nil, errors.New("no report_url") } - return nil + return &config, nil } // Compute the service name for Zipkin: this is usually the program @@ -78,24 +83,18 @@ func getServiceName() string { // We need to check the configuration as soon as possible, because // it's likely that client transports are created before HTTP servers, // and we need to wrap them with opencensus at creation time. -func init() { - // Kill switch from environment. - if s := os.Getenv("TRACING_ENABLE"); s == "0" { - return - } - - if err := readTracingConfig(); err != nil { - return - } - - Enabled = true -} - func initTracing(endpointAddr string) { - if !Enabled { - return - } initOnce.Do(func() { + // Kill switch from environment. + if s := os.Getenv("TRACING_ENABLE"); s == "0" { + return + } + + config, err := readTracingConfig() + if err != nil { + return + } + ze, err := zipkin.New(config.ReportURL) if err != nil { log.Printf("error creating Zipkin exporter: %v", err) @@ -123,6 +122,7 @@ func initTracing(endpointAddr string) { ) otel.SetTracerProvider(tp) + Tracer = tp.Tracer(endpointAddr) log.Printf("tracing enabled (report_url %s)", config.ReportURL) diff --git a/tracing/tracing_test.go b/tracing/tracing_test.go new file mode 100644 index 0000000..a65ba6d --- /dev/null +++ b/tracing/tracing_test.go @@ -0,0 +1,41 @@ +package tracing + +import ( + "encoding/json" + "io/ioutil" + "net/http" + "net/http/httptest" + "os" + "testing" +) + +func TestTracing(t *testing.T) { + h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(200) + }) + httpSrv := httptest.NewServer(h) + defer httpSrv.Close() + + tmpf, err := ioutil.TempFile("", "") + if err != nil { + t.Fatal(err) + } + defer os.Remove(tmpf.Name()) + defer tmpf.Close() + + if err := json.NewEncoder(tmpf).Encode(&tracingConfig{ + ReportURL: httpSrv.URL, + Sample: "1.0", + }); err != nil { + t.Fatal(err) + } + + os.Setenv("TRACING_ENABLE", "1") + os.Setenv("TRACING_CONFIG", tmpf.Name()) + + Init() + + if !Enabled { + t.Fatal("tracing not enabled") + } +} -- GitLab