Skip to content

Commit

Permalink
Fix setting of tracing variables (#806)
Browse files Browse the repository at this point in the history
  • Loading branch information
ribice committed Apr 11, 2024
1 parent 5d6374c commit 54a39b2
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 11 deletions.
27 changes: 16 additions & 11 deletions traces_profiler.go
Expand Up @@ -69,22 +69,27 @@ func (info *profileInfo) UpdateFromEvent(event *Event) {
info.Dist = event.Dist
info.Transaction.ID = event.EventID

if runtimeContext, ok := event.Contexts["runtime"]; ok {
if value, ok := runtimeContext["name"]; !ok {
info.Runtime.Name = value.(string)
getStringFromContext := func(context map[string]interface{}, originalValue, key string) string {
v, ok := context[key]
if !ok {
return originalValue
}
if value, ok := runtimeContext["version"]; !ok {
info.Runtime.Version = value.(string)

if s, ok := v.(string); ok {
return s
}

return originalValue
}

if runtimeContext, ok := event.Contexts["runtime"]; ok {
info.Runtime.Name = getStringFromContext(runtimeContext, info.Runtime.Name, "name")
info.Runtime.Version = getStringFromContext(runtimeContext, info.Runtime.Version, "version")
}
if osContext, ok := event.Contexts["os"]; ok {
if value, ok := osContext["name"]; !ok {
info.OS.Name = value.(string)
}
info.OS.Name = getStringFromContext(osContext, info.OS.Name, "name")
}
if deviceContext, ok := event.Contexts["device"]; ok {
if value, ok := deviceContext["arch"]; !ok {
info.Device.Architecture = value.(string)
}
info.Device.Architecture = getStringFromContext(deviceContext, info.Device.Architecture, "arch")
}
}
81 changes: 81 additions & 0 deletions traces_profiler_test.go
Expand Up @@ -71,3 +71,84 @@ func TestTraceProfilingDisabled(t *testing.T) {
require.Equal(transactionType, event.Type)
require.Nil(event.sdkMetaData.transactionProfile)
}
func TestUpdateFromEvent(t *testing.T) {
var require = require.New(t)

t.Parallel()

cases := map[string]struct {
event *Event
original *profileInfo
expected *profileInfo
}{
"basic": {
event: &Event{
Environment: "test",
Platform: "go",
Release: "v1.0.0",
Dist: "dist",
EventID: "12345",
Contexts: map[string]Context{
"runtime": map[string]any{"name": "go", "version": "1.16"},
"os": map[string]any{"name": "linux"},
"device": map[string]any{"arch": "x86_64"},
},
},
expected: &profileInfo{
Environment: "test",
Platform: "go",
Release: "v1.0.0",
Dist: "dist",
Transaction: profileTransaction{ID: "12345"},
Runtime: profileRuntime{Name: "go", Version: "1.16"},
OS: profileOS{Name: "linux"},
Device: profileDevice{Architecture: "x86_64"},
},
original: &profileInfo{},
},
"values from original": {
event: &Event{
Environment: "test",
Platform: "go",
Release: "v1.0.0",
Dist: "dist",
EventID: "12345",
Contexts: map[string]Context{
"runtime": map[string]any{"name": "go"},
"os": map[string]any{"name": "linux"},
"device": map[string]any{"arch": 86},
},
},
original: &profileInfo{
Environment: "original",
Platform: "original",
Release: "original",
Dist: "original",
Transaction: profileTransaction{ID: "original"},
Runtime: profileRuntime{Name: "original", Version: "original"},
OS: profileOS{Name: "original"},
Device: profileDevice{Architecture: "original"},
},
expected: &profileInfo{
Environment: "test",
Platform: "go",
Release: "v1.0.0",
Dist: "dist",
Transaction: profileTransaction{ID: "12345"},
Runtime: profileRuntime{Name: "go", Version: "original"},
OS: profileOS{Name: "linux"},
Device: profileDevice{Architecture: "original"},
},
},
}

for name, tc := range cases {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
info := tc.original
info.UpdateFromEvent(tc.event)
require.Equal(tc.expected, info)
})
}
}

0 comments on commit 54a39b2

Please sign in to comment.