|
| 1 | +// Unless explicitly stated otherwise all files in this repository are licensed |
| 2 | +// under the Apache License Version 2.0. |
| 3 | +// This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 4 | +// Copyright 2016-present Datadog, Inc. |
| 5 | + |
| 6 | +package httpimpl |
| 7 | + |
| 8 | +import ( |
| 9 | + "fmt" |
| 10 | + "io" |
| 11 | + "net/http" |
| 12 | + |
| 13 | + log "github.com/DataDog/datadog-agent/comp/core/log/def" |
| 14 | + tagger "github.com/DataDog/datadog-agent/comp/core/tagger/def" |
| 15 | + pb "github.com/DataDog/datadog-agent/pkg/proto/pbgo/dogstatsdhttp" |
| 16 | +) |
| 17 | + |
| 18 | +type requestCtx struct { |
| 19 | + prefix string |
| 20 | + log log.Component |
| 21 | + writer http.ResponseWriter |
| 22 | +} |
| 23 | + |
| 24 | +func (ctx *requestCtx) debugf(format string, args ...any) { |
| 25 | + ctx.log.Debugf(ctx.prefix+format, args...) |
| 26 | +} |
| 27 | + |
| 28 | +func (ctx *requestCtx) errorf(format string, args ...any) { |
| 29 | + ctx.log.Errorf(ctx.prefix+format, args...) |
| 30 | +} |
| 31 | + |
| 32 | +func (ctx *requestCtx) respond(status int, format string, args ...any) { |
| 33 | + msg := fmt.Sprintf(format, args...) |
| 34 | + ctx.debugf("complete with status %d: %q", status, msg) |
| 35 | + ctx.writer.WriteHeader(status) |
| 36 | + _, err := ctx.writer.Write([]byte(msg)) |
| 37 | + if err != nil { |
| 38 | + ctx.debugf("failed to write response: %v", err) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +type handlerBase struct { |
| 43 | + log log.Component |
| 44 | + tagger tagger.Component |
| 45 | + hostname string |
| 46 | + out serializer |
| 47 | +} |
| 48 | + |
| 49 | +type seriesHandler struct { |
| 50 | + handlerBase |
| 51 | +} |
| 52 | + |
| 53 | +func (h *seriesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 54 | + ctx := requestCtx{ |
| 55 | + prefix: fmt.Sprintf("dogstatsdhttp %q: ", r.RemoteAddr), |
| 56 | + log: h.log, |
| 57 | + writer: w, |
| 58 | + } |
| 59 | + |
| 60 | + origin, err := originFromHeader(r.Header, h.tagger) |
| 61 | + if err != nil { |
| 62 | + ctx.respond(http.StatusBadRequest, "origin detection error: %v", err) |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + body, err := io.ReadAll(r.Body) |
| 67 | + r.Body.Close() |
| 68 | + if err != nil { |
| 69 | + ctx.respond(http.StatusBadRequest, "error reading body: %v", err) |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + var payload pb.Payload |
| 74 | + err = payload.UnmarshalVT(body) |
| 75 | + if err != nil { |
| 76 | + ctx.respond(http.StatusBadRequest, "error parsing payload: %v", err) |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + it, err := newSeriesIterator(&payload, origin, h.hostname) |
| 81 | + if err != nil { |
| 82 | + ctx.respond(http.StatusBadRequest, "error decoding payload dictionaries: %v", err) |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + err = h.out.SendIterableSeries(it) |
| 87 | + if err != nil { |
| 88 | + // this error indicates a failure in the agent itself, so we don't |
| 89 | + // propagate it to the client |
| 90 | + ctx.errorf("failed to serialize payload: %v", err) |
| 91 | + ctx.respond(http.StatusInternalServerError, "internal error") |
| 92 | + return |
| 93 | + } |
| 94 | + |
| 95 | + if it.err != nil { |
| 96 | + ctx.respond(http.StatusBadRequest, "error decoding payload: %v", it.err) |
| 97 | + return |
| 98 | + } |
| 99 | + |
| 100 | + ctx.respond(http.StatusOK, "OK") |
| 101 | +} |
0 commit comments