|
| 1 | +package nrslog |
| 2 | + |
| 3 | +import ( |
| 4 | + "log/slog" |
| 5 | + "maps" |
| 6 | + "strings" |
| 7 | +) |
| 8 | + |
| 9 | +type attributeCache struct { |
| 10 | + preCompiledAttributes map[string]interface{} |
| 11 | + prefix string |
| 12 | +} |
| 13 | + |
| 14 | +func newAttributeCache() *attributeCache { |
| 15 | + return &attributeCache{ |
| 16 | + preCompiledAttributes: make(map[string]interface{}), |
| 17 | + prefix: "", |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +func (c *attributeCache) clone() *attributeCache { |
| 22 | + return &attributeCache{ |
| 23 | + preCompiledAttributes: maps.Clone(c.preCompiledAttributes), |
| 24 | + prefix: c.prefix, |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +func (c *attributeCache) copyPreCompiledAttributes() map[string]interface{} { |
| 29 | + return maps.Clone(c.preCompiledAttributes) |
| 30 | +} |
| 31 | + |
| 32 | +func (c *attributeCache) getPrefix() string { |
| 33 | + return c.prefix |
| 34 | +} |
| 35 | + |
| 36 | +// precompileGroup sets the group prefix for the cache created by a handler |
| 37 | +// precompileGroup call. This is used to avoid re-computing the group prefix |
| 38 | +// and should only ever be called on newly created caches and handlers. |
| 39 | +func (c *attributeCache) precompileGroup(group string) { |
| 40 | + if c.prefix != "" { |
| 41 | + c.prefix += "." |
| 42 | + } |
| 43 | + c.prefix += group |
| 44 | +} |
| 45 | + |
| 46 | +// precompileAttributes appends attributes to the cache created by a handler |
| 47 | +// WithAttrs call. This is used to avoid re-computing the with Attrs attributes |
| 48 | +// and should only ever be called on newly created caches and handlers. |
| 49 | +func (c *attributeCache) precompileAttributes(attrs []slog.Attr) { |
| 50 | + if len(attrs) == 0 { |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + for _, a := range attrs { |
| 55 | + c.appendAttr(c.preCompiledAttributes, a, c.prefix) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func (c *attributeCache) appendAttr(nrAttrs map[string]interface{}, a slog.Attr, groupPrefix string) { |
| 60 | + // Resolve the Attr's value before doing anything else. |
| 61 | + a.Value = a.Value.Resolve() |
| 62 | + // Ignore empty Attrs. |
| 63 | + if a.Equal(slog.Attr{}) { |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + // majority of runtime spent allocating and copying strings |
| 68 | + group := strings.Builder{} |
| 69 | + group.Grow(len(groupPrefix) + len(a.Key) + 1) |
| 70 | + group.WriteString(groupPrefix) |
| 71 | + |
| 72 | + if a.Key != "" { |
| 73 | + if group.Len() > 0 { |
| 74 | + group.WriteByte('.') |
| 75 | + } |
| 76 | + group.WriteString(a.Key) |
| 77 | + } |
| 78 | + |
| 79 | + key := group.String() |
| 80 | + |
| 81 | + // If the Attr is a group, append its attributes |
| 82 | + if a.Value.Kind() == slog.KindGroup { |
| 83 | + attrs := a.Value.Group() |
| 84 | + // Ignore empty groups. |
| 85 | + if len(attrs) == 0 { |
| 86 | + return |
| 87 | + } |
| 88 | + |
| 89 | + for _, ga := range attrs { |
| 90 | + c.appendAttr(nrAttrs, ga, key) |
| 91 | + } |
| 92 | + return |
| 93 | + } |
| 94 | + |
| 95 | + // attr is an attribute |
| 96 | + nrAttrs[key] = a.Value.Any() |
| 97 | +} |
0 commit comments