Skip to content
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

feat: Migrate from OpenTracing to OpenTelemetry #524

Closed
wants to merge 10 commits into from
Closed
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
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,27 +155,27 @@ If you wish logs to be written to a file, set `log_file` to a valid file path.

### Opentracing

Currently, only the Datadog tracer is supported.

```properties
GOTRUE_TRACING_ENABLED=true
GOTRUE_TRACING_EXPORTER=otlphttp
GOTRUE_TRACING_HOST=127.0.0.1
GOTRUE_TRACING_PORT=8126
GOTRUE_TRACING_TAGS="tag1:value1,tag2:value2"
GOTRUE_SERVICE_NAME="gotrue"
GOTRUE_TRACING_SERVICE_NAME="gotrue"
```

`TRACING_ENABLED` - `bool`
`TRACING_EXPORTER` - `string`

The selected exporter, must be one of: `prometheus`, `otlpgrpc` (Open Telemetry GRPC Collector), `otlphttp` (Open Telemetry HTTP Collector), `noop`.

Whether tracing is enabled or not. Defaults to `false`.
This defaults to `noop` so that no actions happen. If you pick an option other than `noop` tracing is enabled.

`TRACING_HOST` - `bool`

The tracing destination.
The tracing destination. If using `prometheus` this is used as the listening host.

`TRACING_PORT` - `bool`

The port for the tracing host.
The port for the tracing host. If using `prometheus` this is used as the listening port.

`TRACING_TAGS` - `string`

Expand Down
42 changes: 40 additions & 2 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package api

