Skip to content

Commit eb5de94

Browse files
feat: show line:count in assertion; use global variables for httpmetrics
1 parent 5306925 commit eb5de94

File tree

4 files changed

+50
-39
lines changed

4 files changed

+50
-39
lines changed

debug/assert.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import "fmt"
99
// Assert calls are ignore when "noassert" build tag is provided.
1010
func Assert(condition bool, m string, args ...any) {
1111
if !condition {
12-
panic(fmt.Errorf(m, args...))
12+
13+
c := caller(3) // [caller, closure, Debuglog]
14+
panic(fmt.Errorf("%s: %s", c, fmt.Sprintf(m, args...)))
1315
}
1416
}

debug/caller.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package debug
2+
3+
import (
4+
"fmt"
5+
"path/filepath"
6+
"runtime"
7+
)
8+
9+
//go:noinline
10+
func caller(skip int) string {
11+
var pcs [1]uintptr
12+
runtime.Callers(skip, pcs[:])
13+
cf := runtime.CallersFrames([]uintptr{pcs[0]})
14+
f, _ := cf.Next()
15+
16+
var caller string
17+
if f.File != "" {
18+
file, line := f.File, f.Line
19+
dir, file := filepath.Split(file)
20+
21+
return fmt.Sprintf("%s:%d", filepath.Join(filepath.Base(dir), file), line)
22+
}
23+
24+
return caller
25+
}

debug/debuglog.go

+2-19
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ package debug
55
import (
66
"fmt"
77
"os"
8-
"path/filepath"
9-
"runtime"
108
)
119

1210
// Debuglog creates a print function that prints a message to stdout.
@@ -18,23 +16,8 @@ import (
1816
func Debuglog(tag string) func(string, ...any) {
1917
return func(m string, args ...any) {
2018
pid := os.Getpid()
19+
c := caller(3) // [caller, closure, Debuglog]
2120

22-
var pcs [1]uintptr
23-
runtime.Callers(2, pcs[:]) // skip [Debuglog, closure]
24-
cf := runtime.CallersFrames([]uintptr{pcs[0]})
25-
f, _ := cf.Next()
26-
27-
var caller string
28-
if f.File != "" {
29-
file, line := f.File, f.Line
30-
caller = format(file, line)
31-
}
32-
33-
fmt.Println(fmt.Sprintf("%s %d [%s]: %s", tag, pid, caller, fmt.Sprintf(m, args...)))
21+
fmt.Println(fmt.Sprintf("%s %d %s: %s", tag, pid, c, fmt.Sprintf(m, args...)))
3422
}
3523
}
36-
37-
func format(file string, line int) string {
38-
dir, file := filepath.Split(file)
39-
return fmt.Sprintf("%s:%d", filepath.Join(filepath.Base(dir), file), line)
40-
}

metrics/httpmetrics/httpmetrics.go

+20-19
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,31 @@ import (
66
"github.com/felixge/httpsnoop"
77
"go.inout.gg/foundations/http/httpmiddleware"
88
"go.inout.gg/foundations/must"
9+
"go.opentelemetry.io/otel"
910
"go.opentelemetry.io/otel/attribute"
1011
"go.opentelemetry.io/otel/metric"
1112
)
1213

13-
// Middleware returns a middleware that captures metrics for incoming HTTP requests.
14-
func Middleware(p metric.MeterProvider) httpmiddleware.Middleware {
15-
meter := p.Meter("foundations:httpmetrics")
16-
requestDurationHisto := must.Must(
17-
meter.Int64Histogram(
18-
"request_duration_ms",
19-
metric.WithDescription("The incoming request duration in milliseconds."),
20-
metric.WithUnit("ms"),
21-
metric.WithExplicitBucketBoundaries(1, 5, 10, 25, 50, 100, 200, 500, 1_000, 5_000, 10_000, 30_000, 60_000),
22-
),
23-
)
24-
responseBodySizeHisto := must.Must(
25-
meter.Int64Histogram(
26-
"response_body_size_bytes",
27-
metric.WithDescription("The outgoing response body size in bytes."),
28-
metric.WithUnit("bytes"),
29-
metric.WithExplicitBucketBoundaries(1, 10, 100, 1_000, 10_000, 100_000, 1_000_000, 10_000_000),
30-
),
31-
)
14+
var (
15+
provider = otel.GetMeterProvider()
16+
meter = provider.Meter("foundations:httpmetrics")
17+
18+
requestDurationHisto = must.Must(meter.Int64Histogram(
19+
"request_duration_ms",
20+
metric.WithDescription("The incoming request duration in milliseconds."),
21+
metric.WithUnit("ms"),
22+
metric.WithExplicitBucketBoundaries(1, 5, 10, 25, 50, 100, 200, 500, 1_000, 5_000, 10_000, 30_000, 60_000),
23+
))
24+
responseBodySizeHisto = must.Must(meter.Int64Histogram(
25+
"response_body_size_bytes",
26+
metric.WithDescription("The outgoing response body size in bytes."),
27+
metric.WithUnit("bytes"),
28+
metric.WithExplicitBucketBoundaries(1, 10, 100, 1_000, 10_000, 100_000, 1_000_000, 10_000_000),
29+
))
30+
)
3231

32+
// Middleware returns a middleware that captures metrics for incoming HTTP requests.
33+
func Middleware() httpmiddleware.Middleware {
3334
return httpmiddleware.MiddlewareFunc(func(next http.Handler) http.Handler {
3435
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
3536
ctx := r.Context()

0 commit comments

Comments
 (0)