Documentation
¶
Overview ¶
Package otellog adalah wrapper tipis di atas go-lib RunLogger yang otomatis menambahkan trace.id dan span.id ke setiap log entry, sekaligus mengirim log via OTEL Log SDK → Collector → Elasticsearch.
Index ¶
- func Activity(ctx goravelhttp.Context, m golib.Map)
- func EnrichMap(ctx goravelhttp.Context, m map[string]any) map[string]any
- func GoContext(ctx goravelhttp.Context) context.Context
- func SetDBAttrs(ctx context.Context, statement string)
- func SetDBPaginateAttrs(ctx context.Context, statement string, page, limit int, rowsReturned int64)
- func TraceDB(ctx context.Context, table, operation string) (context.Context, func(*error))
- func TraceFunc(ctx context.Context, name string, fields ...golib.Map) (context.Context, func(*error))
- func WithRequestFields(ctx context.Context, fields golib.Map) context.Context
- type Logger
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Activity ¶
func Activity(ctx goravelhttp.Context, m golib.Map)
Activity emits an HTTP activity log via OTEL SDK only (does not write to runtime.log). Use in middleware after calling Next() to capture full request/response with real trace.id.
func GoContext ¶
func GoContext(ctx goravelhttp.Context) context.Context
GoContext mengambil enriched Go context (dengan OTEL span) dari goravelhttp.Context. Gunakan di controller saat memanggil usecase agar trace menyambung. Fallback ke ctx.Context() jika OTEL belum di-set.
func SetDBAttrs ¶
SetDBAttrs menyimpan db.statement ke span aktif di ctx. Gunakan untuk INSERT, UPDATE, DELETE, atau single SELECT.
ctx, done := otellog.TraceDB(ctx, "mst_rekanan", "UPDATE")
defer done(&err)
otellog.SetDBAttrs(ctx, query.ToRawSql().Update("status", val))
func SetDBPaginateAttrs ¶
SetDBPaginateAttrs menyimpan db.statement + pagination + rows_returned ke span aktif di ctx. Panggil setelah Paginate selesai dengan stmt yang diambil sebelum eksekusi.
ctx, done := otellog.TraceDB(ctx, "mst_rekanan", "SELECT") defer done(&err) stmt := query.ToRawSql().Find(&list) _ = query.Paginate(page, limit, &list, &total) otellog.SetDBPaginateAttrs(ctx, stmt, page, limit, total)
func TraceDB ¶
TraceDB membuat DB span untuk operasi query di repository. Gunakan dengan named return error dan defer:
func (r *Repo) GetList(ctx context.Context, ...) (result []Model, err error) {
ctx, done := otellog.TraceDB(ctx, "nama_tabel", "SELECT")
defer done(&err)
...
}
func TraceFunc ¶
func TraceFunc(ctx context.Context, name string, fields ...golib.Map) (context.Context, func(*error))
TraceFunc membuat span + log start/ok/error otomatis untuk satu fungsi. Gunakan dengan named return error dan defer:
func (u *Usecase) MyFunc(ctx context.Context, req Req) (resp Resp, err error) {
ctx, done := otellog.TraceFunc(ctx, "MyFunc", log.Map{"key": val})
defer done(&err)
...
}
fields opsional — dipakai untuk log start, tidak wajib diisi.
func WithRequestFields ¶
WithRequestFields menyimpan field request ke dalam Go context. Field ini otomatis di-merge ke setiap log call yang menggunakan context tersebut, tanpa perlu ditulis ulang di setiap Info/Warning/Error.
Types ¶
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
func Runtime ¶
func Runtime(ctx goravelhttp.Context) *Logger
Runtime — pakai di controller yang punya goravelhttp.Context. Otomatis mengambil enriched context (dengan request fields) jika sudah di-set oleh OtelPropagation atau OtelRequestFields middleware.
func RuntimeCtx ¶
RuntimeCtx — pakai di usecase/repository yang punya context.Context.