Skip to content

Commit 455d139

Browse files
committed
Remove modification of TracerSettings by CIVisibility
Use configuration source instead
1 parent dd8d03c commit 455d139

File tree

5 files changed

+74
-52
lines changed

5 files changed

+74
-52
lines changed

tracer/src/Datadog.Trace/Ci/CIVisibility.cs

-30
Original file line numberDiff line numberDiff line change
@@ -129,21 +129,6 @@ public static void Initialize()
129129

130130
var tracerSettings = settings.TracerSettings;
131131
Log.Debug("Setting up the test session name to: {TestSessionName}", settings.TestSessionName);
132-
133-
// Set the service name if empty
134-
if (string.IsNullOrEmpty(tracerSettings.ServiceName))
135-
{
136-
// Extract repository name from the git url and use it as a default service name.
137-
tracerSettings.ServiceName = GetServiceNameFromRepository(CIEnvironmentValues.Instance.Repository);
138-
tracerSettings.GlobalTags[CommonTags.UserProvidedTestServiceTag] = "false";
139-
}
140-
else
141-
{
142-
tracerSettings.GlobalTags[CommonTags.UserProvidedTestServiceTag] = "true";
143-
}
144-
145-
// Normalize the service name
146-
tracerSettings.ServiceName = NormalizerTraceProcessor.NormalizeService(tracerSettings.ServiceName);
147132
Log.Debug("Setting up the service name to: {ServiceName}", tracerSettings.ServiceName);
148133

149134
// Initialize Tracer
@@ -203,21 +188,6 @@ internal static void InitializeFromRunner(CIVisibilitySettings settings, IDiscov
203188

204189
var tracerSettings = settings.TracerSettings;
205190
Log.Debug("Setting up the test session name to: {TestSessionName}", settings.TestSessionName);
206-
207-
// Set the service name if empty
208-
if (string.IsNullOrEmpty(tracerSettings.ServiceName))
209-
{
210-
// Extract repository name from the git url and use it as a default service name.
211-
tracerSettings.ServiceName = GetServiceNameFromRepository(CIEnvironmentValues.Instance.Repository);
212-
tracerSettings.GlobalTags[CommonTags.UserProvidedTestServiceTag] = "false";
213-
}
214-
else
215-
{
216-
tracerSettings.GlobalTags[CommonTags.UserProvidedTestServiceTag] = "true";
217-
}
218-
219-
// Normalize the service name
220-
tracerSettings.ServiceName = NormalizerTraceProcessor.NormalizeService(tracerSettings.ServiceName);
221191
Log.Debug("Setting up the service name to: {ServiceName}", tracerSettings.ServiceName);
222192

223193
// Initialize Tracer

tracer/src/Datadog.Trace/Ci/Configuration/CIVisibilitySettings.cs

+19-20
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#nullable enable
66

77
using System;
8+
using System.Collections.Generic;
89
using System.Collections.Specialized;
910
using System.Linq;
1011
using System.Runtime.CompilerServices;
@@ -298,32 +299,30 @@ internal void SetDefaultManualInstrumentationSettings()
298299
}
299300

