-
Notifications
You must be signed in to change notification settings - Fork 3.7k
fix(otlp): Write protobuf status on error #15097
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
fix(otlp): Write status on error
- Loading branch information
commit a12cf8c2549fc586211e87e140f3cbbbb3694d07
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,17 +16,19 @@ import ( | |
|
||
"github.com/dustin/go-humanize" | ||
"github.com/go-kit/log" | ||
"github.com/gogo/protobuf/proto" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can this be gogo proto? i am not sure how different this is from the google version There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately not. I tried but panics. |
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promauto" | ||
"github.com/prometheus/prometheus/model/labels" | ||
|
||
loki_util "github.com/grafana/loki/v3/pkg/util" | ||
"google.golang.org/grpc/codes" | ||
grpcstatus "google.golang.org/grpc/status" | ||
|
||
"github.com/grafana/loki/v3/pkg/analytics" | ||
"github.com/grafana/loki/v3/pkg/loghttp" | ||
"github.com/grafana/loki/v3/pkg/logproto" | ||
"github.com/grafana/loki/v3/pkg/logql/syntax" | ||
"github.com/grafana/loki/v3/pkg/util" | ||
loki_util "github.com/grafana/loki/v3/pkg/util" | ||
"github.com/grafana/loki/v3/pkg/util/constants" | ||
"github.com/grafana/loki/v3/pkg/util/unmarshal" | ||
unmarshal2 "github.com/grafana/loki/v3/pkg/util/unmarshal/legacy" | ||
|
@@ -86,6 +88,7 @@ func (EmptyLimits) DiscoverServiceName(string) []string { | |
type ( | ||
RequestParser func(userID string, r *http.Request, tenantsRetention TenantsRetention, limits Limits, tracker UsageTracker, logPushRequestStreams bool, logger log.Logger) (*logproto.PushRequest, *Stats, error) | ||
RequestParserWrapper func(inner RequestParser) RequestParser | ||
ErrorWriter func(w http.ResponseWriter, error string, code int, logger log.Logger) | ||
) | ||
|
||
type Stats struct { | ||
|
@@ -307,3 +310,62 @@ func RetentionPeriodToString(retentionPeriod time.Duration) string { | |
} | ||
return retentionHours | ||
} | ||
|
||
// OTLPError writes an OTLP-compliant error response to the given http.ResponseWriter. | ||
// | ||
// According to the OTLP spec: https://opentelemetry.io/docs/specs/otlp/#failures-1 | ||
// Re. the error response format | ||
// > If the processing of the request fails, the server MUST respond with appropriate HTTP 4xx or HTTP 5xx status code. | ||
// > The response body for all HTTP 4xx and HTTP 5xx responses MUST be a Protobuf-encoded Status message that describes the problem. | ||
// > This specification does not use Status.code field and the server MAY omit Status.code field. | ||
// > The clients are not expected to alter their behavior based on Status.code field but MAY record it for troubleshooting purposes. | ||
// > The Status.message field SHOULD contain a developer-facing error message as defined in Status message schema. | ||
// | ||
// Re. retryable errors | ||
// > The requests that receive a response status code listed in following table SHOULD be retried. | ||
// > All other 4xx or 5xx response status codes MUST NOT be retried | ||
// > 429 Too Many Requests | ||
// > 502 Bad Gateway | ||
// > 503 Service Unavailable | ||
// > 504 Gateway Timeout | ||
// In loki, we expect clients to retry on 500 errors, so we map 500 errors to 503. | ||
func OTLPError(w http.ResponseWriter, error string, code int, logger log.Logger) { | ||
// Map 500 errors to 503. 500 errors are never retried on the client side, but 503 are. | ||
if code == http.StatusInternalServerError { | ||
code = http.StatusServiceUnavailable | ||
} | ||
|
||
// As per the OTLP spec, we send the status code on the http header. | ||
w.WriteHeader(code) | ||
|
||
// Status 0 because we omit the Status.code field. | ||
status := grpcstatus.New(0, error).Proto() | ||
respBytes, err := proto.Marshal(status) | ||
if err != nil { | ||
level.Error(logger).Log("msg", "failed to marshal error response", "error", err) | ||
writeResponseFailedBody, _ := proto.Marshal(grpcstatus.New( | ||
codes.Internal, | ||
fmt.Sprintf("failed to marshal error response. original error: %s", err.Error()), | ||
salvacorts marked this conversation as resolved.
Show resolved
Hide resolved
|
||
).Proto()) | ||
_, _ = w.Write(writeResponseFailedBody) | ||
return | ||
} | ||
|
||
w.Header().Set(contentType, "application/octet-stream") | ||
if _, err = w.Write(respBytes); err != nil { | ||
level.Error(logger).Log("msg", "failed to write error response", "error", err) | ||
writeResponseFailedBody, _ := proto.Marshal(grpcstatus.New( | ||
codes.Internal, | ||
fmt.Sprintf("failed write error. original error: %s", err.Error()), | ||
salvacorts marked this conversation as resolved.
Show resolved
Hide resolved
|
||
).Proto()) | ||
_, _ = w.Write(writeResponseFailedBody) | ||
} | ||
} | ||
|
||
var _ ErrorWriter = OTLPError | ||
|
||
func HTTPError(w http.ResponseWriter, error string, code int, _ log.Logger) { | ||
http.Error(w, error, code) | ||
} | ||
|
||
var _ ErrorWriter = HTTPError |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Logic moved into new
OTLPError
writer