MCPcopy Create free account
hub / github.com/cortexproject/cortex / Handler

Function Handler

pkg/util/push/push.go:49–200  ·  view source on GitHub ↗

Handler is a http.Handler which accepts WriteRequests.

(remoteWrite2Enabled bool, acceptUnknownRemoteWriteContentType bool, maxRecvMsgSize int, overrides *validation.Overrides, sourceIPs *middleware.SourceIPExtractor, push Func, requestTotal *prometheus.CounterVec)

Source from the content-addressed store, hash-verified

47
48// Handler is a http.Handler which accepts WriteRequests.
49func Handler(remoteWrite2Enabled bool, acceptUnknownRemoteWriteContentType bool, maxRecvMsgSize int, overrides *validation.Overrides, sourceIPs *middleware.SourceIPExtractor, push Func, requestTotal *prometheus.CounterVec) http.Handler {
50 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
51 ctx := r.Context()
52 logger := log.WithContext(ctx, log.Logger)
53 if sourceIPs != nil {
54 source := sourceIPs.Get(r)
55 if source != "" {
56 ctx = util.AddSourceIPsToOutgoingContext(ctx, source)
57 logger = log.WithSourceIPs(source, logger)
58 }
59 }
60
61 handlePRW1 := func() {
62 var req cortexpb.PreallocWriteRequest
63 err := util.ParseProtoReader(ctx, r.Body, int(r.ContentLength), maxRecvMsgSize, &req, util.RawSnappy)
64 if err != nil {
65 level.Error(logger).Log("err", err.Error())
66 http.Error(w, err.Error(), http.StatusBadRequest)
67 return
68 }
69
70 req.SkipLabelNameValidation = false
71 if req.Source == 0 {
72 req.Source = cortexpb.API
73 }
74
75 if _, err := push(ctx, &req.WriteRequest); err != nil {
76 resp, ok := httpgrpc.HTTPResponseFromError(err)
77 if !ok {
78 http.Error(w, err.Error(), http.StatusInternalServerError)
79 return
80 }
81 if resp.GetCode()/100 == 5 {
82 level.Error(logger).Log("msg", "push error", "err", err)
83 } else if resp.GetCode() != http.StatusAccepted && resp.GetCode() != http.StatusTooManyRequests {
84 level.Warn(logger).Log("msg", "push refused", "err", err)
85 }
86 http.Error(w, string(resp.Body), int(resp.Code))
87 }
88 }
89
90 handlePRW2 := func() {
91 userID, err := users.TenantID(ctx)
92 if err != nil {
93 return
94 }
95
96 req := cortexpb.PreallocWriteRequestV2FromPool()
97 // v1 request is put back into the pool by the Distributor.
98 defer cortexpb.ReuseWriteRequestV2(req)
99
100 err = util.ParseProtoReader(ctx, r.Body, int(r.ContentLength), maxRecvMsgSize, req, util.RawSnappy)
101 if err != nil {
102 level.Error(logger).Log("err", err.Error())
103 http.Error(w, err.Error(), http.StatusBadRequest)
104 return
105 }
106

Calls 15

WithContextFunction · 0.92
WithSourceIPsFunction · 0.92
ParseProtoReaderFunction · 0.92
TenantIDFunction · 0.92
ReuseWriteRequestV2Function · 0.92
convertV2RequestToV1Function · 0.85
setPRW2RespHeaderFunction · 0.85
getTypeLabelFunction · 0.85
GetCodeMethod · 0.80