otellog

package
v1.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 5, 2026 License: MIT Imports: 10 Imported by: 0

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

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 EnrichMap

func EnrichMap(ctx goravelhttp.Context, m map[string]any) map[string]any

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

func SetDBAttrs(ctx context.Context, statement string)

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

func SetDBPaginateAttrs(ctx context.Context, statement string, page, limit int, rowsReturned int64)

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

func TraceDB(ctx context.Context, table, operation string) (context.Context, func(*error))

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

func WithRequestFields(ctx context.Context, fields golib.Map) context.Context

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

func RuntimeCtx(ctx context.Context) *Logger

RuntimeCtx — pakai di usecase/repository yang punya context.Context.

func (*Logger) Error

func (l *Logger) Error(m golib.Map)

func (*Logger) Info

func (l *Logger) Info(m golib.Map)

func (*Logger) Warning

func (l *Logger) Warning(m golib.Map)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL