Skip to content

Commit

Permalink
Merge pull request #129 from atomist-skills/add-request-headers-to-lo…
Browse files Browse the repository at this point in the history
…g-context

add skill request headers to log context
  • Loading branch information
chrispatrick authored Aug 21, 2024
2 parents 0682613 + 7dc3be8 commit d252c57
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
5 changes: 4 additions & 1 deletion handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func Start(handlers Handlers) {

func CreateHttpHandler(handlers Handlers) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
handleStart := time.Now()
buf := new(strings.Builder)
rc := r.Body
defer rc.Close()
Expand All @@ -65,14 +66,16 @@ 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,

ctx: ctx,
}

logger.Debugf("Skill request parsed in %d ms", time.Now().UnixMilli()-handleStart.UnixMilli())

defer func() {
if err := recover(); err != nil {
sendStatus(ctx, req, Status{
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 d252c57

Please sign in to comment.