@@ -2,6 +2,7 @@ package zapx
2
2
3
3
import (
4
4
"path"
5
+ "slices"
5
6
"sort"
6
7
7
8
"go.uber.org/zap"
@@ -15,49 +16,53 @@ type SlowlogEncoder struct {
15
16
}
16
17
17
18
func NewSlowlogEncoder () * SlowlogEncoder {
18
- return & SlowlogEncoder {strBuf : make ([]string , 0 )}
19
+ return & SlowlogEncoder {strBuf : make ([]string , 0 , 32 )}
19
20
}
20
21
21
22
func (sle * SlowlogEncoder ) reset () {
22
23
sle .strBuf = sle .strBuf [:0 ]
23
24
}
24
25
25
26
func (sle * SlowlogEncoder ) add (s string ) {
26
- if l := len (sle .strBuf ); l > 1 && sle .strBuf [l - 1 ] == s {
27
+ i := sort .SearchStrings (sle .strBuf , s )
28
+ if i < len (sle .strBuf ) && sle .strBuf [i ] == s {
29
+ // Do not insert duplicates
27
30
return
28
31
}
29
32
30
- i := sort .SearchStrings (sle .strBuf , s )
31
- sle .strBuf = append (sle .strBuf , "" )
32
- copy (sle .strBuf [i + 1 :], sle .strBuf [i :])
33
-
34
- sle .strBuf [i ] = s
33
+ sle .strBuf = slices .Insert (sle .strBuf , i , s )
35
34
}
36
35
37
- func (sle * SlowlogEncoder ) addDir (p string ) {
38
- sle .add (path .Dir (p ))
36
+ func (sle * SlowlogEncoder ) addDir (p string ) bool {
37
+ dir , _ := path .Split (p )
38
+ if dir == "" {
39
+ return false
40
+ }
41
+
42
+ sle .add (path .Clean (dir ))
43
+
44
+ return true
39
45
}
40
46
41
47
func (sle * SlowlogEncoder ) longestCommonPrefOffset () int {
42
- offset := 0
43
- endPrefix := false
44
-
45
48
if len (sle .strBuf ) <= 0 {
46
49
return 0
47
50
}
48
51
52
+ if len (sle .strBuf ) == 1 {
53
+ return len (sle .strBuf [0 ]) + 1
54
+ }
55
+
49
56
first := sle .strBuf [0 ]
50
57
last := sle .strBuf [len (sle .strBuf )- 1 ]
51
58
52
59
for i := 0 ; i < len (first ); i ++ {
53
- if ! endPrefix && last [i ] == first [i ] {
54
- offset = i + 1
55
- } else {
56
- endPrefix = true
60
+ if last [i ] != first [i ] {
61
+ return i
57
62
}
58
63
}
59
64
60
- return offset
65
+ return 0
61
66
}
62
67
63
68
func (sle * SlowlogEncoder ) encodeStacktraceEntry (encoder zapcore.ObjectEncoder , entry phpfpm.SlowlogTraceEntry , pathOffset int ) {
@@ -89,12 +94,15 @@ func (sle *SlowlogEncoder) encodeStacktrace(stacktrace []phpfpm.SlowlogTraceEntr
89
94
func (sle * SlowlogEncoder ) Encode (entry phpfpm.SlowlogEntry ) []zap.Field {
90
95
sle .reset ()
91
96
92
- sle .addDir (entry .ScriptFilename )
97
+ cutPrefix := sle .addDir (entry .ScriptFilename )
93
98
for i := range entry .Stacktrace {
94
- sle .addDir (entry .Stacktrace [i ].Path )
99
+ cutPrefix = cutPrefix && sle .addDir (entry .Stacktrace [i ].Path )
95
100
}
96
101
97
- pathOffset := sle .longestCommonPrefOffset ()
102
+ pathOffset := 0
103
+ if cutPrefix {
104
+ pathOffset = sle .longestCommonPrefOffset ()
105
+ }
98
106
99
107
return []zap.Field {
100
108
zap .String ("filename" , entry .ScriptFilename [pathOffset :]),
0 commit comments