getsentry / getsentry/sentry-go

Improve getting & setting data on spans and transactions

Open
#991 0 comments 0 reactions 1 assignee Claimed by @giortzisg View on GitHub
Go Improvement
Dominant language
Go
Stars
1.1k
Forks
262
Avg merge
1d 48m
Merged PRs (30d)
7

Description

### Problem Statement

Currently, if I want to get attribute data from an existing span or transaction I need to check the existance and also typecast into the final value.
```go
if d, found := transaction.Data["dataAttr1"]; found {
if dataAttr1, ok := d.(int); ok {
transaction.SetData("dataAttr1", dataAttr1.(int)+42)
}
}
```
For getting multiple values, the code footprint would become fairly large. It would be nicer to be able to handle getting and casting values with a `GetData` function in the transaction/span layer.

### Solution Brainstorm

Not sure about the better way of doing that, but here are two proposals.
- Generic helper function:
```go
// signature
func GetData[T any](s *Span, name string) (T, error)

// usage
val, err := sentry.GetData[int](transaction, "int")
if err != nil {
// handle error
}
transaction.SetData("int", val+42)
```

pros: small footprint, less maintenance, custom data structures
cons: casting on runtime (not type safe), how to handle complex cases?

- Custom datastructure and types
not sure on exact implementation but something like [otel](https://github.com/open-telemetry/opentelemetry-go/blob/v1.35.0/attribute/value.go#L22) `Value` type?
``` go
type Value struct {
vtype Type
numeric uint64
stringly string
slice interface{}
}
```
pros: concrete data types (no interface{}), type safe
cons: bigger footprint, complex logic, more maintenance, no support for custom data types

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.