2024-05-17 13:31:35 +00:00
|
|
|
package layer_attr
|
|
|
|
|
|
|
|
import (
|
2024-05-20 06:20:13 +00:00
|
|
|
"git.ma-al.com/maal-libraries/observer/pkg/attr"
|
|
|
|
"git.ma-al.com/maal-libraries/observer/pkg/level"
|
2024-05-17 13:31:35 +00:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Handler struct {
|
|
|
|
// Extra attributes to be attached. Can be also added with [Handler.CustomAttrs] method.
|
|
|
|
Attributes []attribute.KeyValue
|
|
|
|
Level level.SeverityLevel
|
2024-05-17 16:21:09 +00:00
|
|
|
Name string
|
2024-05-17 13:31:35 +00:00
|
|
|
extraSkipInStack int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h Handler) IntoTraceAttributes() []attribute.KeyValue {
|
|
|
|
attrs := make([]attribute.KeyValue, 5+len(h.Attributes))
|
|
|
|
attrs = append(attrs, attr.SourceCodeLocation(1+h.extraSkipInStack)...) // 3 attrs added here
|
|
|
|
attrs = append(attrs, attr.ServiceLayer(attr.LayerHandler), attr.SeverityLevel(h.Level))
|
2024-05-17 16:21:09 +00:00
|
|
|
if len(h.Name) > 0 {
|
|
|
|
attrs = append(attrs, attr.ServiceLayerName(h.Name))
|
|
|
|
}
|
2024-05-17 13:31:35 +00:00
|
|
|
attrs = append(attrs, h.Attributes...)
|
|
|
|
return attrs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h Handler) CustomAttrs(attrs ...interface{}) Handler {
|
|
|
|
h.Attributes = append(h.Attributes, attr.CollectAttributes(attrs...)...)
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) SkipMoreInCallStack(skip int) {
|
|
|
|
h.extraSkipInStack += skip
|
|
|
|
}
|
|
|
|
|
2024-05-17 16:21:09 +00:00
|
|
|
// Works the same as [Handler.IntoTraceAttributes]
|
|
|
|
func (h Handler) AsAttrs() []attribute.KeyValue {
|
|
|
|
h.extraSkipInStack += 1
|
|
|
|
return h.IntoTraceAttributes()
|
|
|
|
}
|
|
|
|
|
2024-05-17 13:31:35 +00:00
|
|
|
func (h Handler) AsOpts() trace.SpanStartEventOption {
|
|
|
|
h.extraSkipInStack += 1
|
|
|
|
return trace.WithAttributes(h.IntoTraceAttributes()...)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Service struct {
|
|
|
|
// Extra attributes to be attached. Can be also added with [Service.CustomAttrs] method.
|
|
|
|
Attributes []attribute.KeyValue
|
|
|
|
Level level.SeverityLevel
|
2024-05-17 16:21:09 +00:00
|
|
|
Name string
|
2024-05-17 13:31:35 +00:00
|
|
|
extraSkipInStack int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Service) IntoTraceAttributes() []attribute.KeyValue {
|
2024-05-17 16:21:09 +00:00
|
|
|
attrs := make([]attribute.KeyValue, 6+len(s.Attributes))
|
2024-05-17 13:31:35 +00:00
|
|
|
attrs = append(attrs, attr.SourceCodeLocation(1+s.extraSkipInStack)...)
|
|
|
|
attrs = append(attrs, attr.ServiceLayer(attr.LayerService), attr.SeverityLevel(s.Level))
|
2024-05-17 16:21:09 +00:00
|
|
|
if len(s.Name) > 0 {
|
|
|
|
attrs = append(attrs, attr.ServiceLayerName(s.Name))
|
|
|
|
}
|
2024-05-17 13:31:35 +00:00
|
|
|
attrs = append(attrs, s.Attributes...)
|
|
|
|
return attrs
|
|
|
|
}
|
|
|
|
|
2024-05-17 16:21:09 +00:00
|
|
|
// Works the same as [Service.IntoTraceAttributes]
|
|
|
|
func (s Service) AsAttrs() []attribute.KeyValue {
|
|
|
|
s.extraSkipInStack += 1
|
|
|
|
return s.IntoTraceAttributes()
|
|
|
|
}
|
|
|
|
|
2024-05-17 13:31:35 +00:00
|
|
|
func (s Service) CustomAttrs(attrs ...interface{}) Service {
|
|
|
|
s.Attributes = append(s.Attributes, attr.CollectAttributes(attrs...)...)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) SkipMoreInCallStack(skip int) {
|
|
|
|
s.extraSkipInStack += skip
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Service) AsOpts() trace.SpanStartEventOption {
|
|
|
|
s.extraSkipInStack += 1
|
|
|
|
return trace.WithAttributes(s.IntoTraceAttributes()...)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Repo struct {
|
|
|
|
// Extra attributes to be attached. Can be also added with [Repo.CustomAttrs] method
|
|
|
|
Attributes []attribute.KeyValue
|
|
|
|
Level level.SeverityLevel
|
2024-05-17 16:21:09 +00:00
|
|
|
Name string
|
2024-05-17 13:31:35 +00:00
|
|
|
extraSkipInStack int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r Repo) IntoTraceAttributes() []attribute.KeyValue {
|
2024-05-17 16:21:09 +00:00
|
|
|
attrs := make([]attribute.KeyValue, 6+len(r.Attributes))
|
2024-05-17 13:31:35 +00:00
|
|
|
attrs = append(attrs, attr.SourceCodeLocation(1+r.extraSkipInStack)...)
|
|
|
|
attrs = append(attrs, attr.ServiceLayer(attr.LayerRepository), attr.SeverityLevel(r.Level))
|
2024-05-17 16:21:09 +00:00
|
|
|
if len(r.Name) > 0 {
|
|
|
|
attrs = append(attrs, attr.ServiceLayerName(r.Name))
|
|
|
|
}
|
2024-05-17 13:31:35 +00:00
|
|
|
attrs = append(attrs, r.Attributes...)
|
|
|
|
return attrs
|
|
|
|
}
|
|
|
|
|
2024-05-17 16:21:09 +00:00
|
|
|
// Works the same as [Repo.IntoTraceAttributes]
|
|
|
|
func (r Repo) AsAttrs() []attribute.KeyValue {
|
|
|
|
r.extraSkipInStack += 1
|
|
|
|
return r.IntoTraceAttributes()
|
|
|
|
}
|
|
|
|
|
2024-05-17 13:31:35 +00:00
|
|
|
func (r Repo) CustomAttrs(attrs ...interface{}) Repo {
|
|
|
|
r.Attributes = append(r.Attributes, attr.CollectAttributes(attrs...)...)
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
func (r *Repo) SkipMoreInCallStack(skip int) {
|
|
|
|
r.extraSkipInStack += skip
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r Repo) AsOpts() trace.SpanStartEventOption {
|
|
|
|
r.extraSkipInStack += 1
|
|
|
|
return trace.WithAttributes(r.IntoTraceAttributes()...)
|
|
|
|
}
|