Skip to content

Commit

Permalink
[TT-9504] Moving AddTraceID call to ProcessRequest function (TykTechn…
Browse files Browse the repository at this point in the history
…ologies#5425)

<!-- Provide a general summary of your changes in the Title above -->

## Description
Moving AddTraceID call to ProcessRequest function in order to write the
header no matter if the request to the upstream was made or not (i.e.
4xx cases, which fail because of authentication issues)
<!-- Describe your changes in detail -->

## Related Issue

https://tyktech.atlassian.net/jira/software/c/projects/TT/boards/42?selectedIssue=TT-9464
<!-- This project only accepts pull requests related to open issues. -->
<!-- If suggesting a new feature or change, please discuss it in an
issue first. -->
<!-- If fixing a bug, there should be an issue describing it with steps
to reproduce. -->
<!-- OSS: Please link to the issue here. Tyk: please create/link the
JIRA ticket. -->

## Motivation and Context

https://tyktech.atlassian.net/jira/software/c/projects/TT/boards/42?selectedIssue=TT-9464
<!-- Why is this change required? What problem does it solve? -->

## How This Has Been Tested
Manual testing (based on this Loom video:
https://www.loom.com/share/d43ed2aa81b843f5908e5b39a09033e7?sid=5c59e225-6dd1-4ac1-9a2e-80f35a2752ab)
and unit tests were already created for this function
<!-- Please describe in detail how you tested your changes -->
<!-- Include details of your testing environment, and the tests -->
<!-- you ran to see how your change affects other areas of the code,
etc. -->
<!-- This information is helpful for reviewers and QA. -->

## Screenshots (if appropriate)

## Types of changes

<!-- What types of changes does your code introduce? Put an `x` in all
the boxes that apply: -->

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Refactoring or add test (improvements in base code or adds test
coverage to functionality)

## Checklist

<!-- Go over all the following points, and put an `x` in all the boxes
that apply -->
<!-- If there are no documentation updates required, mark the item as
checked. -->
<!-- Raise up any additional concerns not covered by the checklist. -->

- [ ] I ensured that the documentation is up to date
- [ ] I explained why this PR updates go.mod in detail with reasoning
why it's required
- [ ] I would like a code coverage CI quality gate exception and have
explained why
  • Loading branch information
mativm02 authored Aug 17, 2023
1 parent c985ee4 commit a923516
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 23 deletions.
3 changes: 0 additions & 3 deletions gateway/handler_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"time"

"github.com/TykTechnologies/tyk/apidef"
"github.com/TykTechnologies/tyk/internal/otel"

"github.com/TykTechnologies/tyk-pump/analytics"
"github.com/TykTechnologies/tyk/config"
Expand Down Expand Up @@ -227,8 +226,6 @@ func (e *ErrorHandler) HandleError(w http.ResponseWriter, r *http.Request, errMs

ip := request.RealIP(r)

otel.AddTraceID(w, r, e.Spec.GlobalConfig.OpenTelemetry.Enabled)

if e.Spec.GlobalConfig.StoreAnalytics(ip) {

t := time.Now()
Expand Down
3 changes: 0 additions & 3 deletions gateway/handler_success.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"time"

"github.com/TykTechnologies/tyk/apidef"
"github.com/TykTechnologies/tyk/internal/otel"

"github.com/TykTechnologies/tyk-pump/analytics"
"github.com/TykTechnologies/tyk/config"
Expand Down Expand Up @@ -323,7 +322,6 @@ func (s *SuccessHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) *http
s.Spec.SanitizeProxyPaths(r)

addVersionHeader(w, r, s.Spec.GlobalConfig)
otel.AddTraceID(w, r, s.Spec.GlobalConfig.OpenTelemetry.Enabled)

t1 := time.Now()
resp := s.Proxy.ServeHTTP(w, r)
Expand Down Expand Up @@ -355,7 +353,6 @@ func (s *SuccessHandler) ServeHTTPWithCache(w http.ResponseWriter, r *http.Reque
millisec := DurationToMillisecond(time.Since(t1))

addVersionHeader(w, r, s.Spec.GlobalConfig)
otel.AddTraceID(w, r, s.Spec.GlobalConfig.OpenTelemetry.Enabled)

log.Debug("Upstream request took (ms): ", millisec)

Expand Down
1 change: 1 addition & 0 deletions gateway/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ 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 {
otel.AddTraceID(r.Context(), w)
var span otel.Span
if baseMw.Spec.DetailedTracing {
var ctx context.Context
Expand Down
10 changes: 4 additions & 6 deletions internal/otel/otel.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,9 @@ func SpanFromContext(ctx context.Context) tyktrace.Span {
return tyktrace.SpanFromContext(ctx)
}

func AddTraceID(w http.ResponseWriter, r *http.Request, otelEnabled bool) {
if otelEnabled {
span := SpanFromContext(r.Context())
if span.SpanContext().HasTraceID() {
w.Header().Set("X-Tyk-Trace-Id", span.SpanContext().TraceID().String())
}
func AddTraceID(ctx context.Context, w http.ResponseWriter) {
span := SpanFromContext(ctx)
if span.SpanContext().HasTraceID() {
w.Header().Set("X-Tyk-Trace-Id", span.SpanContext().TraceID().String())
}
}
13 changes: 2 additions & 11 deletions internal/otel/otel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,25 +268,16 @@ func TestContextWithSpan(t *testing.T) {
func TestAddTraceID(t *testing.T) {
tests := []struct {
name string
enabled bool
hasTraceID bool
wantHeader bool
}{
{
name: "otel enabled with trace id",
enabled: true,
hasTraceID: true,
wantHeader: true,
},
{
name: "otel enabled without trace id",
enabled: true,
hasTraceID: false,
wantHeader: false,
},
{
name: "otel disabled",
enabled: false,
hasTraceID: false,
wantHeader: false,
},
Expand All @@ -298,7 +289,7 @@ func TestAddTraceID(t *testing.T) {
w := httptest.NewRecorder()

otelConfig := Config{
Enabled: tt.enabled,
Enabled: true,
Exporter: "http",
Endpoint: "http://localhost:4317",
}
Expand All @@ -309,7 +300,7 @@ func TestAddTraceID(t *testing.T) {
req = req.WithContext(ctx)
}

AddTraceID(w, req, tt.enabled)
AddTraceID(req.Context(), w)

if tt.wantHeader && w.Header().Get("X-Tyk-Trace-Id") == "" {
t.Errorf("expected header to be set, but it wasn't")
Expand Down

0 comments on commit a923516

Please sign in to comment.