Add fail-fast on init for missing provider credentials
This commit is contained in:
@@ -48,15 +48,30 @@ type metricsResponseWriter struct {
|
||||
http.ResponseWriter
|
||||
statusCode int
|
||||
bytesWritten int
|
||||
wroteHeader bool
|
||||
}
|
||||
|
||||
func (w *metricsResponseWriter) WriteHeader(statusCode int) {
|
||||
if w.wroteHeader {
|
||||
return
|
||||
}
|
||||
w.wroteHeader = true
|
||||
w.statusCode = statusCode
|
||||
w.ResponseWriter.WriteHeader(statusCode)
|
||||
}
|
||||
|
||||
func (w *metricsResponseWriter) Write(b []byte) (int, error) {
|
||||
if !w.wroteHeader {
|
||||
w.wroteHeader = true
|
||||
w.statusCode = http.StatusOK
|
||||
}
|
||||
n, err := w.ResponseWriter.Write(b)
|
||||
w.bytesWritten += n
|
||||
return n, err
|
||||
}
|
||||
|
||||
func (w *metricsResponseWriter) Flush() {
|
||||
if flusher, ok := w.ResponseWriter.(http.Flusher); ok {
|
||||
flusher.Flush()
|
||||
}
|
||||
}
|
||||
|
||||
65
internal/observability/middleware_response_writer_test.go
Normal file
65
internal/observability/middleware_response_writer_test.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package observability
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
var _ http.Flusher = (*metricsResponseWriter)(nil)
|
||||
var _ http.Flusher = (*statusResponseWriter)(nil)
|
||||
|
||||
type testFlusherRecorder struct {
|
||||
*httptest.ResponseRecorder
|
||||
flushCount int
|
||||
}
|
||||
|
||||
func newTestFlusherRecorder() *testFlusherRecorder {
|
||||
return &testFlusherRecorder{ResponseRecorder: httptest.NewRecorder()}
|
||||
}
|
||||
|
||||
func (r *testFlusherRecorder) Flush() {
|
||||
r.flushCount++
|
||||
}
|
||||
|
||||
func TestMetricsResponseWriterWriteHeaderOnlyOnce(t *testing.T) {
|
||||
rec := httptest.NewRecorder()
|
||||
rw := &metricsResponseWriter{ResponseWriter: rec, statusCode: http.StatusOK}
|
||||
|
||||
rw.WriteHeader(http.StatusAccepted)
|
||||
rw.WriteHeader(http.StatusInternalServerError)
|
||||
|
||||
assert.Equal(t, http.StatusAccepted, rec.Code)
|
||||
assert.Equal(t, http.StatusAccepted, rw.statusCode)
|
||||
}
|
||||
|
||||
func TestMetricsResponseWriterFlushDelegates(t *testing.T) {
|
||||
rec := newTestFlusherRecorder()
|
||||
rw := &metricsResponseWriter{ResponseWriter: rec, statusCode: http.StatusOK}
|
||||
|
||||
rw.Flush()
|
||||
|
||||
assert.Equal(t, 1, rec.flushCount)
|
||||
}
|
||||
|
||||
func TestStatusResponseWriterWriteHeaderOnlyOnce(t *testing.T) {
|
||||
rec := httptest.NewRecorder()
|
||||
rw := &statusResponseWriter{ResponseWriter: rec, statusCode: http.StatusOK}
|
||||
|
||||
rw.WriteHeader(http.StatusNoContent)
|
||||
rw.WriteHeader(http.StatusInternalServerError)
|
||||
|
||||
assert.Equal(t, http.StatusNoContent, rec.Code)
|
||||
assert.Equal(t, http.StatusNoContent, rw.statusCode)
|
||||
}
|
||||
|
||||
func TestStatusResponseWriterFlushDelegates(t *testing.T) {
|
||||
rec := newTestFlusherRecorder()
|
||||
rw := &statusResponseWriter{ResponseWriter: rec, statusCode: http.StatusOK}
|
||||
|
||||
rw.Flush()
|
||||
|
||||
assert.Equal(t, 1, rec.flushCount)
|
||||
}
|
||||
@@ -72,14 +72,29 @@ func TracingMiddleware(next http.Handler, tp *sdktrace.TracerProvider) http.Hand
|
||||
// statusResponseWriter wraps http.ResponseWriter to capture the status code.
|
||||
type statusResponseWriter struct {
|
||||
http.ResponseWriter
|
||||
statusCode int
|
||||
statusCode int
|
||||
wroteHeader bool
|
||||
}
|
||||
|
||||
func (w *statusResponseWriter) WriteHeader(statusCode int) {
|
||||
if w.wroteHeader {
|
||||
return
|
||||
}
|
||||
w.wroteHeader = true
|
||||
w.statusCode = statusCode
|
||||
w.ResponseWriter.WriteHeader(statusCode)
|
||||
}
|
||||
|
||||
func (w *statusResponseWriter) Write(b []byte) (int, error) {
|
||||
if !w.wroteHeader {
|
||||
w.wroteHeader = true
|
||||
w.statusCode = http.StatusOK
|
||||
}
|
||||
return w.ResponseWriter.Write(b)
|
||||
}
|
||||
|
||||
func (w *statusResponseWriter) Flush() {
|
||||
if flusher, ok := w.ResponseWriter.(http.Flusher); ok {
|
||||
flusher.Flush()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user