Skip to content

Commit cdc506c

Browse files
authored
PLAT-12362 fix issue with span parentage when MakeCurrentContext is false (#122)
1 parent 06476c6 commit cdc506c

File tree

7 files changed

+87
-13
lines changed

7 files changed

+87
-13
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ features/fixtures/minimalapp/Packages
2727
features/fixtures/minimalapp/minimal_with_xcode
2828
features/fixtures/minimalapp/minimal_without_xcode
2929
features/fixtures/mazerunner/mazerunner_macos_BackUpThisFolder_ButDontShipItWithYourGame
30+
BugsnagPerformance/.vscode

BugsnagPerformance/Assets/BugsnagPerformance/Scripts/Internal/SpanFactory.cs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,27 +72,29 @@ internal Span StartCustomSpan(string name, SpanOptions spanOptions)
7272

7373
private Span CreateSpan(string name, SpanKind kind, SpanOptions spanOptions)
7474
{
75-
76-
if (spanOptions.ParentContext != null)
77-
{
78-
AddToContextStack(spanOptions.ParentContext);
79-
}
80-
81-
string traceId = string.Empty;
8275
string parentSpanId = null;
76+
string traceId;
8377
string spanId = GetNewSpanId();
8478

85-
var existingContext = GetCurrentContext();
86-
if (existingContext != null)
79+
if (spanOptions.ParentContext != null)
8780
{
88-
traceId = existingContext.TraceId;
89-
parentSpanId = existingContext.SpanId;
81+
traceId = spanOptions.ParentContext.TraceId;
82+
parentSpanId = spanOptions.ParentContext.SpanId;
9083
}
9184
else
9285
{
93-
traceId = GetNewTraceId();
86+
var existingContext = GetCurrentContext();
87+
if (existingContext != null)
88+
{
89+
traceId = existingContext.TraceId;
90+
parentSpanId = existingContext.SpanId;
91+
}
92+
else
93+
{
94+
traceId = GetNewTraceId();
95+
}
9496
}
95-
97+
9698
var newSpan = new Span(name, kind, spanId, traceId, parentSpanId, spanOptions.StartTime, spanOptions.IsFirstClass, _onSpanEnd);
9799

98100
if (spanOptions.MakeCurrentContext)

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## TBD
4+
5+
### Bug Fixes
6+
7+
- Fix an issue where spans with MakeCurrentContext set to false and passed as a parent would be added to the context stack. [#122](https://github.com/bugsnag/bugsnag-unity-performance/pull/122)
8+
39
## v1.4.1 (2024-11-06)
410

511
### Bug Fixes

features/fixtures/mazerunner/Assets/Scenes/MainScene.unity

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ MonoBehaviour:
389389
m_Script: {fileID: 11500000, guid: 94f564ad5b05945d9ac6e530f5873bfe, type: 3}
390390
m_Name:
391391
m_EditorClassIdentifier:
392+
ShouldStartNotifier: 0
392393
--- !u!1 &320782971
393394
GameObject:
394395
m_ObjectHideFlags: 0
@@ -1133,6 +1134,7 @@ GameObject:
11331134
- component: {fileID: 1609554508}
11341135
- component: {fileID: 1609554509}
11351136
- component: {fileID: 1609554510}
1137+
- component: {fileID: 1609554511}
11361138
m_Layer: 0
11371139
m_Name: NestedSpans
11381140
m_TagString: Untagged
@@ -1220,6 +1222,18 @@ MonoBehaviour:
12201222
m_Name:
12211223
m_EditorClassIdentifier:
12221224
ShouldStartNotifier: 0
1225+
--- !u!114 &1609554511
1226+
MonoBehaviour:
1227+
m_ObjectHideFlags: 0
1228+
m_CorrespondingSourceObject: {fileID: 0}
1229+
m_PrefabInstance: {fileID: 0}
1230+
m_PrefabAsset: {fileID: 0}
1231+
m_GameObject: {fileID: 1609554504}
1232+
m_Enabled: 1
1233+
m_EditorHideFlags: 0
1234+
m_Script: {fileID: 11500000, guid: 3d7da4daac88a4bb2be4043564a67ac0, type: 3}
1235+
m_Name:
1236+
m_EditorClassIdentifier:
12231237
--- !u!1 &2053587485
12241238
GameObject:
12251239
m_ObjectHideFlags: 0
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using BugsnagUnityPerformance;
4+
using UnityEngine;
5+
6+
public class PassContextWithMakeContextFalse : Scenario
7+
{
8+
public override void PreparePerformanceConfig(string apiKey, string host)
9+
{
10+
base.PreparePerformanceConfig(apiKey, host);
11+
SetMaxBatchSize(3);
12+
}
13+
14+
public override void Run()
15+
{
16+
var span1 = BugsnagPerformance.StartSpan("span1", new SpanOptions { MakeCurrentContext = false });
17+
var span2 = BugsnagPerformance.StartSpan("span2", new SpanOptions { ParentContext = span1 });
18+
span2.End();
19+
var span3 = BugsnagPerformance.StartSpan("span3");
20+
span3.End();
21+
span1.End();
22+
}
23+
}

features/fixtures/mazerunner/Assets/Scripts/Scenarios/NestedSpans/PassContextWithMakeContextFalse.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

features/nested_spans.feature

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,21 @@ Feature: Nested Spans
8585
* the trace payload field "resourceSpans.0.scopeSpans.0.spans.2" bool attribute "bugsnag.span.first_class" is false
8686

8787

88+
Scenario: Pass Context With Make Context False
89+
When I run the game in the "PassContextWithMakeContextFalse" state
90+
And I wait for 3 spans
91+
Then the trace Bugsnag-Integrity header is valid
92+
And the trace "Bugsnag-Api-Key" header equals "a35a2a72bd230ac0aa0f52715bbdc6aa"
93+
* the trace "Bugsnag-Sent-At" header matches the regex "^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d\.\d\d\dZ$"
94+
* the trace "Bugsnag-Span-Sampling" header equals "1:3"
95+
96+
* the span named "span1" exists
97+
* the span named "span2" exists
98+
* the span named "span3" exists
99+
100+
* the span named "span1" has no parent
101+
102+
* the span named "span1" is the parent of the span named "span2"
103+
104+
* the span named "span3" has no parent
88105

0 commit comments

Comments
 (0)