Skip to content

Commit

Permalink
add skill request headers to log context
Browse files Browse the repository at this point in the history
  • Loading branch information
chrispatrick committed Aug 21, 2024
1 parent 0682613 commit 023c6e2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func CreateHttpHandler(handlers Handlers) func(http.ResponseWriter, *http.Reques

name := NameFromEvent(event)
ctx := context.Background()
logger := createLogger(ctx, event)
logger := createLogger(ctx, event, r.Header)
req := RequestContext{
Event: event,
Log: logger,
Expand Down
9 changes: 8 additions & 1 deletion log.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package skill
import (
"context"
"fmt"
"net/http"
"os"
"regexp"
"runtime"
Expand Down Expand Up @@ -87,7 +88,7 @@ type Logger struct {
Close func()
}

func createLogger(ctx context.Context, event EventIncoming) Logger {
func createLogger(ctx context.Context, event EventIncoming, headers http.Header) Logger {
logger := Logger{}

var gcpLogger *logging.Logger
Expand All @@ -104,6 +105,12 @@ func createLogger(ctx context.Context, event EventIncoming) Logger {
labels["workspace_id"] = event.WorkspaceId
labels["instance_id"] = instanceID

labels["request_id"] = headers.Get("X-Request-ID")
labels["forwarded_host"] = headers.Get("X-Forwarded-Host")
labels["original_forwarded_for"] = headers.Get("X-Original-Forwarded-For")
labels["cloud_trace_context"] = headers.Get("X-Cloud-Trace-Context")
labels["trace_parent"] = headers.Get("traceparent")

if projectID != "" {
client, _ = logging.NewClient(ctx, projectID)
gcpLogger = client.Logger("skill_logging")
Expand Down
2 changes: 1 addition & 1 deletion log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestSuccessfulLogging(t *testing.T) {
Logs: server.URL,
},
Token: "token",
})
}, http.Header{})
logger.Infof("This is a %s message", "test")
}

Expand Down

0 comments on commit 023c6e2

Please sign in to comment.