Compare commits
2
Commits
bbd92df20f
...
39d531c501
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
39d531c501 | ||
|
|
9b3d7326ce |
+12
-5
@@ -9,13 +9,20 @@ RUN make fmt-check
|
|||||||
# Called directly: make lint is itself a docker build of this stage.
|
# Called directly: make lint is itself a docker build of this stage.
|
||||||
RUN golangci-lint run --config .golangci.yml ./...
|
RUN golangci-lint run --config .golangci.yml ./...
|
||||||
|
|
||||||
# Test stage: run full test suite
|
# Test stage: run full test suite under the race detector
|
||||||
# golang 1.22.12 (2025-02-04)
|
# golang 1.22.12 (Debian-based; -race needs its C toolchain), 2025-02-04
|
||||||
FROM golang@sha256:1cf6c45ba39db9fd6db16922041d074a63c935556a05c5ccb62d181034df7f02 AS test
|
FROM golang@sha256:1cf6c45ba39db9fd6db16922041d074a63c935556a05c5ccb62d181034df7f02 AS test
|
||||||
# Depend on lint stage so both stages always run
|
|
||||||
COPY --from=lint /src/go.sum /dev/null
|
|
||||||
WORKDIR /src
|
WORKDIR /src
|
||||||
COPY go.mod go.sum ./
|
COPY go.mod go.sum ./
|
||||||
RUN go mod download
|
RUN go mod download
|
||||||
COPY . .
|
COPY . .
|
||||||
RUN make test
|
# Called directly: make test is itself a docker build of this stage.
|
||||||
|
RUN go test -timeout 90s -race -cover ./... || \
|
||||||
|
{ echo "--- Rerunning with -v for details ---"; \
|
||||||
|
go test -timeout 90s -race -v ./...; exit 1; }
|
||||||
|
|
||||||
|
# Final stage: the copies make a plain docker build run both stages above.
|
||||||
|
# golang 1.22.12 (2025-02-04)
|
||||||
|
FROM golang@sha256:1cf6c45ba39db9fd6db16922041d074a63c935556a05c5ccb62d181034df7f02
|
||||||
|
COPY --from=lint /src/go.sum /dev/null
|
||||||
|
COPY --from=test /src/go.sum /dev/null
|
||||||
|
|||||||
@@ -116,7 +116,9 @@ the record:
|
|||||||
stdout, wrapped, so `errors.Is` still matches the original
|
stdout, wrapped, so `errors.Is` still matches the original
|
||||||
- `WebhookHandler` returns an error when the request fails, and also when
|
- `WebhookHandler` returns an error when the request fails, and also when
|
||||||
the server answers with a status outside 2xx, a redirect included,
|
the server answers with a status outside 2xx, a redirect included,
|
||||||
since it does not follow redirects
|
since it does not follow redirects. A request that has not finished
|
||||||
|
after 5 seconds fails with a timeout error, so a webhook server that
|
||||||
|
never answers holds up a log call for 5 seconds at most
|
||||||
- `MultiplexHandler`, which simplelog installs as the default, passes the
|
- `MultiplexHandler`, which simplelog installs as the default, passes the
|
||||||
record to every handler it holds even after one of them fails, then
|
record to every handler it holds even after one of them fails, then
|
||||||
returns all their errors joined with `errors.Join` (nil if none failed)
|
returns all their errors joined with `errors.Join` (nil if none failed)
|
||||||
@@ -145,7 +147,9 @@ alpine. We provide:
|
|||||||
`script/bootstrap`, then `script/install-precommit`
|
`script/bootstrap`, then `script/install-precommit`
|
||||||
- `script/projectname` — output the project name (our own extension); used by
|
- `script/projectname` — output the project name (our own extension); used by
|
||||||
`script/docker` for the image tag
|
`script/docker` for the image tag
|
||||||
- `script/test` — run the test suite (`go test -v ./...`)
|
- `script/test` — run the test suite under the race detector in Docker by
|
||||||
|
building only the `test` stage of the `Dockerfile`, without the build cache,
|
||||||
|
so every run tests; the image is tagged `simplelog-test`
|
||||||
- `script/lint` — run golangci-lint in Docker by building only the `lint`
|
- `script/lint` — run golangci-lint in Docker by building only the `lint`
|
||||||
stage of the `Dockerfile` (which also runs the format check), without the
|
stage of the `Dockerfile` (which also runs the format check), without the
|
||||||
build cache, so every run lints; the image is tagged `simplelog-lint`
|
build cache, so every run lints; the image is tagged `simplelog-lint`
|
||||||
|
|||||||
@@ -24,6 +24,13 @@ files it depends on: .golangci.yml, REPO_POLICIES.md, .editorconfig,
|
|||||||
|
|
||||||
# Completed Steps
|
# Completed Steps
|
||||||
|
|
||||||
|
* 2026-10-06: a webhook request now times out after 5 seconds, so a
|
||||||
|
server that never answers no longer stops every log call; the webhook
|
||||||
|
handler also reads each answer to the end so its connection is reused
|
||||||
|
* 2026-10-06: the tests run under the race detector, in Docker:
|
||||||
|
`script/test` builds the `test` stage of the `Dockerfile`, which runs
|
||||||
|
`go test -race`, and a new final stage makes a plain `docker build`
|
||||||
|
run both the `lint` and `test` stages
|
||||||
* 2026-10-06: the linter runs only in Docker: `script/lint` builds the
|
* 2026-10-06: the linter runs only in Docker: `script/lint` builds the
|
||||||
`lint` stage of the `Dockerfile`, every `docker build` in `script/`
|
`lint` stage of the `Dockerfile`, every `docker build` in `script/`
|
||||||
runs without the build cache, and `script/bootstrap` no longer
|
runs without the build cache, and `script/bootstrap` no longer
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -160,3 +161,103 @@ func TestWebhookHandlerReturnsErrorOnRedirect(t *testing.T) {
|
|||||||
t.Fatalf("Handle returned %v, want an error for the redirect", err)
|
t.Fatalf("Handle returned %v, want an error for the redirect", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A server that accepts the request and never answers must not hold up
|
||||||
|
// the log call: Handle gives up after webhookTimeout and says why.
|
||||||
|
func TestWebhookHandlerTimesOutOnServerThatNeverAnswers(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
testEnded := make(chan struct{})
|
||||||
|
|
||||||
|
server := httptest.NewServer(http.HandlerFunc(
|
||||||
|
func(_ http.ResponseWriter, _ *http.Request) {
|
||||||
|
<-testEnded
|
||||||
|
},
|
||||||
|
))
|
||||||
|
|
||||||
|
// server.Close waits for running requests, so the server's handler
|
||||||
|
// is released first.
|
||||||
|
defer func() {
|
||||||
|
close(testEnded)
|
||||||
|
server.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
handler, err := NewWebhookHandler(server.URL)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewWebhookHandler: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle runs in a goroutine so that a lost timeout fails the test
|
||||||
|
// instead of hanging the test run.
|
||||||
|
handleErr := make(chan error, 1)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
handleErr <- handler.Handle(context.Background(), errorTestRecord())
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case err = <-handleErr:
|
||||||
|
case <-time.After(webhookTimeout + time.Second):
|
||||||
|
t.Fatal("Handle did not return within webhookTimeout")
|
||||||
|
}
|
||||||
|
|
||||||
|
var netErr net.Error
|
||||||
|
if !errors.As(err, &netErr) || !netErr.Timeout() {
|
||||||
|
t.Fatalf("Handle returned %v, want a timeout error", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// A server that sends a 2xx status and then never finishes the answer
|
||||||
|
// must not hold up the log call either: reading the answer counts toward
|
||||||
|
// webhookTimeout, and Handle returns the read's error.
|
||||||
|
func TestWebhookHandlerTimesOutOnServerThatStallsTheAnswer(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
testEnded := make(chan struct{})
|
||||||
|
|
||||||
|
server := httptest.NewServer(http.HandlerFunc(
|
||||||
|
func(w http.ResponseWriter, _ *http.Request) {
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
|
||||||
|
// Flush sends the status now; the answer stays unfinished
|
||||||
|
// until the test ends.
|
||||||
|
err := http.NewResponseController(w).Flush()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("flush the status: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
<-testEnded
|
||||||
|
},
|
||||||
|
))
|
||||||
|
|
||||||
|
// server.Close waits for running requests, so the server's handler
|
||||||
|
// is released first.
|
||||||
|
defer func() {
|
||||||
|
close(testEnded)
|
||||||
|
server.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
handler, err := NewWebhookHandler(server.URL)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewWebhookHandler: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle runs in a goroutine so that a lost timeout fails the test
|
||||||
|
// instead of hanging the test run.
|
||||||
|
handleErr := make(chan error, 1)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
handleErr <- handler.Handle(context.Background(), errorTestRecord())
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case err = <-handleErr:
|
||||||
|
case <-time.After(webhookTimeout + time.Second):
|
||||||
|
t.Fatal("Handle did not return within webhookTimeout")
|
||||||
|
}
|
||||||
|
|
||||||
|
var netErr net.Error
|
||||||
|
if !errors.As(err, &netErr) || !netErr.Timeout() {
|
||||||
|
t.Fatalf("Handle returned %v, want a timeout error", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+10
-3
@@ -1,12 +1,19 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
# script/test: run the test suite.
|
# script/test: run the test suite. Testing is a phase of the Dockerfile
|
||||||
|
# and this builds that phase alone, on the same terms as script/lint:
|
||||||
|
# --target because a phase that is not the last stage is built only when
|
||||||
|
# named, --no-cache because a cached test layer is a test that did not
|
||||||
|
# run, and a tag so each build replaces the previous image.
|
||||||
set -eu
|
set -eu
|
||||||
|
|
||||||
ROOT="$(cd "$(dirname "$0")/.." && pwd -P)"
|
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd -P)"
|
||||||
|
ROOT="$(cd "$SCRIPT_DIR/.." && pwd -P)"
|
||||||
|
|
||||||
main() {
|
main() {
|
||||||
cd "$ROOT"
|
cd "$ROOT"
|
||||||
go test -v ./...
|
docker build --no-cache \
|
||||||
|
--target test \
|
||||||
|
-t "$("$SCRIPT_DIR/projectname")-test" .
|
||||||
}
|
}
|
||||||
|
|
||||||
main "$@"
|
main "$@"
|
||||||
|
|||||||
+19
-1
@@ -6,15 +6,22 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// errWebhookStatus is returned when the webhook answers with a status
|
// errWebhookStatus is returned when the webhook answers with a status
|
||||||
// outside 2xx.
|
// outside 2xx.
|
||||||
var errWebhookStatus = errors.New("webhook did not accept the record")
|
var errWebhookStatus = errors.New("webhook did not accept the record")
|
||||||
|
|
||||||
|
// webhookTimeout bounds each webhook request, reading the answer
|
||||||
|
// included. Handle runs inside the log call, so a server that never
|
||||||
|
// answers would otherwise hold that call up for good.
|
||||||
|
const webhookTimeout = 5 * time.Second
|
||||||
|
|
||||||
// WebhookHandler POSTs each log record as JSON to a configured webhook
|
// WebhookHandler POSTs each log record as JSON to a configured webhook
|
||||||
// URL.
|
// URL.
|
||||||
type WebhookHandler struct {
|
type WebhookHandler struct {
|
||||||
@@ -34,6 +41,7 @@ func NewWebhookHandler(webhookURL string) (*WebhookHandler, error) {
|
|||||||
return &WebhookHandler{
|
return &WebhookHandler{
|
||||||
webhookURL: webhookURL,
|
webhookURL: webhookURL,
|
||||||
client: &http.Client{
|
client: &http.Client{
|
||||||
|
Timeout: webhookTimeout,
|
||||||
// Following a redirect can resend the request as a GET
|
// Following a redirect can resend the request as a GET
|
||||||
// without the record, so Handle gets the redirect answer
|
// without the record, so Handle gets the redirect answer
|
||||||
// itself and returns it as an error.
|
// itself and returns it as an error.
|
||||||
@@ -80,7 +88,8 @@ func (w *WebhookHandler) WithGroup(name string) slog.Handler {
|
|||||||
|
|
||||||
// Handle marshals the record, with its attributes, to one JSON object and
|
// Handle marshals the record, with its attributes, to one JSON object and
|
||||||
// POSTs it to the webhook URL. It returns an error when the request fails
|
// POSTs it to the webhook URL. It returns an error when the request fails
|
||||||
// or the server answers with a status outside 2xx.
|
// or runs past webhookTimeout, or when the server answers with a status
|
||||||
|
// outside 2xx.
|
||||||
func (w *WebhookHandler) Handle(ctx context.Context, record slog.Record) error {
|
func (w *WebhookHandler) Handle(ctx context.Context, record slog.Record) error {
|
||||||
jsonData, err := json.Marshal(recordToMap(record, w.attrs))
|
jsonData, err := json.Marshal(recordToMap(record, w.attrs))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -106,6 +115,15 @@ func (w *WebhookHandler) Handle(ctx context.Context, record slog.Record) error {
|
|||||||
|
|
||||||
defer func() { _ = response.Body.Close() }()
|
defer func() { _ = response.Body.Close() }()
|
||||||
|
|
||||||
|
// The answer is read to the end so the client can reuse the
|
||||||
|
// connection for the next record. The read counts toward
|
||||||
|
// webhookTimeout, so a server that sends its status and then stalls
|
||||||
|
// fails here.
|
||||||
|
_, err = io.Copy(io.Discard, response.Body)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error reading webhook answer: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
if response.StatusCode < http.StatusOK ||
|
if response.StatusCode < http.StatusOK ||
|
||||||
response.StatusCode >= http.StatusMultipleChoices {
|
response.StatusCode >= http.StatusMultipleChoices {
|
||||||
return fmt.Errorf("%w: %s", errWebhookStatus, response.Status)
|
return fmt.Errorf("%w: %s", errWebhookStatus, response.Status)
|
||||||
|
|||||||
Reference in New Issue
Block a user