Open
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.
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:
// 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 otelValue
type?
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