Skip to content

Commit

Permalink
refactor: update pkg caller identification in initialize (#138)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
dwisiswant0 authored Jan 4, 2024
1 parent c6ab7f3 commit 6c710fa
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
6 changes: 3 additions & 3 deletions teler.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"os"
"path"
"regexp"
"runtime"
"strings"
"time"

Expand All @@ -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
Expand Down Expand Up @@ -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))
}

Expand Down

0 comments on commit 6c710fa

Please sign in to comment.