Skip to content
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

fix: bug where we could error after writing a status code #224

Merged
merged 1 commit into from
Dec 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 7 additions & 12 deletions otlp/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,21 +216,14 @@ func WriteOtlpHttpLogSuccessResponse(w http.ResponseWriter, r *http.Request) err
}

// WriteOtlpHttpResponse writes a compliant OTLP HTTP response to the given http.ResponseWriter
// based on the provided `contentType`. If an error occurs while marshalling to either json or proto it is returned.
// If an error occurs while writing to the http.ResponseWriter it is returned
// based on the provided `contentType`. If an error occurs while marshalling to either json or proto it is returned
// before the http.ResponseWriter is updated. If an error occurs while writing to the http.ResponseWriter it is ignored.
func WriteOtlpHttpResponse(w http.ResponseWriter, r *http.Request, statusCode int, m proto.Message) error {
if r == nil {
return fmt.Errorf("nil Request")
}

contentType := r.Header.Get("Content-Type")
if contentType == "" {
return ErrInvalidContentType
}

w.Header().Set("Content-Type", contentType)
w.WriteHeader(statusCode)

var body []byte
var err error
switch contentType {
Expand All @@ -241,13 +234,15 @@ func WriteOtlpHttpResponse(w http.ResponseWriter, r *http.Request, statusCode in
default:
return ErrInvalidContentType
}

if err != nil {
return err
}

_, err = w.Write(body)
return err
// At this point we're committed
w.Header().Set("Content-Type", contentType)
w.WriteHeader(statusCode)
_, _ = w.Write(body)
return nil
}

func getValueFromMetadata(md metadata.MD, key string) string {
Expand Down