300301
private TracerSettings InitializeTracerSettings()
301-
{
302-
var source = GlobalConfigurationSource.CreateDefaultConfigurationSource();
303-
var defaultExcludedUrlSubstrings = string.Empty;
304-
var configResult = source.GetString(ConfigurationKeys.HttpClientExcludedUrlSubstrings, NullConfigurationTelemetry.Instance, validator: null, recordValue: false);
305-
if (configResult is { IsValid: true, Result: { } substrings } && !string.IsNullOrWhiteSpace(substrings))
306-
{
307-
defaultExcludedUrlSubstrings = substrings + ", ";
308-
}
302+
=> InitializeTracerSettings(GlobalConfigurationSource.CreateDefaultConfigurationSource());
309303

310-
source.Insert(0, new NameValueConfigurationSource(
311-
new NameValueCollection
312-
{
313-
[ConfigurationKeys.HttpClientExcludedUrlSubstrings] = defaultExcludedUrlSubstrings + "/session/FakeSessionIdForPollingPurposes",
314-
},
315-
ConfigurationOrigins.Calculated));
316-
317-
var tracerSettings = new TracerSettings(source, new ConfigurationTelemetry(), new OverrideErrorLog());
304+
// Internal for testing
305+
internal TracerSettings InitializeTracerSettings(CompositeConfigurationSource source)
306+
{
307+
// This is a somewhat hacky way to "tell" TracerSettings that we're running in CI Visibility
308+
// There's no doubt various other ways we could flag it based on values we're _already_ extracting,
309+
// but we don't want to set up too much interdependence there.
310+
var telemetry = new ConfigurationTelemetry();
311+
var additionalSource = new Dictionary<string, object?> { { ConfigurationKeys.CIVisibility.IsRunningInCiVisMode, true } };
318312

319313
if (Logs)
320314
{
321-
// Enable the direct log submission
322-
tracerSettings.LogSubmissionSettings.DirectLogSubmissionEnabledIntegrations.Add("XUnit");
323-
tracerSettings.LogSubmissionSettings.DirectLogSubmissionBatchPeriod = TimeSpan.FromSeconds(1);
315+
// fetch the "original" values, and update them with the CI Visibility settings
316+
var enabledDirectLogSubmissionIntegrations = new ConfigurationBuilder(source, telemetry)
317+
.WithKeys(ConfigurationKeys.DirectLogSubmission.EnabledIntegrations)
318+
.AsString();
319+
var logIntegrations = enabledDirectLogSubmissionIntegrations + ";XUnit";
320+
additionalSource[ConfigurationKeys.DirectLogSubmission.EnabledIntegrations] = logIntegrations;
321+
additionalSource[ConfigurationKeys.DirectLogSubmission.BatchPeriodSeconds] = 1;
324322
}
325323

326-
return tracerSettings;
324+
var newSource = new CompositeConfigurationSource([new DictionaryObjectConfigurationSource(additionalSource), source]);
325+
return new TracerSettings(newSource, telemetry, new OverrideErrorLog());
327326
}
328327
}
329328
}

tracer/src/Datadog.Trace/Configuration/ConfigurationKeys.cs

