-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
responses.go
74 lines (62 loc) · 2.21 KB
/
responses.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-2020 Datadog, Inc.
package api
import (
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/DataDog/datadog-agent/pkg/trace/metrics"
"github.com/DataDog/datadog-agent/pkg/trace/sampler"
"github.com/DataDog/datadog-agent/pkg/util/log"
)
const (
receiverErrorKey = "datadog.trace_agent.receiver.error"
)
// We encaspulate the answers in a container, this is to ease-up transition,
// should we add another fied.
type traceResponse struct {
// All the sampling rates recommended, by service
Rates map[string]float64 `json:"rate_by_service"`
}
// httpFormatError is used for payload format errors
func httpFormatError(w http.ResponseWriter, v Version, err error) {
log.Errorf("Rejecting client request: %v", err)
tags := []string{"error:format-error", "version:" + string(v)}
metrics.Count(receiverErrorKey, 1, tags, 1)
http.Error(w, err.Error(), http.StatusUnsupportedMediaType)
}
// httpDecodingError is used for errors happening in decoding
func httpDecodingError(err error, tags []string, w http.ResponseWriter) {
status := http.StatusBadRequest
errtag := "decoding-error"
msg := err.Error()
if err == ErrLimitedReaderLimitReached {
status = http.StatusRequestEntityTooLarge
errtag := "payload-too-large"
msg = errtag
}
tags = append(tags, fmt.Sprintf("error:%s", errtag))
metrics.Count(receiverErrorKey, 1, tags, 1)
http.Error(w, msg, status)
}
// httpOK is a dumb response for when things are a OK
func httpOK(w http.ResponseWriter) {
w.WriteHeader(http.StatusOK)
io.WriteString(w, "OK\n")
}
// httpRateByService outputs, as a JSON, the recommended sampling rates for all services.
func httpRateByService(w http.ResponseWriter, dynConf *sampler.DynamicConfig) {
w.Header().Set("Content-Type", "application/json")
response := traceResponse{
Rates: dynConf.RateByService.GetAll(), // this is thread-safe
}
encoder := json.NewEncoder(w)
if err := encoder.Encode(response); err != nil {
tags := []string{"error:response-error"}
metrics.Count(receiverErrorKey, 1, tags, 1)
return
}
}