Skip to content

Commit

Permalink
moving imports to internal package
Browse files Browse the repository at this point in the history
  • Loading branch information
mativm02 committed Jul 24, 2023
1 parent c3284a5 commit 6b84656
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
19 changes: 11 additions & 8 deletions gateway/api_loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,6 @@ func TestAllApisAreMTLS(t *testing.T) {
}

func TestOpenTelemetry(t *testing.T) {

t.Run("Opentelemetry enabled - check if we are sending traces", func(t *testing.T) {
otelCollectorMock := httpCollectorMock(t, func(w http.ResponseWriter, r *http.Request) {
//check the body
Expand Down Expand Up @@ -466,15 +465,19 @@ func TestOpenTelemetry(t *testing.T) {
globalConf.OpenTelemetry.SpanProcessorType = "simple"
})
defer ts.Close()
detailedTracing := []bool{true, false}
for _, detailed := range detailedTracing {
ts.Gw.BuildAndLoadAPI(func(spec *APISpec) {
spec.APIID = "test"
spec.Proxy.ListenPath = "/my-api/"
spec.UseKeylessAccess = true
spec.DetailedTracing = detailed
})

ts.Gw.BuildAndLoadAPI(func(spec *APISpec) {
spec.APIID = "test"
spec.Proxy.ListenPath = "/my-api/"
spec.UseKeylessAccess = true
})
_, _ = ts.Run(t, test.TestCase{Path: "/my-api/", Code: http.StatusOK})
assert.Equal(t, "otel", ts.Gw.TracerProvider.Type())
}

_, _ = ts.Run(t, test.TestCase{Path: "/my-api/", Code: http.StatusOK})
assert.Equal(t, "otel", ts.Gw.TracerProvider.Type())
})

t.Run("Opentelemetry disabled - check if we are not sending traces", func(t *testing.T) {
Expand Down
6 changes: 2 additions & 4 deletions gateway/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ import (
"github.com/TykTechnologies/tyk/storage"
"github.com/TykTechnologies/tyk/trace"
"github.com/TykTechnologies/tyk/user"

tyktrace "github.com/TykTechnologies/opentelemetry/trace"
)

const (
Expand Down Expand Up @@ -71,14 +69,14 @@ func (tr TraceMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Request,
} else if baseMw := tr.Base(); baseMw != nil {
cfg := baseMw.Gw.GetConfig()
if cfg.OpenTelemetry.Enabled {
var span tyktrace.Span
var span otel.Span
if baseMw.Spec.DetailedTracing {
var ctx context.Context
ctx, span = baseMw.Gw.TracerProvider.Tracer().Start(r.Context(), tr.Name())
defer span.End()
setContext(r, ctx)
} else {
span = tyktrace.SpanFromContext(r.Context())
span = otel.SpanFromContext(r.Context())
}

err, i := tr.TykMiddleware.ProcessRequest(w, r, conf)
Expand Down
6 changes: 6 additions & 0 deletions internal/otel/otel.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type (
Sampling = otelconfig.Sampling

SpanAttribute = tyktrace.Attribute

Span = tyktrace.Span
)

// HTTP Handlers
Expand Down Expand Up @@ -117,3 +119,7 @@ var APIKeyAttribute = semconv.TykAPIKey
var APIKeyAliasAttribute = semconv.TykAPIKeyAlias

var OAuthClientIDAttribute = semconv.TykOauthID

func SpanFromContext(ctx context.Context) tyktrace.Span {
return tyktrace.SpanFromContext(ctx)
}

0 comments on commit 6b84656

Please sign in to comment.