Skip to content

Commit 811d0eb

Browse files
authored
[Exporter.OTLP] Nullability for compiled in files - part 1 (#4282)
1 parent 905e5d9 commit 811d0eb

File tree

10 files changed

+46
-49
lines changed

10 files changed

+46
-49
lines changed

src/OpenTelemetry.Api/Internal/ExceptionExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
// limitations under the License.
1515
// </copyright>
1616

17+
#nullable enable
18+
1719
using System.Globalization;
1820

1921
namespace OpenTelemetry.Internal

src/OpenTelemetry.Api/Internal/SemanticConventions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
// limitations under the License.
1515
// </copyright>
1616

17+
#nullable enable
18+
1719
namespace OpenTelemetry.Trace
1820
{
1921
/// <summary>

src/OpenTelemetry.Api/Internal/SpanAttributeConstants.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
// limitations under the License.
1515
// </copyright>
1616

17+
#nullable enable
18+
1719
namespace OpenTelemetry.Trace
1820
{
1921
/// <summary>

src/OpenTelemetry.Api/Internal/StatusHelper.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
// limitations under the License.
1515
// </copyright>
1616

17+
#nullable enable
18+
1719
using System.Runtime.CompilerServices;
1820
using OpenTelemetry.Trace;
1921

@@ -26,7 +28,7 @@ internal static class StatusHelper
2628
public const string ErrorStatusCodeTagValue = "ERROR";
2729

2830
[MethodImpl(MethodImplOptions.AggressiveInlining)]
29-
public static string GetTagValueForStatusCode(StatusCode statusCode)
31+
public static string? GetTagValueForStatusCode(StatusCode statusCode)
3032
{
3133
return statusCode switch
3234
{
@@ -43,7 +45,7 @@ public static string GetTagValueForStatusCode(StatusCode statusCode)
4345
}
4446

4547
[MethodImpl(MethodImplOptions.AggressiveInlining)]
46-
public static StatusCode? GetStatusCodeForTagValue(string statusCodeTagValue)
48+
public static StatusCode? GetStatusCodeForTagValue(string? statusCodeTagValue)
4749
{
4850
return statusCodeTagValue switch
4951
{
@@ -52,15 +54,15 @@ public static string GetTagValueForStatusCode(StatusCode statusCode)
5254
* first because assumption is most spans will be
5355
* Unset, then Error. Ok is not set by the SDK.
5456
*/
55-
string _ when UnsetStatusCodeTagValue.Equals(statusCodeTagValue, StringComparison.OrdinalIgnoreCase) => StatusCode.Unset,
56-
string _ when ErrorStatusCodeTagValue.Equals(statusCodeTagValue, StringComparison.OrdinalIgnoreCase) => StatusCode.Error,
57-
string _ when OkStatusCodeTagValue.Equals(statusCodeTagValue, StringComparison.OrdinalIgnoreCase) => StatusCode.Ok,
57+
not null when UnsetStatusCodeTagValue.Equals(statusCodeTagValue, StringComparison.OrdinalIgnoreCase) => StatusCode.Unset,
58+
not null when ErrorStatusCodeTagValue.Equals(statusCodeTagValue, StringComparison.OrdinalIgnoreCase) => StatusCode.Error,
59+
not null when OkStatusCodeTagValue.Equals(statusCodeTagValue, StringComparison.OrdinalIgnoreCase) => StatusCode.Ok,
5860
_ => null,
5961
};
6062
}
6163

6264
[MethodImpl(MethodImplOptions.AggressiveInlining)]
63-
public static bool TryGetStatusCodeForTagValue(string statusCodeTagValue, out StatusCode statusCode)
65+
public static bool TryGetStatusCodeForTagValue(string? statusCodeTagValue, out StatusCode statusCode)
6466
{
6567
StatusCode? tempStatusCode = GetStatusCodeForTagValue(statusCodeTagValue);
6668

src/OpenTelemetry/Internal/OpenTelemetrySdkEventSource.cs

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
// limitations under the License.
1515
// </copyright>
1616

17+
#nullable enable
18+
1719
using System.Diagnostics;
1820
using System.Diagnostics.Tracing;
1921

@@ -39,15 +41,6 @@ public void SpanProcessorException(string evnt, Exception ex)
3941
}
4042
}
4143

42-
[NonEvent]
43-
public void TracestateExtractException(Exception ex)
44-
{
45-
if (this.IsEnabled(EventLevel.Warning, EventKeywords.All))
46-
{
47-
this.TracestateExtractError(ex.ToInvariantString());
48-
}
49-
}
50-
5144
[NonEvent]
5245
public void MetricObserverCallbackException(Exception exception)
5346
{
@@ -147,12 +140,6 @@ public void DroppedExportProcessorItems(string exportProcessorName, string expor
147140
}
148141
}
149142

150-
[Event(3, Message = "Exporter returned error '{0}'.", Level = EventLevel.Warning)]
151-
public void ExporterErrorResult(ExportResult exportResult)
152-
{
153-
this.WriteEvent(3, exportResult);
154-
}
155-
156143
[Event(4, Message = "Unknown error in SpanProcessor event '{0}': '{1}'.", Level = EventLevel.Error)]
157144
public void SpanProcessorException(string evnt, string ex)
158145
{
@@ -165,12 +152,6 @@ public void InvalidArgument(string methodName, string argumentName, string issue
165152
this.WriteEvent(8, methodName, argumentName, issue);
166153
}
167154

168-
[Event(14, Message = "Tracestate parse error: '{0}'", Level = EventLevel.Warning)]
169-
public void TracestateExtractError(string error)
170-
{
171-
this.WriteEvent(14, error);
172-
}
173-
174155
[Event(16, Message = "Exception occurred while invoking Observable instrument callback. Exception: '{0}'", Level = EventLevel.Warning)]
175156
public void ObservableInstrumentCallbackException(string exception)
176157
{
@@ -184,7 +165,7 @@ public void ActivityStarted(string name, string id)
184165
}
185166

186167
[Event(25, Message = "Activity stopped. Name = '{0}', Id = '{1}'.", Level = EventLevel.Verbose)]
187-
public void ActivityStopped(string name, string id)
168+
public void ActivityStopped(string name, string? id)
188169
{
189170
this.WriteEvent(25, name, id);
190171
}
@@ -298,7 +279,7 @@ public void TracerProviderSdkEvent(string message)
298279
}
299280

300281
[Event(47, Message = "{0} environment variable has an invalid value: '{1}'", Level = EventLevel.Warning)]
301-
public void InvalidEnvironmentVariable(string key, string value)
282+
public void InvalidEnvironmentVariable(string key, string? value)
302283
{
303284
this.WriteEvent(47, key, value);
304285
}
@@ -321,7 +302,7 @@ public override void Dispose()
321302

322303
protected override void OnEventSourceCreated(EventSource eventSource)
323304
{
324-
if (eventSource?.Name.StartsWith("OpenTelemetry", StringComparison.OrdinalIgnoreCase) == true)
305+
if (eventSource.Name.StartsWith("OpenTelemetry", StringComparison.OrdinalIgnoreCase))
325306
{
326307
this.eventSources.Add(eventSource);
327308
this.EnableEvents(eventSource, EventLevel.Verbose, EventKeywords.All);
@@ -332,8 +313,8 @@ protected override void OnEventSourceCreated(EventSource eventSource)
332313

333314
protected override void OnEventWritten(EventWrittenEventArgs e)
334315
{
335-
string message;
336-
if (e.Message != null && (e.Payload?.Count ?? 0) > 0)
316+
string? message;
317+
if (e.Message != null && e.Payload != null && e.Payload.Count > 0)
337318
{
338319
message = string.Format(e.Message, e.Payload.ToArray());
339320
}

src/OpenTelemetry/Internal/PeerServiceResolver.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
// limitations under the License.
1515
// </copyright>
1616

17+
#nullable enable
18+
1719
using System.Runtime.CompilerServices;
1820
using OpenTelemetry.Trace;
1921

@@ -32,19 +34,19 @@ internal static class PeerServiceResolver
3234

3335
public interface IPeerServiceState
3436
{
35-
string PeerService { get; set; }
37+
string? PeerService { get; set; }
3638

3739
int? PeerServicePriority { get; set; }
3840

39-
string HostName { get; set; }
41+
string? HostName { get; set; }
4042

41-
string IpAddress { get; set; }
43+
string? IpAddress { get; set; }
4244

4345
long Port { get; set; }
4446
}
4547

4648
[MethodImpl(MethodImplOptions.AggressiveInlining)]
47-
public static void InspectTag<T>(ref T state, string key, string value)
49+
public static void InspectTag<T>(ref T state, string key, string? value)
4850
where T : struct, IPeerServiceState
4951
{
5052
if (PeerServiceKeyResolutionDictionary.TryGetValue(key, out int priority)
@@ -78,7 +80,7 @@ public static void InspectTag<T>(ref T state, string key, long value)
7880
}
7981

8082
[MethodImpl(MethodImplOptions.AggressiveInlining)]
81-
public static void Resolve<T>(ref T state, out string peerServiceName, out bool addAsTag)
83+
public static void Resolve<T>(ref T state, out string? peerServiceName, out bool addAsTag)
8284
where T : struct, IPeerServiceState
8385
{
8486
peerServiceName = state.PeerService;

src/OpenTelemetry/Internal/PeriodicExportingMetricReaderHelper.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
// limitations under the License.
1515
// </copyright>
1616

17+
#nullable enable
18+
1719
namespace OpenTelemetry.Metrics;
1820

1921
internal static class PeriodicExportingMetricReaderHelper
@@ -28,10 +30,10 @@ internal static PeriodicExportingMetricReader CreatePeriodicExportingMetricReade
2830
int defaultExportTimeoutMilliseconds = DefaultExportTimeoutMilliseconds)
2931
{
3032
var exportInterval =
31-
options.PeriodicExportingMetricReaderOptions?.ExportIntervalMilliseconds ?? defaultExportIntervalMilliseconds;
33+
options.PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds ?? defaultExportIntervalMilliseconds;
3234

3335
var exportTimeout =
34-
options.PeriodicExportingMetricReaderOptions?.ExportTimeoutMilliseconds ?? defaultExportTimeoutMilliseconds;
36+
options.PeriodicExportingMetricReaderOptions.ExportTimeoutMilliseconds ?? defaultExportTimeoutMilliseconds;
3537

3638
var metricReader = new PeriodicExportingMetricReader(exporter, exportInterval, exportTimeout)
3739
{

src/OpenTelemetry/Internal/ResourceSemanticConventions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
// limitations under the License.
1515
// </copyright>
1616

17+
#nullable enable
18+
1719
namespace OpenTelemetry.Resources
1820
{
1921
internal static class ResourceSemanticConventions

test/OpenTelemetry.Tests/Internal/PeriodicExportingMetricReaderHelperTests.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@
1414
// limitations under the License.
1515
// </copyright>
1616

17+
#nullable enable
18+
1719
using Microsoft.Extensions.Configuration;
1820
using OpenTelemetry.Exporter;
1921
using OpenTelemetry.Metrics;
2022
using Xunit;
2123

2224
namespace OpenTelemetry.Internal.Tests
2325
{
24-
public class PeriodicExportingMetricReaderHelperTests : IDisposable
26+
public sealed class PeriodicExportingMetricReaderHelperTests : IDisposable
2527
{
2628
public PeriodicExportingMetricReaderHelperTests()
2729
{
@@ -140,14 +142,11 @@ private static void ClearEnvVars()
140142
}
141143

142144
private static PeriodicExportingMetricReader CreatePeriodicExportingMetricReader(
143-
MetricReaderOptions options = null)
145+
MetricReaderOptions? options = null)
144146
{
145-
if (options == null)
146-
{
147-
options = new();
148-
}
147+
options ??= new();
149148

150-
var dummyMetricExporter = new InMemoryExporter<Metric>(new Metric[0]);
149+
var dummyMetricExporter = new InMemoryExporter<Metric>(Array.Empty<Metric>());
151150
return PeriodicExportingMetricReaderHelper.CreatePeriodicExportingMetricReader(dummyMetricExporter, options);
152151
}
153152
}

test/OpenTelemetry.Tests/Internal/SelfDiagnosticsConfigRefresherTest.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
// limitations under the License.
1515
// </copyright>
1616

17+
#nullable enable
18+
1719
using System.Diagnostics;
1820
using System.Text;
1921
using Xunit;
@@ -43,7 +45,7 @@ public void SelfDiagnosticsConfigRefresher_OmitAsConfigured()
4345
using var configRefresher = new SelfDiagnosticsConfigRefresher();
4446

4547
// Emitting event of EventLevel.Warning
46-
OpenTelemetrySdkEventSource.Log.ExporterErrorResult(ExportResult.Success);
48+
OpenTelemetrySdkEventSource.Log.ObservableInstrumentCallbackException("exception");
4749

4850
int bufferSize = 512;
4951
byte[] actualBytes = ReadFile(bufferSize);
@@ -97,12 +99,12 @@ private static string ParseLogMessage(string logLine)
9799

98100
private static byte[] ReadFile(int byteCount)
99101
{
100-
var outputFileName = Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName) + "."
102+
var outputFileName = Path.GetFileName(Process.GetCurrentProcess().MainModule?.FileName) + "."
101103
+ Process.GetCurrentProcess().Id + ".log";
102104
var outputFilePath = Path.Combine(".", outputFileName);
103105
using var file = File.Open(outputFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
104106
byte[] actualBytes = new byte[byteCount];
105-
file.Read(actualBytes, 0, byteCount);
107+
_ = file.Read(actualBytes, 0, byteCount);
106108
return actualBytes;
107109
}
108110

@@ -126,6 +128,7 @@ private static void CleanupConfigFile()
126128
}
127129
catch
128130
{
131+
// ignore any exceptions while removing files
129132
}
130133
}
131134
}

0 commit comments

Comments
 (0)