Documentation
¶
Overview ¶
Package otelmetric adalah wrapper tipis di atas OTel Metrics API yang menyediakan helper untuk mencatat durasi fungsi, operasi DB, dan HTTP request. Pola penggunaannya sengaja dibuat mirip otellog agar konsisten di semua service.
Index ¶
- func Flush(ctx context.Context) error
- func NormalizeRoute(path string) string
- func Record(ctx context.Context) func()
- func RecordDB(ctx context.Context, table, operation string) func()
- func RecordFunc(ctx context.Context, funcName string) func()
- func RecordHTTP(ctx goravelhttp.Context) func()
- func StartHTTP(goCtx context.Context, method, path string) func(status int)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Flush ¶ added in v1.1.8
Flush forces the global MeterProvider to export any metrics still queued for its next periodic collection. Required at the end of short-lived processes (artisan/CLI commands, cron jobs) that call os.Exit right after their handler returns — the periodic reader otherwise never gets a scheduled tick to collect+export before the process dies. Pair with otellog.Flush for full observability on a one-shot command.
func NormalizeRoute ¶ added in v1.1.10
NormalizeRoute mengganti segmen path yang berupa ID (angka, UUID, hex panjang) dengan placeholder ":id", supaya label http.route tidak meledak (high cardinality). Contoh: /v1/users/12345/docs/550e8400-e29b-41d4-a716-446655440000 → /v1/users/:id/docs/:id
func Record ¶ added in v1.1.11
Record sama dengan RecordFunc, tetapi label func.name diambil otomatis dari fungsi pemanggil ("<Struct>.<Func>"), sama dengan nama span otellog.Trace. Harus dipanggil langsung di fungsi yang diukur:
defer otelmetric.Record(ctx)()
func RecordDB ¶
RecordDB mencatat durasi operasi database sebagai histogram. Pasangan dari otellog.TraceDB — gunakan keduanya bersama.
func (r *Repo) GetList(ctx context.Context) (result []Model, err error) {
ctx, traceDone := otellog.TraceDB(ctx, "mst_rekanan", "SELECT")
defer otelmetric.RecordDB(ctx, "mst_rekanan", "SELECT")()
defer traceDone(&err)
...
}
func RecordFunc ¶
RecordFunc mencatat durasi satu pemanggilan fungsi sebagai histogram. Pasangan dari otellog.TraceFunc — gunakan keduanya bersama untuk observability lengkap.
func (u *Usecase) MyFunc(ctx context.Context, req Req) (resp Resp, err error) {
ctx, traceDone := otellog.TraceFunc(ctx, "MyFunc")
defer otelmetric.RecordFunc(ctx, "MyFunc")()
defer traceDone(&err)
...
}
func RecordHTTP ¶
func RecordHTTP(ctx goravelhttp.Context) func()
RecordHTTP mencatat metrics HTTP request: total, durasi (histogram), dan concurrent aktif. Gunakan di middleware — panggil sebelum Next(), gunakan returned func setelah Next():
func OtelMetrics(ctx goravelhttp.Context) {
done := otelmetric.RecordHTTP(ctx)
ctx.Request().Next()
done()
}
Types ¶
This section is empty.