feat(tracing): breadcrumb emitting

This commit is contained in:
DevMiner 2024-08-10 01:47:14 +02:00
parent ab48f8c2ee
commit 18f9295c60

71
span.go
View file

@ -116,6 +116,77 @@ func (s *Span) CaptureMessage(message string) *Span {
return s return s
} }
type (
SeverityLevel string
BreadcrumbType string
)
const (
SeverityDebug SeverityLevel = "debug"
SeverityInfo SeverityLevel = "info"
SeverityWarning SeverityLevel = "warning"
SeverityError SeverityLevel = "error"
SeverityFatal SeverityLevel = "fatal"
BreadcrumbTypeDefault BreadcrumbType = "default"
// BreadcrumbCatagoryConsole is rendered as a BreadcrumbTypeDebug
BreadcrumbCatagoryConsole string = "console"
BreadcrumbTypeDebug BreadcrumbType = "debug"
BreadcrumbTypeError BreadcrumbType = "error"
BreadcrumbCatagoryError string = "error"
// BreadcrumbTypeNavigration requires the `from` and `to` attributes
BreadcrumbTypeNavigration BreadcrumbType = "navigation"
BreadcrumbCatagoryNavigration string = "navigation"
// BreadcrumbTypeHTTP requires the `url`, `method` and `status_code` attributes
BreadcrumbTypeHTTP BreadcrumbType = "http"
BreadcrumbCatagoryHTTP string = "http"
BreadcrumbTypeInfo BreadcrumbType = "info"
BreadcrumbTypeQuery BreadcrumbType = "query"
BreadcrumbTypeTransaction BreadcrumbType = "transaction"
BreadcrumbCategorySentryTransaction string = "sentry.transaction"
// BreadcrumbTypeUI is also applicable for all `ui.*` categories
BreadcrumbTypeUI BreadcrumbType = "ui"
BreadcrumbTypeUser BreadcrumbType = "user"
BreadcrumbCategoryClick string = "click"
)
func (s *Span) CaptureBreadcrumb(level SeverityLevel, type_ BreadcrumbType, category string, message string, data map[string]any) *Span {
now := time.Now()
if hub := sentry.GetHubFromContext(s.Context()); hub != nil {
hub.AddBreadcrumb(&sentry.Breadcrumb{
Level: sentry.Level(level),
Type: string(type_),
Category: category,
Message: message,
Data: data,
Timestamp: now,
}, nil)
}
attrs := []attribute.KeyValue{
attribute.String("category", category),
attribute.String("type", string(type_)),
attribute.String("level", string(level)),
}
for k, v := range data {
attrs = append(attrs, attribute.String(k, fmt.Sprintf("%v", v)))
}
s.otel.AddEvent(message, trace.WithAttributes(attrs...), trace.WithTimestamp(now))
return s
}
func (s *Span) SetName(name string) *Span { func (s *Span) SetName(name string) *Span {
s.otel.SetName(name) s.otel.SetName(name)
s.sentry.Name = name s.sentry.Name = name