+5
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,11 @@ internal static partial class ConfigurationKeys
493493
/// </summary>
494494
public static class CIVisibility
495495
{
496+
/// <summary>
497+
/// An internal key used to "tell" tracer settings that we're running in CI Visibility mode
498+
/// </summary>
499+
public const string IsRunningInCiVisMode = "_DD_INTERNAL_IS_RUNNING_IN_CIVISIBILITY";
500+
496501
/// <summary>
497502
/// Configuration key for enabling or disabling CI Visibility.
498503
/// Default value is false (disabled).

tracer/src/Datadog.Trace/Configuration/TracerSettings.cs

+48-2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@
1010
using System.Diagnostics.CodeAnalysis;
1111
using System.Linq;
1212
using System.Text.RegularExpressions;
13+
using Datadog.Trace.Ci;
14+
using Datadog.Trace.Ci.CiEnvironment;
1315
using Datadog.Trace.ClrProfiler;
1416
using Datadog.Trace.ClrProfiler.ServerlessInstrumentation;
1517
using Datadog.Trace.Configuration.ConfigurationSources.Telemetry;
1618
using Datadog.Trace.Configuration.Telemetry;
1719
using Datadog.Trace.Logging;
1820
using Datadog.Trace.Logging.DirectSubmission;
21+
using Datadog.Trace.Processors;
1922
using Datadog.Trace.Propagators;
2023
using Datadog.Trace.Sampling;
2124
using Datadog.Trace.SourceGenerators;
@@ -87,6 +90,11 @@ internal TracerSettings(IConfigurationSource? source, IConfigurationTelemetry te
8790
GCPFunctionSettings = new ImmutableGCPFunctionSettings(source, _telemetry);
8891
IsRunningInGCPFunctions = GCPFunctionSettings.IsGCPFunction;
8992

93+
// We don't want/need to record this value, so explicitly use null telemetry
94+
var isRunningInCiVisibility = new ConfigurationBuilder(source, NullConfigurationTelemetry.Instance)
95+
.WithKeys(ConfigurationKeys.CIVisibility.IsRunningInCiVisMode)
96+
.AsBool(false);
97+
9098
LambdaMetadata = LambdaMetadata.Create();
9199

92100
IsRunningInAzureAppService = ImmutableAzureAppServiceSettings.GetIsAzureAppService(source, telemetry);
@@ -124,11 +132,34 @@ _ when x.ToBoolean() is { } boolean => boolean,
124132
.AsString();
125133

126134
var otelServiceName = config.WithKeys(ConfigurationKeys.OpenTelemetry.ServiceName).AsStringResult();
127-
ServiceName = config
135+
var serviceName = config
128136
.WithKeys(ConfigurationKeys.ServiceName, "DD_SERVICE_NAME")
129137
.AsStringResult()
130138
.OverrideWith(in otelServiceName, ErrorLog);
131139

140+
var isUserProvidedTestServiceTag = true;
141+
if (isRunningInCiVisibility)
142+
{
143+
// Set the service name if not set
144+
var ciVisServiceName = serviceName;
145+
if (string.IsNullOrEmpty(serviceName))
146+
{
147+
// Extract repository name from the git url and use it as a default service name.
148+
ciVisServiceName = CIVisibility.GetServiceNameFromRepository(CIEnvironmentValues.Instance.Repository);
149+
isUserProvidedTestServiceTag = false;
150+
}
151+
152+
// Normalize the service name
153+
ciVisServiceName = NormalizerTraceProcessor.NormalizeService(ciVisServiceName);
154+
if (ciVisServiceName != serviceName)
155+
{
156+
serviceName = ciVisServiceName;
157+
telemetry.Record(ConfigurationKeys.ServiceName, serviceName, recordValue: true, ConfigurationOrigins.Calculated);
158+
}
159+
}
160+
161+
ServiceName = serviceName;
162+
132163
ServiceVersion = config
133164
.WithKeys(ConfigurationKeys.ServiceVersion)
134165
.AsString();
@@ -204,6 +235,11 @@ _ when x.ToBoolean() is { } boolean => boolean,
204235
.Where(kvp => !string.IsNullOrWhiteSpace(kvp.Key) && !string.IsNullOrWhiteSpace(kvp.Value))
205236
.ToDictionary(kvp => kvp.Key.Trim(), kvp => kvp.Value.Trim());
206237

238+
if (isRunningInCiVisibility)
239+
{
240+
GlobalTags[Ci.Tags.CommonTags.UserProvidedTestServiceTag] = isUserProvidedTestServiceTag ? "true" : "false";
241+
}
242+
207243
var headerTagsNormalizationFixEnabled = config
208244
.WithKeys(ConfigurationKeys.FeatureFlags.HeaderTagsNormalizationFixEnabled)
209245
.AsBool(defaultValue: true);
@@ -477,9 +513,19 @@ _ when x.ToBoolean() is { } boolean => boolean,
477513
IsRunningInAzureAppService ? ImmutableAzureAppServiceSettings.DefaultHttpClientExclusions :
478514
LambdaMetadata is { IsRunningInLambda: true } m ? m.DefaultHttpClientExclusions : string.Empty);
479515

516+
if (isRunningInCiVisibility)
517+
{
518+
// always add the additional exclude in ci vis
519+
const string fakeSessionEndpoint = "/session/FakeSessionIdForPollingPurposes";
520+
urlSubstringSkips = string.IsNullOrEmpty(urlSubstringSkips)
521+
? fakeSessionEndpoint
522+
: $"{urlSubstringSkips},{fakeSessionEndpoint}";
523+
telemetry.Record(ConfigurationKeys.HttpClientExcludedUrlSubstrings, urlSubstringSkips, recordValue: true, ConfigurationOrigins.Calculated);
524+
}
525+
480526
HttpClientExcludedUrlSubstrings = !string.IsNullOrEmpty(urlSubstringSkips)
481527
? TrimSplitString(urlSubstringSkips.ToUpperInvariant(), commaSeparator)
482-
: Array.Empty<string>();
528+
: [];
483529

484530
DbmPropagationMode = config
485531
.WithKeys(ConfigurationKeys.DbmPropagationMode)

tracer/test/Datadog.Trace.Tests/Telemetry/ConfigurationTests.cs

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public class ConfigurationTests
2323
// Lambda handler extracts these directly from env var, and no reason to think that will change
2424
"_DD_EXTENSION_ENDPOINT",
2525
"_DD_EXTENSION_PATH",
26+
// Internal variable just used to "pass" settings to the
27+
"_DD_INTERNAL_IS_RUNNING_IN_CIVISIBILITY",
2628
// mini agent uses this directly from env var, and no reason to think that will change
2729
"DD_MINI_AGENT_PATH",
2830
"DD_ENTITY_ID", // Datadog.Trace.Vendors.StatsdClient.StatsdConfig.EntityIdEnvVar (we don't use this, it was just vendored in)

0 commit comments

Comments
 (0)