From 6c710faaa11564831d3d1344eedde55c90c45867 Mon Sep 17 00:00:00 2001 From: Dwi Siswanto Date: Thu, 4 Jan 2024 23:51:42 +0700 Subject: [PATCH] refactor: update pkg caller identification in initialize (#138) Replace the usage of `runtime.Caller` with the faster, zero memory allocation, and convenient `loc.Caller` in `teler.New`. This change introduces a performance improvement, especially when using the `nikandfor_loc_unsafe` build tag. Signed-off-by: Dwi Siswanto --- go.mod | 1 + go.sum | 2 ++ teler.go | 6 +++--- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 9066a44..f32ca18 100644 --- a/go.mod +++ b/go.mod @@ -24,6 +24,7 @@ require ( golang.org/x/net v0.19.0 golang.org/x/text v0.14.0 gopkg.in/yaml.v3 v3.0.1 + tlog.app/go/loc v0.6.1 ) require ( diff --git a/go.sum b/go.sum index 187e606..158cc71 100644 --- a/go.sum +++ b/go.sum @@ -1000,3 +1000,5 @@ mvdan.cc/sh/v3 v3.6.0/go.mod h1:U4mhtBLZ32iWhif5/lD+ygy1zrgaQhUu+XFy7C8+TTA= rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8= rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0= rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA= +tlog.app/go/loc v0.6.1 h1:Eae0owcUBxpQm5KRuZHJFQKXlA7D8hGMw53tKdp0zMY= +tlog.app/go/loc v0.6.1/go.mod h1:k5eWl4QTHUxt1iyV324nJ/I6ib9KgjlluOl1A634IyE= diff --git a/teler.go b/teler.go index 7102a1d..f0914c1 100644 --- a/teler.go +++ b/teler.go @@ -26,7 +26,6 @@ import ( "os" "path" "regexp" - "runtime" "strings" "time" @@ -46,6 +45,7 @@ import ( "github.com/valyala/fastjson" "go.uber.org/zap" "go.uber.org/zap/zapcore" + "tlog.app/go/loc" ) // Threat defines what threat category should be excluded @@ -126,8 +126,8 @@ func New(opts ...Options) *Teler { } // Get the package name of the calling package - _, file, _, ok := runtime.Caller(1) - if ok { + if pc := loc.Caller(1); pc != 0 { + _, file, _ := pc.NameFileLine() t.caller = path.Base(path.Dir(file)) }