-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathadapter.go
252 lines (217 loc) · 6.41 KB
/
adapter.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package datadog
import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"strings"
"time"
observe "github.com/dylibso/observe-sdk/go"
"github.com/dylibso/observe-sdk/go/adapter/datadog_formatter"
)
type DatadogConfig struct {
AgentHost string `json:"agent_host"`
ServiceName string `json:"service_name"`
DefaultTags map[string]string `json:"default_tags"`
TraceType datadog_formatter.DatadogTraceType `json:"trace_type"`
FlushPeriod time.Duration
BatchSize int
}
func DefaultDatadogConfig() *DatadogConfig {
return &DatadogConfig{
AgentHost: "http://localhost:8126",
ServiceName: "my-wasm-service",
DefaultTags: nil,
TraceType: datadog_formatter.Web,
FlushPeriod: 2 * time.Second,
BatchSize: 100,
}
}
type DatadogAdapter struct {
*observe.AdapterBase
Config *DatadogConfig
}
func NewDatadogAdapter(config *DatadogConfig) *DatadogAdapter {
if config == nil {
config = DefaultDatadogConfig()
}
base := observe.NewAdapterBase(config.BatchSize, config.FlushPeriod)
adapter := &DatadogAdapter{
AdapterBase: &base,
Config: config,
}
adapter.AdapterBase.SetFlusher(adapter)
return adapter
}
func (d *DatadogAdapter) Start(ctx context.Context) {
d.AdapterBase.Start(ctx, d)
}
func (d *DatadogAdapter) Stop(wait bool) {
d.AdapterBase.Stop(wait)
}
func (d *DatadogAdapter) HandleTraceEvent(e observe.TraceEvent) {
d.AdapterBase.HandleTraceEvent(e)
}
func (d *DatadogAdapter) Flush(evts []observe.TraceEvent) error {
log.Printf("Flushing %d events\n", len(evts))
var traces [][]*datadog_formatter.Span
for _, te := range evts {
var allSpans []*datadog_formatter.Span
for _, e := range te.Events {
switch event := e.(type) {
case observe.CallEvent:
traceId := te.TelemetryId.ToUint64()
spans := d.makeCallSpans(event, nil, traceId)
if len(spans) > 0 {
allSpans = append(allSpans, spans...)
}
case observe.MemoryGrowEvent:
log.Println("MemoryGrowEvent should be attached to a span")
case observe.CustomEvent:
log.Println("Datadog adapter does not respect custom events")
}
}
if len(allSpans) == 0 {
return nil
}
if te.AdapterMeta != nil {
if meta, ok := te.AdapterMeta.(DatadogMetadata); ok {
topSpan := allSpans[0]
if topSpan.Meta == nil {
topSpan.Meta = make(map[string]string)
}
if meta.ResourceName != nil {
topSpan.Resource = *meta.ResourceName
}
if meta.HttpUrl != nil {
topSpan.Meta["http.url"] = *meta.HttpUrl
}
if meta.HttpStatusCode != nil {
topSpan.Meta["http.status_code"] = fmt.Sprintf("%d", *meta.HttpStatusCode)
}
if meta.HttpClientIp != nil {
topSpan.Meta["http.client_ip"] = *meta.HttpClientIp
}
if meta.HttpRequestContentLength != nil {
topSpan.Meta["http.request.content_length"] = fmt.Sprintf("%d", *meta.HttpRequestContentLength)
}
if meta.HttpRequestContentLengthUncompressed != nil {
topSpan.Meta["http.request.content_length_uncompressed"] = fmt.Sprintf("%d", *meta.HttpRequestContentLengthUncompressed)
}
if meta.HttpResponseContentLength != nil {
topSpan.Meta["http.response.content_length"] = fmt.Sprintf("%d", *meta.HttpResponseContentLength)
}
if meta.HttpResponseContentLengthUncompressed != nil {
topSpan.Meta["http.response.content_length_uncompressed"] = fmt.Sprintf("%d", *meta.HttpResponseContentLengthUncompressed)
}
if meta.SpanKind != nil {
topSpan.Meta["span.kind"] = meta.SpanKind.String()
}
} else {
log.Println("The Datadog adapter was expecting a DatadogMetadata object on the trace")
}
}
tt := d.Config.TraceType.String()
allSpans[0].Type = &tt
traces = append(traces, allSpans)
}
output := datadog_formatter.New()
log.Println("formatted traces ", len(traces))
for _, trace := range traces {
output.AddTrace(trace)
}
b, err := json.Marshal(output)
if err != nil {
log.Println("failed to encode trace data to json", err)
return nil
}
data := bytes.NewBuffer(b)
url, err := url.JoinPath(d.Config.AgentHost, "v0.3", "traces")
if err != nil {
log.Println("failed to create datadog agent endpoint url:", err)
return nil
}
client := http.Client{
Timeout: time.Second * 2,
}
req, err := http.NewRequest("POST", url, data)
if err != nil {
log.Println("failed to create datadog endpoint url:", err)
}
req.Header = http.Header{
"content-type": {"application/json"},
}
resp, err := client.Do(req)
if err != nil {
log.Println("failed to send data to datadog", err)
}
if resp.StatusCode != http.StatusOK {
log.Println("unexpected status code from datadog:", resp.StatusCode)
}
return nil
}
func (d *DatadogAdapter) makeCallSpans(event observe.CallEvent, parentId *uint64, traceId uint64) []*datadog_formatter.Span {
name := event.FunctionName()
span := datadog_formatter.NewSpan(d.Config.ServiceName, traceId, parentId, name, event.Time, event.Time.Add(event.Duration))
spans := []*datadog_formatter.Span{span}
for _, ev := range event.Within() {
if call, ok := ev.(observe.CallEvent); ok {
spans = append(spans, d.makeCallSpans(call, &span.SpanId, traceId)...)
}
if alloc, ok := ev.(observe.MemoryGrowEvent); ok {
span := spans[len(spans)-1]
span.AddAllocation(alloc.MemoryGrowAmount())
}
if tags, ok := ev.(observe.SpanTagsEvent); ok {
span := spans[len(spans)-1]
for _, tag := range tags.Tags {
parts := strings.Split(tag, ":")
if len(parts) != 2 {
log.Printf("Invalid tag: %s\n", tag)
continue
}
span.AddTag(parts[0], parts[1])
}
}
}
return spans
}
type DatadogMetadata struct {
HttpUrl *string
HttpMethod *string
HttpStatusCode *int
ResourceName *string
HttpClientIp *string
HttpRequestContentLength *int
HttpRequestContentLengthUncompressed *int
HttpResponseContentLength *int
HttpResponseContentLengthUncompressed *int
SpanKind *DatadogSpanKind
}
type DatadogSpanKind int
const (
Server DatadogSpanKind = iota
Client
Producer
Consumer
Internal
)
func (d DatadogSpanKind) String() string {
switch d {
case Server:
return "server"
case Client:
return "client"
case Producer:
return "producer"
case Consumer:
return "consumer"
case Internal:
return "internal"
default:
return "unknown-span-kind"
}
}