added event wrappers, bug fixes, API improvements

This commit is contained in:
2024-05-17 18:21:09 +02:00
parent 4f4a7e09c5
commit 076196c03e
7 changed files with 218 additions and 49 deletions

View File

@ -51,6 +51,7 @@ const (
SessionCurrencyIdKey = attribute.Key("session.currency_id")
ProcessThreadsAvailableKey = attribute.Key("process.threads_available")
ServiceLayerKey = attribute.Key("service.layer")
ServiceLayerNameKey = attribute.Key("service.layer_name")
)
type ServiceArchitectureLayer string
@ -244,3 +245,10 @@ func ServiceLayer(layer ServiceArchitectureLayer) attribute.KeyValue {
Value: attribute.StringValue(string(layer)),
}
}
func ServiceLayerName(name string) attribute.KeyValue {
return attribute.KeyValue{
Key: ServiceLayerNameKey,
Value: attribute.StringValue(name),
}
}

View File

@ -11,6 +11,7 @@ type Handler struct {
// Extra attributes to be attached. Can be also added with [Handler.CustomAttrs] method.
Attributes []attribute.KeyValue
Level level.SeverityLevel
Name string
extraSkipInStack int
}
@ -18,6 +19,9 @@ 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))
if len(h.Name) > 0 {
attrs = append(attrs, attr.ServiceLayerName(h.Name))
}
attrs = append(attrs, h.Attributes...)
return attrs
}
@ -31,6 +35,12 @@ func (h *Handler) SkipMoreInCallStack(skip int) {
h.extraSkipInStack += skip
}
// Works the same as [Handler.IntoTraceAttributes]
func (h Handler) AsAttrs() []attribute.KeyValue {
h.extraSkipInStack += 1
return h.IntoTraceAttributes()
}
func (h Handler) AsOpts() trace.SpanStartEventOption {
h.extraSkipInStack += 1
return trace.WithAttributes(h.IntoTraceAttributes()...)
@ -40,17 +50,27 @@ type Service struct {
// Extra attributes to be attached. Can be also added with [Service.CustomAttrs] method.
Attributes []attribute.KeyValue
Level level.SeverityLevel
Name string
extraSkipInStack int
}
func (s Service) IntoTraceAttributes() []attribute.KeyValue {
attrs := make([]attribute.KeyValue, 5+len(s.Attributes))
attrs := make([]attribute.KeyValue, 6+len(s.Attributes))
attrs = append(attrs, attr.SourceCodeLocation(1+s.extraSkipInStack)...)
attrs = append(attrs, attr.ServiceLayer(attr.LayerService), attr.SeverityLevel(s.Level))
if len(s.Name) > 0 {
attrs = append(attrs, attr.ServiceLayerName(s.Name))
}
attrs = append(attrs, s.Attributes...)
return attrs
}
// Works the same as [Service.IntoTraceAttributes]
func (s Service) AsAttrs() []attribute.KeyValue {
s.extraSkipInStack += 1
return s.IntoTraceAttributes()
}
func (s Service) CustomAttrs(attrs ...interface{}) Service {
s.Attributes = append(s.Attributes, attr.CollectAttributes(attrs...)...)
return s
@ -69,17 +89,27 @@ type Repo struct {
// Extra attributes to be attached. Can be also added with [Repo.CustomAttrs] method
Attributes []attribute.KeyValue
Level level.SeverityLevel
Name string
extraSkipInStack int
}
func (r Repo) IntoTraceAttributes() []attribute.KeyValue {
attrs := make([]attribute.KeyValue, 5+len(r.Attributes))
attrs := make([]attribute.KeyValue, 6+len(r.Attributes))
attrs = append(attrs, attr.SourceCodeLocation(1+r.extraSkipInStack)...)
attrs = append(attrs, attr.ServiceLayer(attr.LayerRepository), attr.SeverityLevel(r.Level))
if len(r.Name) > 0 {
attrs = append(attrs, attr.ServiceLayerName(r.Name))
}
attrs = append(attrs, r.Attributes...)
return attrs
}
// Works the same as [Repo.IntoTraceAttributes]
func (r Repo) AsAttrs() []attribute.KeyValue {
r.extraSkipInStack += 1
return r.IntoTraceAttributes()
}
func (r Repo) CustomAttrs(attrs ...interface{}) Repo {
r.Attributes = append(r.Attributes, attr.CollectAttributes(attrs...)...)
return r