Skip to content

Commit 016a935

Browse files
committed
link logs to traces
1 parent acc5f45 commit 016a935

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

backend/config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
tracing:
22
enabled: false
33
environment: "local"
4+
version: "local-dev"

backend/log/context.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package log
33
import (
44
"context"
55

6+
"github.com/DataDog/dd-trace-go/v2/ddtrace/tracer"
67
"go.uber.org/zap"
78

89
"csh/cluesheet/config"
@@ -38,8 +39,22 @@ func ContextWithLogger(ctx context.Context, logger *zap.Logger) context.Context
3839

3940
func FromContext(ctx context.Context) *zap.Logger {
4041
l := ctx.Value(contextKey)
42+
var logger *zap.Logger
4143
if l == nil {
42-
return zap.L()
44+
logger = zap.L()
45+
} else {
46+
logger = l.(*zap.Logger)
47+
}
48+
49+
config := config.FromContext(ctx)
50+
if span, ok := tracer.SpanFromContext(ctx); ok {
51+
spanContext := span.Context()
52+
logger = logger.With(
53+
zap.String("dd.trace_id", spanContext.TraceID()),
54+
zap.Uint64("dd.span_id", spanContext.SpanID()),
55+
zap.String("dd.version", config.GetString("version")),
56+
zap.String("dd.env", config.GetString("environment")),
57+
)
4358
}
44-
return l.(*zap.Logger)
59+
return logger
4560
}

backend/main.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ func main() {
286286
tx, err := conn.Begin(r.Context())
287287
if err != nil {
288288
http.Error(rw, "failed to store clue", 500)
289-
fmt.Printf(err.Error())
289+
fmt.Println(err.Error())
290290
return
291291
}
292292
defer tx.Rollback(r.Context())
@@ -303,7 +303,7 @@ func main() {
303303
)
304304
if err != nil {
305305
http.Error(rw, "failed to store clue", 500)
306-
fmt.Printf(err.Error())
306+
fmt.Println(err.Error())
307307
return
308308
}
309309

@@ -323,25 +323,25 @@ func main() {
323323
_, err = tx.Exec(r.Context(), `insert into clue_relation(id, parent_id, child_id) values ($1, $2, $3)`, cr.Id, cr.Parent_id, cr.Child_id)
324324
if err != nil {
325325
http.Error(rw, "failed to store clue parent", 500)
326-
fmt.Printf(err.Error())
326+
fmt.Println(err.Error())
327327
return
328328
}
329329
}
330330

331331
err = tx.Commit(r.Context())
332332
if err != nil {
333333
http.Error(rw, "failed to store clue with parent", 500)
334-
fmt.Printf(err.Error())
334+
fmt.Println(err.Error())
335335
return
336336
}
337337

338338
data, err := json.Marshal(struct {
339339
Clue Clue
340-
ClueRelation *ClueRelation `json:,omitempty,omitzero`
340+
ClueRelation *ClueRelation `json:",omitempty,omitzero"`
341341
}{Clue: newClue, ClueRelation: cr})
342342
if err != nil {
343343
http.Error(rw, "Failed to marshal data", 500)
344-
fmt.Printf(err.Error())
344+
fmt.Println(err.Error())
345345
return
346346
}
347347

backend/model.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type Cluesheet struct {
2828
Visibility string /* TODO: int key? or defined enum? I think SQL enums are difficult to work with in migrations */
2929
Owners []string
3030
Groups []string
31-
Clues *[]Clue
31+
Clues *[]Clue `json:",omitzero"`
3232
}
3333

3434
type Clue struct {
@@ -42,7 +42,7 @@ type Clue struct {
4242
Edited_by string
4343
Edited_at time.Time
4444
Tags []string
45-
Children []*Clue
45+
Children []*Clue `json:",omitzero"`
4646
}
4747

4848
type ClueRelation struct {

0 commit comments

Comments
 (0)