Skip to content

Commit

Permalink
feat: Injection trace into req.head (#1088)
Browse files Browse the repository at this point in the history
  • Loading branch information
tttoad authored Dec 10, 2024
1 parent 4458dfa commit beedba2
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
16 changes: 15 additions & 1 deletion pkg/infinity/headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package infinity

import (
"bytes"
"context"
"encoding/base64"
"fmt"
"mime/multipart"
"net/http"
"strings"

"github.com/grafana/grafana-infinity-datasource/pkg/models"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
)

const dummyHeader = "xxxxxxxx"
Expand Down Expand Up @@ -148,6 +151,16 @@ func ApplyForwardedOAuthIdentity(requestHeaders map[string]string, settings mode
return req
}

// ApplyTraceHead injecting trace
// https://opentelemetry.io/docs/specs/otel/context/api-propagators/#textmap-propagator
func ApplyTraceHead(ctx context.Context, req *http.Request) *http.Request {
prop := otel.GetTextMapPropagator()
if prop != nil {
prop.Inject(ctx, propagation.HeaderCarrier(req.Header))
}
return req
}

func getQueryReqHeader(requestHeaders map[string]string, headerName string) string {
for name, value := range requestHeaders {
if strings.EqualFold(headerName, name) {
Expand All @@ -156,4 +169,5 @@ func getQueryReqHeader(requestHeaders map[string]string, headerName string) stri
}

return ""
}
}

1 change: 1 addition & 0 deletions pkg/infinity/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func GetRequest(ctx context.Context, settings models.InfinitySettings, body io.R
req = ApplyBearerToken(settings, req, includeSect)
req = ApplyApiKeyAuth(settings, req, includeSect)
req = ApplyForwardedOAuthIdentity(requestHeaders, settings, req, includeSect)
req = ApplyTraceHead(ctx, req)
return req, err
}

Expand Down
72 changes: 72 additions & 0 deletions pkg/infinity/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ package infinity_test
import (
"context"
"fmt"
"net/http"
"testing"

"github.com/grafana/grafana-infinity-datasource/pkg/infinity"
"github.com/grafana/grafana-infinity-datasource/pkg/models"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
)

func Test_getQueryURL(t *testing.T) {
Expand Down Expand Up @@ -193,6 +197,7 @@ func Test_getQueryURL(t *testing.T) {
})
}
}

func TestClient_GetExecutedURL(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -266,3 +271,70 @@ func TestNormalizeURL(t *testing.T) {
})
}
}

const (
traceIDStr = "4bf92f3577b34da6a3ce929d0e0e4736"
spanIDStr = "00f067aa0ba902b7"
)

var (
traceID = mustTraceIDFromHex(traceIDStr)
spanID = mustSpanIDFromHex(spanIDStr)
)

func mustTraceIDFromHex(s string) (t trace.TraceID) {
var err error
t, err = trace.TraceIDFromHex(s)
if err != nil {
panic(err)
}
return
}

func mustSpanIDFromHex(s string) (t trace.SpanID) {
var err error
t, err = trace.SpanIDFromHex(s)
if err != nil {
panic(err)
}
return
}

func TestInjectTrace(t *testing.T) {
tests := []struct {
name string
sc trace.SpanContext
before func()
}{
{
name: "empty propagation",
before: func() {},
sc: trace.NewSpanContext(trace.SpanContextConfig{}),
},

{
name: "inject trace",
before: func() {
otel.SetTextMapPropagator(propagation.TraceContext{})
},
sc: trace.NewSpanContext(trace.SpanContextConfig{
TraceID: traceID,
SpanID: spanID,
Remote: true,
}),
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
test.before()
ctx := trace.ContextWithRemoteSpanContext(context.Background(), test.sc)
req, _ := http.NewRequest(http.MethodGet, "https://github.com/grafana", nil)
req = infinity.ApplyTraceHead(ctx, req)
if test.sc.HasTraceID() {
newCtx := otel.GetTextMapPropagator().Extract(context.Background(), propagation.HeaderCarrier(req.Header))
require.Equal(t, test.sc.TraceID(), trace.SpanFromContext(newCtx).SpanContext().TraceID())
}
})
}
}

0 comments on commit beedba2

Please sign in to comment.