-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: httpmetrics; cookie -> httpcookie; add health endpoint to httph…
…andler
- Loading branch information
1 parent
4904511
commit 5306925
Showing
16 changed files
with
199 additions
and
38 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
if use flake; then | ||
if nix flake show &>/dev/null; then | ||
use flake | ||
fi |
This file contains 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 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package cookie | ||
package httpcookie | ||
|
||
import ( | ||
"net/http" | ||
|
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package httphandler | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestTodo(t *testing.T) { | ||
// Create a new request | ||
req := httptest.NewRequest(http.MethodGet, "/", nil) | ||
rr := httptest.NewRecorder() | ||
|
||
// Call the handler | ||
TODO.ServeHTTP(rr, req) | ||
|
||
// Check status code | ||
assert.Equal(t, http.StatusOK, rr.Code, "handler returned wrong status code") | ||
|
||
// Check response body | ||
body, err := io.ReadAll(rr.Body) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "todo", string(body), "handler returned unexpected body") | ||
} | ||
|
||
func TestHealthCheck(t *testing.T) { | ||
// Create a new request | ||
req := httptest.NewRequest(http.MethodGet, "/health", nil) | ||
rr := httptest.NewRecorder() | ||
|
||
// Call the handler | ||
HealthCheck(rr, req) | ||
|
||
// Check status code | ||
assert.Equal(t, http.StatusOK, rr.Code, "handler returned wrong status code") | ||
|
||
// Check response body | ||
body, err := io.ReadAll(rr.Body) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "ok", string(body), "handler returned unexpected body") | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package httpmetrics | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/felixge/httpsnoop" | ||
"go.inout.gg/foundations/http/httpmiddleware" | ||
"go.inout.gg/foundations/must" | ||
"go.opentelemetry.io/otel/attribute" | ||
"go.opentelemetry.io/otel/metric" | ||
) | ||
|
||
// Middleware returns a middleware that captures metrics for incoming HTTP requests. | ||
func Middleware(p metric.MeterProvider) httpmiddleware.Middleware { | ||
meter := p.Meter("foundations:httpmetrics") | ||
requestDurationHisto := must.Must( | ||
meter.Int64Histogram( | ||
"request_duration_ms", | ||
metric.WithDescription("The incoming request duration in milliseconds."), | ||
metric.WithUnit("ms"), | ||
metric.WithExplicitBucketBoundaries(1, 5, 10, 25, 50, 100, 200, 500, 1_000, 5_000, 10_000, 30_000, 60_000), | ||
), | ||
) | ||
responseBodySizeHisto := must.Must( | ||
meter.Int64Histogram( | ||
"response_body_size_bytes", | ||
metric.WithDescription("The outgoing response body size in bytes."), | ||
metric.WithUnit("bytes"), | ||
metric.WithExplicitBucketBoundaries(1, 10, 100, 1_000, 10_000, 100_000, 1_000_000, 10_000_000), | ||
), | ||
) | ||
|
||
return httpmiddleware.MiddlewareFunc(func(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
|
||
metrics := httpsnoop.CaptureMetrics(next, w, r) | ||
defaultAttributes := attribute.NewSet( | ||
attribute.Int("code", metrics.Code), | ||
attribute.String("method", r.Method), | ||
attribute.String("path", r.URL.Path), | ||
) | ||
|
||
requestDurationHisto.Record(ctx, metrics.Duration.Milliseconds(), metric.WithAttributeSet(defaultAttributes)) | ||
responseBodySizeHisto.Record(ctx, metrics.Written, metric.WithAttributeSet(defaultAttributes)) | ||
}) | ||
}) | ||
} |
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package sqldb | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/jackc/pgx/v5/pgxpool" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMiddleware(t *testing.T) { | ||
mockPool := &pgxpool.Pool{} | ||
|
||
t.Run("should bind pool to context", func(t *testing.T) { | ||
testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
// Assert | ||
pool, err := FromRequest(r) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, mockPool, pool) | ||
|
||
pool, err = FromContext(r.Context()) | ||
assert.NoError(t, err) | ||
assert.Equal(t, mockPool, pool) | ||
}) | ||
|
||
// Arrange | ||
middleware := Middleware(mockPool) | ||
|
||
// Act | ||
middleware(testHandler).ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/", nil)) | ||
}) | ||
|
||
t.Run("should return error when pool not in context", func(t *testing.T) { | ||
emptyCtx := context.Background() | ||
_, err := FromContext(emptyCtx) | ||
assert.Error(t, err) | ||
assert.Equal(t, ErrDBPoolNotFound, err) | ||
}) | ||
} |
This file contains 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 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
package sqldb |
This file contains 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