import (
"context"
"go.opentelemetry.io/otel"
otelcontroller "go.opentelemetry.io/otel/sdk/metric/controller/basic"
"net/http"
"os"
"os/signal"
Expand All @@ -20,6 +22,8 @@ import (
"github.com/rs/cors"
"github.com/sebest/xff"
"github.com/sirupsen/logrus"
"go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux"
otelglobal "go.opentelemetry.io/otel/metric/global"
)

const (
Expand All @@ -45,13 +49,47 @@ func (a *API) ListenAndServe(hostAndPort string) {
Handler: a.handler,
}

// Open Tracing GRPC requires some extra deferrals!
if a.config.Tracing.OtlpMetricExporter != nil {
defer a.config.Tracing.ContextCancel() // Defer GRPC Context Cancel
d := a.config.Tracing.OtlpMetricExporter
baseContext := context.Background()
defer func() {
ctx, cancel := context.WithTimeout(baseContext, time.Second)
defer cancel()
if err := d.Shutdown(ctx); err != nil {
otel.Handle(err)
}
}()
if pusher, ok := otelglobal.MeterProvider().(*otelcontroller.Controller); ok {
if err := pusher.Start(baseContext); err != nil {
log.Fatalf("could not start metric controller: %v", err)
}
defer func() {
ctx, cancel := context.WithTimeout(baseContext, time.Second)
defer cancel()
// pushes any last exports to the receiver
if err := pusher.Stop(ctx); err != nil {
otel.Handle(err)
}
}()
}
}
if a.config.Tracing.TracingShutdown != nil {
defer func() {
if err := a.config.Tracing.TracingShutdown(context.Background()); err != nil {
log.Fatal("failed to shutdown TracerProvider: %w", err)
}
}()
}

done := make(chan struct{})
defer close(done)
go func() {
waitForTermination(log, done)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
server.Shutdown(ctx)
_ = server.Shutdown(ctx)
}()

if err := server.ListenAndServe(); err != http.ErrServerClosed {
Expand Down Expand Up @@ -87,7 +125,7 @@ func NewAPIWithVersion(ctx context.Context, globalConfig *conf.GlobalConfigurati
r.UseBypass(xffmw.Handler)
r.Use(addRequestID(globalConfig))
r.Use(recoverer)
r.UseBypass(tracer)
r.UseBypass(otelmux.Middleware(globalConfig.Tracing.ServiceName))

r.Get("/health", api.HealthCheck)

Expand Down
65 changes: 0 additions & 65 deletions api/tracer.go

This file was deleted.

56 changes: 40 additions & 16 deletions api/tracer_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package api

import (
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.10.0"
"net/http"
"net/http/httptest"
"testing"

"github.com/gofrs/uuid"
"github.com/netlify/gotrue/conf"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/mocktracer"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"go.opentelemetry.io/otel/sdk/trace/tracetest"
)

type TracerTestSuite struct {
Expand All @@ -36,28 +39,49 @@ func TestTracer(t *testing.T) {
suite.Run(t, ts)
}

func getAttribute(attributes []attribute.KeyValue, key attribute.Key) *attribute.Value {
for _, value := range attributes {
if value.Key == key {
return &value.Value
}
}

return nil
}

func (ts *TracerTestSuite) TestTracer_Spans() {
mt := mocktracer.New()
opentracing.SetGlobalTracer(mt)
exporter := tracetest.NewInMemoryExporter()
bsp := sdktrace.NewSimpleSpanProcessor(exporter)
traceProvider := sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithSpanProcessor(bsp),
)
otel.SetTracerProvider(traceProvider)

w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "http://localhost/something1", nil)
ts.API.handler.ServeHTTP(w, req)
req = httptest.NewRequest(http.MethodGet, "http://localhost/something2", nil)
ts.API.handler.ServeHTTP(w, req)

spans := mt.FinishedSpans()
spanStubs := exporter.GetSpans()
spans := spanStubs.Snapshots()

if assert.Equal(ts.T(), 2, len(spans)) {
assert.Equal(ts.T(), "POST", spans[0].Tag("http.method"))
assert.Equal(ts.T(), "/something1", spans[0].Tag("http.url"))
assert.Equal(ts.T(), "POST /something1", spans[0].Tag("resource.name"))
assert.Equal(ts.T(), "404", spans[0].Tag("http.status_code"))
assert.NotEmpty(ts.T(), spans[0].Tag("http.request_id"))

assert.Equal(ts.T(), "GET", spans[1].Tag("http.method"))
assert.Equal(ts.T(), "/something2", spans[1].Tag("http.url"))
assert.Equal(ts.T(), "GET /something2", spans[1].Tag("resource.name"))
assert.Equal(ts.T(), "404", spans[1].Tag("http.status_code"))
assert.NotEmpty(ts.T(), spans[1].Tag("http.request_id"))
attributes1 := spans[0].Attributes()
method1 := getAttribute(attributes1, semconv.HTTPMethodKey)
assert.Equal(ts.T(), "POST", method1.AsString())
url1 := getAttribute(attributes1, semconv.HTTPTargetKey)
assert.Equal(ts.T(), "http://localhost/something1", url1.AsString())
statusCode1 := getAttribute(attributes1, semconv.HTTPStatusCodeKey)
assert.Equal(ts.T(), int64(404), statusCode1.AsInt64())

attributes2 := spans[1].Attributes()
method2 := getAttribute(attributes2, semconv.HTTPMethodKey)
assert.Equal(ts.T(), "GET", method2.AsString())
url2 := getAttribute(attributes2, semconv.HTTPTargetKey)
assert.Equal(ts.T(), "http://localhost/something2", url2.AsString())
statusCode2 := getAttribute(attributes2, semconv.HTTPStatusCodeKey)
assert.Equal(ts.T(), int64(404), statusCode2.AsInt64())
}
}
9 changes: 5 additions & 4 deletions conf/configuration_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package conf

import (
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/trace"
"os"
"testing"

"github.com/opentracing/opentracing-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand All @@ -29,16 +30,16 @@ func TestTracing(t *testing.T) {
os.Setenv("GOTRUE_DB_DRIVER", "mysql")
os.Setenv("GOTRUE_DB_DATABASE_URL", "fake")
os.Setenv("GOTRUE_OPERATOR_TOKEN", "token")
os.Setenv("GOTRUE_TRACING_TRACING_EXPORTER", "noop")
os.Setenv("GOTRUE_TRACING_SERVICE_NAME", "identity")
os.Setenv("GOTRUE_TRACING_PORT", "8126")
os.Setenv("GOTRUE_TRACING_HOST", "127.0.0.1")
os.Setenv("GOTRUE_TRACING_TAGS", "tag1:value1,tag2:value2")

gc, _ := LoadGlobal("")
tc := opentracing.GlobalTracer()
tc := otel.GetTracerProvider()

assert.Equal(t, opentracing.NoopTracer{}, tc)
assert.Equal(t, false, gc.Tracing.Enabled)
assert.NotNil(t, trace.NewNoopTracerProvider(), tc)
assert.Equal(t, "identity", gc.Tracing.ServiceName)
assert.Equal(t, "8126", gc.Tracing.Port)
assert.Equal(t, "127.0.0.1", gc.Tracing.Host)
Expand Down
Loading