| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package otlpreceiver // import "go.opentelemetry.io/collector/receiver/otlpreceiver" |
| 5 |
|
| 6 |
import ( |
| 7 |
"fmt" |
| 8 |
"io" |
| 9 |
"mime" |
| 10 |
"net/http" |
| 11 |
"strconv" |
| 12 |
"time" |
| 13 |
|
| 14 |
"google.golang.org/grpc/status" |
| 15 |
|
| 16 |
"go.opentelemetry.io/collector/internal/statusutil" |
| 17 |
"go.opentelemetry.io/collector/receiver/otlpreceiver/internal/errors" |
| 18 |
"go.opentelemetry.io/collector/receiver/otlpreceiver/internal/logs" |
| 19 |
"go.opentelemetry.io/collector/receiver/otlpreceiver/internal/metrics" |
| 20 |
"go.opentelemetry.io/collector/receiver/otlpreceiver/internal/profiles" |
| 21 |
"go.opentelemetry.io/collector/receiver/otlpreceiver/internal/trace" |
| 22 |
) |
| 23 |
|
| 24 |
// Pre-computed status with code=Internal to be used in case of a marshaling error. |
| 25 |
var fallbackMsg = []byte(`{"code": 13, "message": "failed to marshal error message"}`) |
| 26 |
|
| 27 |
const fallbackContentType = "application/json" |
| 28 |
|
| 29 |
func handleTraces(resp http.ResponseWriter, req *http.Request, tracesReceiver *trace.Receiver) { |
| 30 |
enc, ok := readContentType(resp, req) |
| 31 |
if !ok { |
| 32 |
return |
| 33 |
} |
| 34 |
|
| 35 |
body, ok := readAndCloseBody(resp, req, enc) |
| 36 |
if !ok { |
| 37 |
return |
| 38 |
} |
| 39 |
|
| 40 |
otlpReq, err := enc.unmarshalTracesRequest(body) |
| 41 |
if err != nil { |
| 42 |
writeError(resp, enc, err, http.StatusBadRequest) |
| 43 |
return |
| 44 |
} |
| 45 |
|
| 46 |
otlpResp, err := tracesReceiver.Export(req.Context(), otlpReq) |
| 47 |
if err != nil { |
| 48 |
writeError(resp, enc, err, http.StatusInternalServerError) |
| 49 |
return |
| 50 |
} |
| 51 |
|
| 52 |
msg, err := enc.marshalTracesResponse(otlpResp) |
| 53 |
if err != nil { |
| 54 |
writeError(resp, enc, err, http.StatusInternalServerError) |
| 55 |
return |
| 56 |
} |
| 57 |
writeResponse(resp, enc.contentType(), http.StatusOK, msg) |
| 58 |
} |
| 59 |
|
| 60 |
func handleMetrics(resp http.ResponseWriter, req *http.Request, metricsReceiver *metrics.Receiver) { |
| 61 |
enc, ok := readContentType(resp, req) |
| 62 |
if !ok { |
| 63 |
return |
| 64 |
} |
| 65 |
|
| 66 |
body, ok := readAndCloseBody(resp, req, enc) |
| 67 |
if !ok { |
| 68 |
return |
| 69 |
} |
| 70 |
|
| 71 |
otlpReq, err := enc.unmarshalMetricsRequest(body) |
| 72 |
if err != nil { |
| 73 |
writeError(resp, enc, err, http.StatusBadRequest) |
| 74 |
return |
| 75 |
} |
| 76 |
|
| 77 |
otlpResp, err := metricsReceiver.Export(req.Context(), otlpReq) |
| 78 |
if err != nil { |
| 79 |
writeError(resp, enc, err, http.StatusInternalServerError) |
| 80 |
return |
| 81 |
} |
| 82 |
|
| 83 |
msg, err := enc.marshalMetricsResponse(otlpResp) |
| 84 |
if err != nil { |
| 85 |
writeError(resp, enc, err, http.StatusInternalServerError) |
| 86 |
return |
| 87 |
} |
| 88 |
writeResponse(resp, enc.contentType(), http.StatusOK, msg) |
| 89 |
} |
| 90 |
|
| 91 |
func handleLogs(resp http.ResponseWriter, req *http.Request, logsReceiver *logs.Receiver) { |
| 92 |
enc, ok := readContentType(resp, req) |
| 93 |
if !ok { |
| 94 |
return |
| 95 |
} |
| 96 |
|
| 97 |
body, ok := readAndCloseBody(resp, req, enc) |
| 98 |
if !ok { |
| 99 |
return |
| 100 |
} |
| 101 |
|
| 102 |
otlpReq, err := enc.unmarshalLogsRequest(body) |
| 103 |
if err != nil { |
| 104 |
writeError(resp, enc, err, http.StatusBadRequest) |
| 105 |
return |
| 106 |
} |
| 107 |
|
| 108 |
otlpResp, err := logsReceiver.Export(req.Context(), otlpReq) |
| 109 |
if err != nil { |
| 110 |
writeError(resp, enc, err, http.StatusInternalServerError) |
| 111 |
return |
| 112 |
} |
| 113 |
|
| 114 |
msg, err := enc.marshalLogsResponse(otlpResp) |
| 115 |
if err != nil { |
| 116 |
writeError(resp, enc, err, http.StatusInternalServerError) |
| 117 |
return |
| 118 |
} |
| 119 |
writeResponse(resp, enc.contentType(), http.StatusOK, msg) |
| 120 |
} |
| 121 |
|
| 122 |
func handleProfiles(resp http.ResponseWriter, req *http.Request, profilesReceiver *profiles.Receiver) { |
| 123 |
enc, ok := readContentType(resp, req) |
| 124 |
if !ok { |
| 125 |
return |
| 126 |
} |
| 127 |
|
| 128 |
body, ok := readAndCloseBody(resp, req, enc) |
| 129 |
if !ok { |
| 130 |
return |
| 131 |
} |
| 132 |
|
| 133 |
otlpReq, err := enc.unmarshalProfilesRequest(body) |
| 134 |
if err != nil { |
| 135 |
writeError(resp, enc, err, http.StatusBadRequest) |
| 136 |
return |
| 137 |
} |
| 138 |
|
| 139 |
otlpResp, err := profilesReceiver.Export(req.Context(), otlpReq) |
| 140 |
if err != nil { |
| 141 |
writeError(resp, enc, err, http.StatusInternalServerError) |
| 142 |
return |
| 143 |
} |
| 144 |
|
| 145 |
msg, err := enc.marshalProfilesResponse(otlpResp) |
| 146 |
if err != nil { |
| 147 |
writeError(resp, enc, err, http.StatusInternalServerError) |
| 148 |
return |
| 149 |
} |
| 150 |
writeResponse(resp, enc.contentType(), http.StatusOK, msg) |
| 151 |
} |
| 152 |
|
| 153 |
func readContentType(resp http.ResponseWriter, req *http.Request) (encoder, bool) { |
| 154 |
if req.Method != http.MethodPost { |
| 155 |
handleUnmatchedMethod(resp) |
| 156 |
return nil, false |
| 157 |
} |
| 158 |
|
| 159 |
switch getMimeTypeFromContentType(req.Header.Get("Content-Type")) { |
| 160 |
case pbContentType: |
| 161 |
return pbEncoder, true |
| 162 |
case jsonContentType: |
| 163 |
return jsEncoder, true |
| 164 |
default: |
| 165 |
handleUnmatchedContentType(resp) |
| 166 |
return nil, false |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
func readAndCloseBody(resp http.ResponseWriter, req *http.Request, enc encoder) ([]byte, bool) { |
| 171 |
body, err := io.ReadAll(req.Body) |
| 172 |
if err != nil { |
| 173 |
writeError(resp, enc, err, http.StatusBadRequest) |
| 174 |
return nil, false |
| 175 |
} |
| 176 |
if err = req.Body.Close(); err != nil { |
| 177 |
writeError(resp, enc, err, http.StatusBadRequest) |
| 178 |
return nil, false |
| 179 |
} |
| 180 |
return body, true |
| 181 |
} |
| 182 |
|
| 183 |
// writeError encodes the HTTP error inside a rpc.Status message as required by the OTLP protocol. |
| 184 |
func writeError(w http.ResponseWriter, encoder encoder, err error, statusCode int) { |
| 185 |
s, ok := status.FromError(err) |
| 186 |
if ok { |
| 187 |
statusCode = errors.GetHTTPStatusCodeFromStatus(s) |
| 188 |
} else { |
| 189 |
s = statusutil.NewStatusFromMsgAndHTTPCode(err.Error(), statusCode) |
| 190 |
} |
| 191 |
writeStatusResponse(w, encoder, statusCode, s) |
| 192 |
} |
| 193 |
|
| 194 |
// errorHandler encodes the HTTP error message inside a rpc.Status message as required |
| 195 |
// by the OTLP protocol. |
| 196 |
func errorHandler(w http.ResponseWriter, r *http.Request, errMsg string, statusCode int) { |
| 197 |
s := statusutil.NewStatusFromMsgAndHTTPCode(errMsg, statusCode) |
| 198 |
contentType := r.Header.Get("Content-Type") |
| 199 |
if contentType == "" { |
| 200 |
contentType = fallbackContentType |
| 201 |
} |
| 202 |
switch getMimeTypeFromContentType(contentType) { |
| 203 |
case pbContentType: |
| 204 |
writeStatusResponse(w, pbEncoder, statusCode, s) |
| 205 |
return |
| 206 |
case jsonContentType: |
| 207 |
writeStatusResponse(w, jsEncoder, statusCode, s) |
| 208 |
return |
| 209 |
} |
| 210 |
writeResponse(w, fallbackContentType, http.StatusInternalServerError, fallbackMsg) |
| 211 |
} |
| 212 |
|
| 213 |
func writeStatusResponse(w http.ResponseWriter, enc encoder, statusCode int, st *status.Status) { |
| 214 |
// https://github.com/open-telemetry/opentelemetry-proto/blob/main/docs/specification.md#otlphttp-throttling |
| 215 |
if statusCode == http.StatusTooManyRequests || statusCode == http.StatusServiceUnavailable { |
| 216 |
retryInfo := statusutil.GetRetryInfo(st) |
| 217 |
// Check if server returned throttling information. |
| 218 |
if retryInfo != nil { |
| 219 |
// We are throttled. Wait before retrying as requested by the server. |
| 220 |
// The value of Retry-After field can be either an HTTP-date or a number of |
| 221 |
// seconds to delay after the response is received. See https://datatracker.ietf.org/doc/html/rfc7231#section-7.1.3 |
| 222 |
// |
| 223 |
// Retry-After = HTTP-date / delay-seconds |
| 224 |
// |
| 225 |
// Use delay-seconds since is easier to format as well as does not require clock synchronization. |
| 226 |
w.Header().Set("Retry-After", strconv.FormatInt(int64(retryInfo.GetRetryDelay().AsDuration()/time.Second), 10)) |
| 227 |
} |
| 228 |
} |
| 229 |
msg, err := enc.marshalStatus(st.Proto()) |
| 230 |
if err != nil { |
| 231 |
writeResponse(w, fallbackContentType, http.StatusInternalServerError, fallbackMsg) |
| 232 |
return |
| 233 |
} |
| 234 |
|
| 235 |
writeResponse(w, enc.contentType(), statusCode, msg) |
| 236 |
} |
| 237 |
|
| 238 |
func writeResponse(w http.ResponseWriter, contentType string, statusCode int, msg []byte) { |
| 239 |
w.Header().Set("Content-Type", contentType) |
| 240 |
w.WriteHeader(statusCode) |
| 241 |
// Nothing we can do with the error if we cannot write to the response. |
| 242 |
_, _ = w.Write(msg) |
| 243 |
} |
| 244 |
|
| 245 |
func getMimeTypeFromContentType(contentType string) string { |
| 246 |
mediatype, _, err := mime.ParseMediaType(contentType) |
| 247 |
if err != nil { |
| 248 |
return "" |
| 249 |
} |
| 250 |
return mediatype |
| 251 |
} |
| 252 |
|
| 253 |
func handleUnmatchedMethod(resp http.ResponseWriter) { |
| 254 |
hst := http.StatusMethodNotAllowed |
| 255 |
writeResponse(resp, "text/plain", hst, fmt.Appendf(nil, "%v method not allowed, supported: [POST]", hst)) |
| 256 |
} |
| 257 |
|
| 258 |
func handleUnmatchedContentType(resp http.ResponseWriter) { |
| 259 |
hst := http.StatusUnsupportedMediaType |
| 260 |
writeResponse(resp, "text/plain", hst, fmt.Appendf(nil, "%v unsupported media type, supported: [%s, %s]", hst, jsonContentType, pbContentType)) |
| 261 |
} |