-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.go
161 lines (129 loc) · 3.55 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package slogdatadog
import (
"context"
"encoding/json"
"fmt"
"strings"
"time"
"log/slog"
slogcommon "github.com/samber/slog-common"
"github.com/DataDog/datadog-api-client-go/v2/api/datadog"
"github.com/DataDog/datadog-api-client-go/v2/api/datadogV2"
)
type Option struct {
// log level (default: debug)
Level slog.Leveler
// datadog endpoint
Client *datadog.APIClient
Context context.Context
Timeout time.Duration // default: 10s
// batching (default: disabled)
// Batching bool // @TODO
// source parameters
Service string
Hostname string
GlobalTags map[string]string
// optional: customize Datadog message builder
Converter Converter
// optional: custom marshaler
Marshaler func(v any) ([]byte, error)
// optional: fetch attributes from context
AttrFromContext []func(ctx context.Context) []slog.Attr
// optional: see slog.HandlerOptions
AddSource bool
ReplaceAttr func(groups []string, a slog.Attr) slog.Attr
}
func (o Option) NewDatadogHandler() slog.Handler {
if o.Level == nil {
o.Level = slog.LevelDebug
}
if o.Client == nil {
panic("missing Datadog client")
}
if o.Context == nil {
o.Context = context.Background()
}
if o.Timeout == 0 {
o.Timeout = 10 * time.Second
}
if o.Converter == nil {
o.Converter = DefaultConverter
}
if o.Marshaler == nil {
o.Marshaler = json.Marshal
}
if o.AttrFromContext == nil {
o.AttrFromContext = []func(ctx context.Context) []slog.Attr{}
}
return &DatadogHandler{
option: o,
attrs: []slog.Attr{},
groups: []string{},
}
}
var _ slog.Handler = (*DatadogHandler)(nil)
type DatadogHandler struct {
option Option
attrs []slog.Attr
groups []string
}
func (h *DatadogHandler) Enabled(_ context.Context, level slog.Level) bool {
return level >= h.option.Level.Level()
}
func (h *DatadogHandler) Handle(ctx context.Context, record slog.Record) error {
bytes, err := handle(h, ctx, record)
if err != nil {
return err
}
// non-blocking
go func() {
// @TODO: batching
_ = h.send(string(bytes))
}()
return nil
}
func handle(h *DatadogHandler, ctx context.Context, record slog.Record) ([]byte, error) {
fromContext := slogcommon.ContextExtractor(ctx, h.option.AttrFromContext)
log := h.option.Converter(h.option.AddSource, h.option.ReplaceAttr, append(h.attrs, fromContext...), h.groups, &record)
return h.option.Marshaler(log)
}
func (h *DatadogHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
return &DatadogHandler{
option: h.option,
attrs: slogcommon.AppendAttrsToGroup(h.groups, h.attrs, attrs...),
groups: h.groups,
}
}
func (h *DatadogHandler) WithGroup(name string) slog.Handler {
// https://cs.opensource.google/go/x/exp/+/46b07846:slog/handler.go;l=247
if name == "" {
return h
}
return &DatadogHandler{
option: h.option,
attrs: h.attrs,
groups: append(h.groups, name),
}
}
func (h *DatadogHandler) send(message string) error {
var tags []string
if h.option.GlobalTags != nil {
for key, value := range h.option.GlobalTags {
tags = append(tags, fmt.Sprintf("%v:%v", key, value))
}
}
body := []datadogV2.HTTPLogItem{
{
Ddsource: datadog.PtrString(name),
Hostname: datadog.PtrString(h.option.Hostname),
Service: datadog.PtrString(h.option.Service),
Ddtags: datadog.PtrString(strings.Join(tags, ",")),
Message: message,
},
}
ctx, cancel := context.WithTimeout(h.option.Context, h.option.Timeout)
defer cancel()
api := datadogV2.NewLogsApi(h.option.Client)
_, _, err := api.SubmitLog(ctx, body, *datadogV2.NewSubmitLogOptionalParameters().WithContentEncoding(datadogV2.CONTENTENCODING_DEFLATE))
return err
}