@@ -20,12 +20,12 @@ namespace Datadog.Trace.Configuration
20
20
public partial class ExporterSettings
21
21
{
22
22
/// <summary>
23
- /// The default host value for <see cref="AgentUriInternal "/>.
23
+ /// The default host value for <see cref="AgentUri "/>.
24
24
/// </summary>
25
25
public const string DefaultAgentHost = "localhost" ;
26
26
27
27
/// <summary>
28
- /// The default port value for <see cref="AgentUriInternal "/>.
28
+ /// The default port value for <see cref="AgentUri "/>.
29
29
/// </summary>
30
30
public const int DefaultAgentPort = 8126 ;
31
31
@@ -39,8 +39,7 @@ public partial class ExporterSettings
39
39
/// </summary>
40
40
internal const string DefaultTracesUnixDomainSocket = "/var/run/datadog/apm.socket" ;
41
41
42
- [ MemberNotNull ( nameof ( _agentUri ) ) ]
43
- private void ConfigureTraceTransport ( string ? agentUri , string ? tracesPipeName , string ? agentHost , int ? agentPort , string ? tracesUnixDomainSocketPath )
42
+ private TraceTransportSettings GetTraceTransport ( string ? agentUri , string ? tracesPipeName , string ? agentHost , int ? agentPort , string ? tracesUnixDomainSocketPath )
44
43
{
45
44
var origin = ConfigurationOrigins . Default ; // default because only called from constructor
46
45
@@ -49,37 +48,37 @@ private void ConfigureTraceTransport(string? agentUri, string? tracesPipeName, s
49
48
// For other cases (eg a configured unix domain socket path not found), we don't fallback as the problem could be fixed outside the application.
50
49
if ( ! string . IsNullOrWhiteSpace ( agentUri ) )
51
50
{
52
- if ( TrySetAgentUriAndTransport ( agentUri ! , origin ) )
51
+ if ( TryGetAgentUriAndTransport ( agentUri ! , origin , out var settings ) )
53
52
{
54
- return ;
53
+ return settings ;
55
54
}
56
55
}
57
56
58
57
if ( ! string . IsNullOrWhiteSpace ( tracesPipeName ) )
59
58
{
60
- TracesTransport = TracesTransportType . WindowsNamedPipe ;
61
- TracesPipeName = tracesPipeName ;
62
59
RecordTraceTransport ( nameof ( TracesTransportType . WindowsNamedPipe ) , origin ) ;
63
60
64
61
// The Uri isn't needed anymore in that case, just populating it for retro compatibility.
65
62
if ( ! Uri . TryCreate ( $ "http://{ agentHost ?? DefaultAgentHost } :{ agentPort ?? DefaultAgentPort } ", UriKind . Absolute , out var uri ) )
66
63
{
67
- // fallback so _agentUri is always non-null
64
+ // fallback so AgentUri is always non-null
68
65
uri = CreateDefaultUri ( ) ;
69
66
}
70
67
71
- SetAgentUriReplacingLocalhost ( uri , origin ) ;
72
- return ;
68
+ return new TraceTransportSettings (
69
+ TracesTransportType . WindowsNamedPipe ,
70
+ GetAgentUriReplacingLocalhost ( uri , origin ) ,
71
+ PipeName : tracesPipeName ) ;
73
72
}
74
73
75
74
// This property shouldn't have been introduced. We need to remove it as part of 3.0
76
75
// But while it's here, we need to handle it properly
77
76
if ( ! string . IsNullOrWhiteSpace ( tracesUnixDomainSocketPath ) )
78
77
{
79
78
#if NETCOREAPP3_1_OR_GREATER
80
- if ( TrySetAgentUriAndTransport ( UnixDomainSocketPrefix + tracesUnixDomainSocketPath , origin ) )
79
+ if ( TryGetAgentUriAndTransport ( UnixDomainSocketPrefix + tracesUnixDomainSocketPath , origin , out var settings ) )
81
80
{
82
- return ;
81
+ return settings ;
83
82
}
84
83
#else
85
84
// .NET Core 2.1 and .NET FX don't support Unix Domain Sockets
@@ -99,9 +98,9 @@ private void ConfigureTraceTransport(string? agentUri, string? tracesPipeName, s
99
98
// The agent will fail to start if it can not bind a port, so we need to override 8126 to prevent port conflict
100
99
// Port 0 means it will pick some random available port
101
100
102
- if ( TrySetAgentUriAndTransport ( agentHost ?? DefaultAgentHost , agentPort ?? DefaultAgentPort ) )
101
+ if ( TryGetAgentUriAndTransport ( agentHost ?? DefaultAgentHost , agentPort ?? DefaultAgentPort , out var settings ) )
103
102
{
104
- return ;
103
+ return settings ;
105
104
}
106
105
}
107
106
@@ -111,45 +110,44 @@ private void ConfigureTraceTransport(string? agentUri, string? tracesPipeName, s
111
110
{
112
111
// setting the urls as well for retro compatibility in the almost impossible case where someone
113
112
// used this config and accessed the AgentUri property as well (to avoid a potential null ref)
114
- // Using Set not TrySet because we know this is a valid Uri and ensures _agentUri is always non-null
115
- SetAgentUriAndTransport ( new Uri ( UnixDomainSocketPrefix + DefaultTracesUnixDomainSocket ) , origin ) ;
116
- return ;
113
+ // Using Get not TryGet because we know this is a valid Uri and ensures _agentUri is always non-null
114
+ return GetAgentUriAndTransport ( new Uri ( UnixDomainSocketPrefix + DefaultTracesUnixDomainSocket ) , origin ) ;
117
115
}
118
116
#endif
119
117
120
118
ValidationWarnings . Add ( "No transport configuration found, using default values" ) ;
121
119
122
120
// we know this URL is valid so don't use TrySet, otherwise can't guarantee _agentUri is non null
123
- SetAgentUriAndTransport ( CreateDefaultUri ( ) , origin ) ;
121
+ return GetAgentUriAndTransport ( CreateDefaultUri ( ) , origin ) ;
124
122
}
125
123
126
- [ MemberNotNullWhen ( true , nameof ( _agentUri ) ) ]
127
- private bool TrySetAgentUriAndTransport ( string host , int port )
124
+ private bool TryGetAgentUriAndTransport ( string host , int port , out TraceTransportSettings settings )
128
125
{
129
- return TrySetAgentUriAndTransport ( $ "http://{ host } :{ port } ", ConfigurationOrigins . Default ) ; // default because only called from constructor
126
+ return TryGetAgentUriAndTransport ( $ "http://{ host } :{ port } ", ConfigurationOrigins . Default , out settings ) ; // default because only called from constructor
130
127
}
131
128
132
- [ MemberNotNullWhen ( true , nameof ( _agentUri ) ) ]
133
- private bool TrySetAgentUriAndTransport ( string url , ConfigurationOrigins origin )
129
+ private bool TryGetAgentUriAndTransport ( string url , ConfigurationOrigins origin , out TraceTransportSettings settings )
134
130
{
135
131
if ( ! Uri . TryCreate ( url , UriKind . Absolute , out var uri ) )
136
132
{
137
133
ValidationWarnings . Add ( $ "The Uri: '{ url } ' is not valid. It won't be taken into account to send traces. Note that only absolute urls are accepted.") ;
134
+ settings = default ;
138
135
return false ;
139
136
}
140
137
141
- SetAgentUriAndTransport ( uri , ConfigurationOrigins . Default ) ; // default because only called from constructor
138
+ settings = GetAgentUriAndTransport ( uri , ConfigurationOrigins . Default ) ; // default because only called from constructor
142
139
return true ;
143
140
}
144
141
145
- [ MemberNotNull ( nameof ( _agentUri ) ) ]
146
- private void SetAgentUriAndTransport ( Uri uri , ConfigurationOrigins origin )
142
+ private TraceTransportSettings GetAgentUriAndTransport ( Uri uri , ConfigurationOrigins origin )
147
143
{
144
+ TracesTransportType transport ;
145
+ string ? udsPath ;
148
146
if ( uri . OriginalString . StartsWith ( UnixDomainSocketPrefix , StringComparison . OrdinalIgnoreCase ) )
149
147
{
150
148
#if NETCOREAPP3_1_OR_GREATER
151
- TracesTransport = TracesTransportType . UnixDomainSocket ;
152
- TracesUnixDomainSocketPath = uri . PathAndQuery ;
149
+ transport = TracesTransportType . UnixDomainSocket ;
150
+ udsPath = uri . PathAndQuery ;
153
151
154
152
var absoluteUri = uri . AbsoluteUri . Replace ( UnixDomainSocketPrefix , string . Empty ) ;
155
153
bool potentiallyInvalid = false ;
@@ -184,39 +182,43 @@ private void SetAgentUriAndTransport(Uri uri, ConfigurationOrigins origin)
184
182
recordValue : true ,
185
183
origin ,
186
184
TelemetryErrorCode . UdsOnUnsupportedPlatform ) ;
187
- SetAgentUriAndTransport ( CreateDefaultUri ( ) , ConfigurationOrigins . Calculated ) ;
188
- return ;
185
+ return GetAgentUriAndTransport ( CreateDefaultUri ( ) , ConfigurationOrigins . Calculated ) ;
189
186
#endif
190
187
}
191
188
else
192
189
{
193
- TracesTransport = TracesTransportType . Default ;
190
+ transport = TracesTransportType . Default ;
191
+ udsPath = null ;
194
192
RecordTraceTransport ( nameof ( TracesTransportType . Default ) , origin ) ;
195
193
}
196
194
197
- SetAgentUriReplacingLocalhost ( uri , origin ) ;
195
+ var agentUri = GetAgentUriReplacingLocalhost ( uri , origin ) ;
196
+ return new ( transport , agentUri , UdsPath : udsPath ) ;
198
197
}
199
198
200
- [ MemberNotNull ( nameof ( _agentUri ) ) ]
201
- private void SetAgentUriReplacingLocalhost ( Uri uri , ConfigurationOrigins origin )
199
+ private Uri GetAgentUriReplacingLocalhost ( Uri uri , ConfigurationOrigins origin )
202
200
{
201
+ Uri agentUri ;
203
202
if ( string . Equals ( uri . Host , "localhost" , StringComparison . OrdinalIgnoreCase ) )
204
203
{
205
204
// Replace localhost with 127.0.0.1 to avoid DNS resolution.
206
205
// When ipv6 is enabled, localhost is first resolved to ::1, which fails
207
206
// because the trace agent is only bound to ipv4.
208
207
// This causes delays when sending traces.
209
208
var builder = new UriBuilder ( uri ) { Host = "127.0.0.1" } ;
210
- _agentUri = builder . Uri ;
209
+ agentUri = builder . Uri ;
211
210
}
212
211
else
213
212
{
214
- _agentUri = uri ;
213
+ agentUri = uri ;
215
214
}
216
215
217
- _telemetry . Record ( ConfigurationKeys . AgentUri , _agentUri . ToString ( ) , recordValue : true , origin ) ;
216
+ _telemetry . Record ( ConfigurationKeys . AgentUri , agentUri . ToString ( ) , recordValue : true , origin ) ;
217
+ return agentUri ;
218
218
}
219
219
220
220
private Uri CreateDefaultUri ( ) => new Uri ( $ "http://{ DefaultAgentHost } :{ DefaultAgentPort } ") ;
221
+
222
+ private readonly record struct TraceTransportSettings ( TracesTransportType Transport , Uri AgentUri , string ? UdsPath = null , string ? PipeName = null ) ;
221
223
}
222
224
}
0 commit comments