Repository navigation
[API] Isolate ambient baggage updates across execution contexts #8042
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Kielek
wants to merge
11
commits into
open-telemetry:main
Choose a base branch
from
Kielek:codex/ant-2026-yxedg1g8-baggage-isolation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+363
−12
Open
Changes from 2 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8faa883
[API] Isolate ambient baggage updates across execution contexts
Kielek 20ada3a
PR number in changelog
Kielek e440eff
Reuse shared temporary directory in baggage configuration tests
Kielek 7636bc4
Remove baggage AppContext-specific test harness
Kielek e53872a
Clarify baggage compatibility switch documentation
Kielek a70e0da
Merge branch 'main' into codex/ant-2026-yxedg1g8-baggage-isolation
Kielek 3c24ecd
Merge branch 'main' into codex/ant-2026-yxedg1g8-baggage-isolation
matt-hensley 27a43f9
Merge branch 'main' into codex/ant-2026-yxedg1g8-baggage-isolation
matt-hensley 78e0618
Merge branch 'main' into codex/ant-2026-yxedg1g8-baggage-isolation
Kielek a687448
Merge branch 'main' into codex/ant-2026-yxedg1g8-baggage-isolation
martincostello c15cf9c
Merge branch 'main' into codex/ant-2026-yxedg1g8-baggage-isolation
Kielek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
test/OpenTelemetry.Api.Tests/BaggageConfigurationTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| using System.Diagnostics; | ||
| #if NET | ||
| using System.Text.Json.Nodes; | ||
| #else | ||
| using System.Xml; | ||
| #endif | ||
|
|
||
| namespace OpenTelemetry.Tests; | ||
|
|
||
| public class BaggageConfigurationTests | ||
| { | ||
| private const string AppContextSwitch = "OpenTelemetry.Baggage.AllowUnsafeLegacyFlow"; | ||
|
|
||
| [Theory] | ||
| [InlineData(null)] | ||
| [InlineData(false)] | ||
| [InlineData(true)] | ||
| public Task FlowModeIsSelectedAtProcessStartup(bool? enabled) | ||
| => RunChildProcess(enabled); | ||
|
|
||
| private static async Task RunChildProcess(bool? appContextValue) | ||
| { | ||
| var directory = Path.Combine(Path.GetTempPath(), $"OpenTelemetry.BaggageTests-{Guid.NewGuid():N}"); | ||
| Directory.CreateDirectory(directory); | ||
|
martincostello marked this conversation as resolved.
Resolved
|
||
| try | ||
| { | ||
| await RunChildProcess(CreateStartInfo(directory, appContextValue), appContextValue); | ||
| } | ||
| finally | ||
| { | ||
| Directory.Delete(directory, recursive: true); | ||
| } | ||
| } | ||
|
|
||
| private static async Task RunChildProcess(ProcessStartInfo startInfo, bool? appContextValue) | ||
|
Kielek marked this conversation as resolved.
Resolved
|
||
| { | ||
| using var process = Process.Start(startInfo); | ||
| Assert.NotNull(process); | ||
| #if NET | ||
| var output = process.StandardOutput.ReadToEndAsync(TestContext.Current.CancellationToken); | ||
| var error = process.StandardError.ReadToEndAsync(TestContext.Current.CancellationToken); | ||
| #else | ||
| var output = process.StandardOutput.ReadToEndAsync(); | ||
| var error = process.StandardError.ReadToEndAsync(); | ||
| #endif | ||
|
|
||
| try | ||
| { | ||
| #if NET | ||
| using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(30)); | ||
| await process.WaitForExitAsync(timeout.Token); | ||
| #else | ||
| Assert.True(process.WaitForExit(30_000), "Baggage child test process timed out."); | ||
| #endif | ||
| Assert.True( | ||
| process.ExitCode == 0, | ||
| $"Baggage AppContext '{appContextValue?.ToString() ?? "<unset>"}' failed:\n{await output}\n{await error}"); | ||
| } | ||
| finally | ||
| { | ||
| if (!process.HasExited) | ||
| { | ||
| process.Kill(); | ||
| #if NET | ||
| using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(5)); | ||
| await process.WaitForExitAsync(timeout.Token); | ||
| #else | ||
| Assert.True(process.WaitForExit(5_000), "Baggage child test process did not exit after termination."); | ||
| #endif | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static ProcessStartInfo CreateStartInfo(string directory, bool? enabled) | ||
| { | ||
| var assemblyPath = typeof(BaggageTests).Assembly.Location; | ||
| #if NET | ||
| var config = JsonNode.Parse(File.ReadAllText(Path.ChangeExtension(assemblyPath, ".runtimeconfig.json")))!; | ||
| var options = config["runtimeOptions"]!; | ||
| var properties = options["configProperties"]?.AsObject(); | ||
| if (properties == null) | ||
| { | ||
| properties = []; | ||
| options["configProperties"] = properties; | ||
| } | ||
|
|
||
| properties.Remove(AppContextSwitch); | ||
| if (enabled.HasValue) | ||
| { | ||
| properties[AppContextSwitch] = enabled.Value; | ||
| } | ||
|
|
||
| var configPath = Path.Combine(directory, "test.runtimeconfig.json"); | ||
| File.WriteAllText(configPath, config.ToJsonString()); | ||
| var startInfo = new ProcessStartInfo(Environment.GetEnvironmentVariable("DOTNET_HOST_PATH") ?? "dotnet"); | ||
| startInfo.ArgumentList.Add("exec"); | ||
| startInfo.ArgumentList.Add("--runtimeconfig"); | ||
| startInfo.ArgumentList.Add(configPath); | ||
| startInfo.ArgumentList.Add(assemblyPath); | ||
| startInfo.ArgumentList.Add("--filter-class"); | ||
| startInfo.ArgumentList.Add(typeof(BaggageTests).FullName!); | ||
| #else | ||
| // Framework loads configuration next to the executable. Give the child | ||
| // its own copy without changing the parent's executable or configuration. | ||
| foreach (var file in Directory.EnumerateFiles(Path.GetDirectoryName(assemblyPath)!)) | ||
| { | ||
| File.Copy(file, Path.Combine(directory, Path.GetFileName(file))); | ||
| } | ||
|
|
||
| var childAssemblyPath = Path.Combine(directory, Path.GetFileName(assemblyPath)); | ||
| var configPath = childAssemblyPath + ".config"; | ||
| var config = new XmlDocument { XmlResolver = null }; | ||
| using (var reader = XmlReader.Create(configPath, new XmlReaderSettings { DtdProcessing = DtdProcessing.Prohibit, XmlResolver = null })) | ||
| { | ||
| config.Load(reader); | ||
| } | ||
|
|
||
| var runtime = config.SelectSingleNode("/configuration/runtime")!; | ||
| var overrides = (XmlElement?)runtime.SelectSingleNode("AppContextSwitchOverrides"); | ||
| if (overrides == null) | ||
| { | ||
| overrides = config.CreateElement("AppContextSwitchOverrides"); | ||
| runtime.AppendChild(overrides); | ||
| } | ||
|
|
||
| var switches = overrides.GetAttribute("value").Split(';') | ||
| .Where(value => !string.IsNullOrWhiteSpace(value) | ||
| && !value.StartsWith(AppContextSwitch + "=", StringComparison.Ordinal)) | ||
| .ToList(); | ||
| if (enabled.HasValue) | ||
| { | ||
| switches.Add($"{AppContextSwitch}={enabled.Value}"); | ||
| } | ||
|
|
||
| if (switches.Count == 0) | ||
| { | ||
| runtime.RemoveChild(overrides); | ||
| } | ||
| else | ||
| { | ||
| overrides.SetAttribute("value", string.Join(";", switches)); | ||
| } | ||
|
|
||
| config.Save(configPath); | ||
| var startInfo = new ProcessStartInfo(childAssemblyPath) | ||
| { | ||
| Arguments = $"--filter-class {typeof(BaggageTests).FullName}", | ||
| }; | ||
| #endif | ||
| startInfo.UseShellExecute = false; | ||
| startInfo.RedirectStandardOutput = true; | ||
| startInfo.RedirectStandardError = true; | ||
| return startInfo; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.