Skip to content
Snippets Groups Projects
Commit ae4e0887 authored by ale's avatar ale
Browse files

Add simple tests for the tracing pkg

parent 2c5f006b
No related branches found
No related tags found
No related merge requests found
...@@ -25,6 +25,7 @@ require ( ...@@ -25,6 +25,7 @@ require (
go.opentelemetry.io/otel v1.9.1-0.20220810155701-d96e8d2912af go.opentelemetry.io/otel v1.9.1-0.20220810155701-d96e8d2912af
go.opentelemetry.io/otel/exporters/zipkin v1.9.0 go.opentelemetry.io/otel/exporters/zipkin v1.9.0
go.opentelemetry.io/otel/sdk 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/crypto v0.0.0-20220622213112-05595931fe9d
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4
) )
...@@ -15,12 +15,16 @@ import ( ...@@ -15,12 +15,16 @@ import (
"go.opentelemetry.io/otel" "go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/zipkin" "go.opentelemetry.io/otel/exporters/zipkin"
"go.opentelemetry.io/otel/sdk/trace" "go.opentelemetry.io/otel/sdk/trace"
apitrace "go.opentelemetry.io/otel/trace"
) )
var ( var (
// Enabled reports whether tracing is globally enabled or not. // Enabled reports whether tracing is globally enabled or not.
Enabled bool Enabled bool
// Global Tracer instance.
Tracer apitrace.Tracer
// The active tracing configuration, if Enabled is true. // The active tracing configuration, if Enabled is true.
config tracingConfig config tracingConfig
...@@ -37,7 +41,7 @@ type tracingConfig struct { ...@@ -37,7 +41,7 @@ type tracingConfig struct {
// Read the global tracing configuration file. Its location is // Read the global tracing configuration file. Its location is
// hardcoded, but it can be overriden using the TRACING_CONFIG // hardcoded, but it can be overriden using the TRACING_CONFIG
// environment variable. // environment variable.
func readTracingConfig() error { func readTracingConfig() (*tracingConfig, error) {
// Read and decode configuration. // Read and decode configuration.
cfgPath := globalTracingConfigPath cfgPath := globalTracingConfigPath
if s := os.Getenv("TRACING_CONFIG"); s != "" { if s := os.Getenv("TRACING_CONFIG"); s != "" {
...@@ -45,20 +49,21 @@ func readTracingConfig() error { ...@@ -45,20 +49,21 @@ func readTracingConfig() error {
} }
data, err := ioutil.ReadFile(cfgPath) data, err := ioutil.ReadFile(cfgPath)
if err != nil { if err != nil {
return err return nil, err
} }
var config tracingConfig
if err := json.Unmarshal(data, &config); err != nil { if err := json.Unmarshal(data, &config); err != nil {
log.Printf("warning: error in tracing configuration: %v, tracing disabled", err) log.Printf("warning: error in tracing configuration: %v, tracing disabled", err)
return err return nil, err
} }
if config.ReportURL == "" { if config.ReportURL == "" {
log.Printf("warning: tracing configuration contains no report_url, tracing disabled") 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 // Compute the service name for Zipkin: this is usually the program
...@@ -78,24 +83,18 @@ func getServiceName() string { ...@@ -78,24 +83,18 @@ func getServiceName() string {
// We need to check the configuration as soon as possible, because // We need to check the configuration as soon as possible, because
// it's likely that client transports are created before HTTP servers, // it's likely that client transports are created before HTTP servers,
// and we need to wrap them with opencensus at creation time. // and we need to wrap them with opencensus at creation time.
func init() { func initTracing(endpointAddr string) {
initOnce.Do(func() {
// Kill switch from environment. // Kill switch from environment.
if s := os.Getenv("TRACING_ENABLE"); s == "0" { if s := os.Getenv("TRACING_ENABLE"); s == "0" {
return return
} }
if err := readTracingConfig(); err != nil { config, err := readTracingConfig()
if err != nil {
return return
} }
Enabled = true
}
func initTracing(endpointAddr string) {
if !Enabled {
return
}
initOnce.Do(func() {
ze, err := zipkin.New(config.ReportURL) ze, err := zipkin.New(config.ReportURL)
if err != nil { if err != nil {
log.Printf("error creating Zipkin exporter: %v", err) log.Printf("error creating Zipkin exporter: %v", err)
...@@ -123,6 +122,7 @@ func initTracing(endpointAddr string) { ...@@ -123,6 +122,7 @@ func initTracing(endpointAddr string) {
) )
otel.SetTracerProvider(tp) otel.SetTracerProvider(tp)
Tracer = tp.Tracer(endpointAddr)
log.Printf("tracing enabled (report_url %s)", config.ReportURL) log.Printf("tracing enabled (report_url %s)", config.ReportURL)
......
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")
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment