Skip to content

Commit db73fcf

Browse files
authored
feat(core): add new GetTraceId function (#3720)
1 parent 43846a3 commit db73fcf

File tree

5 files changed

+100
-1
lines changed

5 files changed

+100
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## Release (2025-XX-YY)
2+
- `core`: [v0.20.0](core/CHANGELOG.md#v0200)
3+
- **New:** Added new `GetTraceId` function
4+
15
## Release (2025-11-14)
26
- `core`:
37
- [v0.19.0](core/CHANGELOG.md#v0190)

core/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## v0.20.0
2+
- **New:** Added new `GetTraceId` function
3+
14
## v0.19.0
25
- **New:** Added new `EnumSliceToStringSlice ` util func
36

core/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v0.19.0
1+
v0.20.0

core/runtime/runtime.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import (
77
"github.com/stackitcloud/stackit-sdk-go/core/config"
88
)
99

10+
const (
11+
xTraceIdHeader = "x-trace-id"
12+
)
13+
1014
// WithCaptureHTTPResponse adds the raw HTTP response retrieval annotation to the parent context.
1115
// The resp parameter will contain the raw HTTP response after the request has completed.
1216
func WithCaptureHTTPResponse(parent context.Context, resp **http.Response) context.Context {
@@ -18,3 +22,16 @@ func WithCaptureHTTPResponse(parent context.Context, resp **http.Response) conte
1822
func WithCaptureHTTPRequest(parent context.Context, req **http.Request) context.Context {
1923
return context.WithValue(parent, config.ContextHTTPRequest, req)
2024
}
25+
26+
// GetTraceId returns the X-trace-id from the last response. If no trace-id can be found, it returns an empty string.
27+
// Prerequisite is, that WithCaptureHTTPResponse was executed before. It reads the X-trace-id header from the
28+
// attached http response within the context.
29+
func GetTraceId(ctx context.Context) string {
30+
var traceId string
31+
if resp, ok := ctx.Value(config.ContextHTTPResponse).(**http.Response); ok {
32+
if resp != nil && *resp != nil {
33+
traceId = (*resp).Header.Get(xTraceIdHeader)
34+
}
35+
}
36+
return traceId
37+
}

core/runtime/runtime_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package runtime
2+
3+
import (
4+
"context"
5+
"net/http"
6+
"testing"
7+
)
8+
9+
func TestGetTraceId(t *testing.T) {
10+
type args struct {
11+
ctx context.Context
12+
}
13+
tests := []struct {
14+
name string
15+
args args
16+
wantTraceId string
17+
}{
18+
{
19+
name: "with set traceId",
20+
args: args{
21+
ctx: func() context.Context {
22+
ctx := context.Background()
23+
header := http.Header{}
24+
header.Set(xTraceIdHeader, "my-trace-id")
25+
httpResp := &http.Response{
26+
Header: header,
27+
}
28+
ctx = WithCaptureHTTPResponse(ctx, &httpResp)
29+
return ctx
30+
}(),
31+
},
32+
wantTraceId: "my-trace-id",
33+
},
34+
{
35+
name: "without traceId header",
36+
args: args{
37+
ctx: func() context.Context {
38+
ctx := context.Background()
39+
httpResp := &http.Response{
40+
Header: http.Header{},
41+
}
42+
ctx = WithCaptureHTTPResponse(ctx, &httpResp)
43+
return ctx
44+
}(),
45+
},
46+
wantTraceId: "",
47+
},
48+
{
49+
name: "with empty response",
50+
args: args{
51+
ctx: func() context.Context {
52+
ctx := context.Background()
53+
var httpResp *http.Response
54+
ctx = WithCaptureHTTPResponse(ctx, &httpResp)
55+
return ctx
56+
}(),
57+
},
58+
wantTraceId: "",
59+
},
60+
{
61+
name: "without response in context",
62+
args: args{
63+
ctx: context.Background(),
64+
},
65+
wantTraceId: "",
66+
},
67+
}
68+
for _, tt := range tests {
69+
t.Run(tt.name, func(t *testing.T) {
70+
if gotTraceId := GetTraceId(tt.args.ctx); gotTraceId != tt.wantTraceId {
71+
t.Errorf("GetTraceId() = %v, want %v", gotTraceId, tt.wantTraceId)
72+
}
73+
})
74+
}
75+
}

0 commit comments

Comments
 (0)