Skip to content

Commit ce6a600

Browse files
author
Timothy Mothra
authored
[AzureMonitorExporter] fix more nullables (Azure#35047)
* fix nullables * cleanup
1 parent 23bad1b commit ce6a600

File tree

5 files changed

+18
-20
lines changed

5 files changed

+18
-20
lines changed

sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Customizations/Models/RemoteDependencyData.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,23 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4-
#nullable disable // TODO: remove and fix errors
5-
64
using System.Collections.Generic;
75
using System.Diagnostics;
86
using System.Globalization;
97

108
using Azure.Core;
119
using Azure.Monitor.OpenTelemetry.Exporter.Internals;
1210

13-
using OpenTelemetry.Trace;
14-
1511
namespace Azure.Monitor.OpenTelemetry.Exporter.Models
1612
{
1713
internal partial class RemoteDependencyData
1814
{
1915
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/database.md#connection-level-attributes
20-
internal static readonly HashSet<string> s_sqlDbs = new HashSet<string>() { "mssql" };
16+
internal static readonly HashSet<string?> s_sqlDbs = new HashSet<string?>() { "mssql" };
2117

2218
public RemoteDependencyData(int version, Activity activity, ref TagEnumerationState monitorTags) : base(version)
2319
{
24-
string httpUrl = null;
20+
string? httpUrl = null;
2521
string dependencyName;
2622

2723
if (monitorTags.activityType == OperationType.Http)

sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Customizations/Models/RequestData.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4-
#nullable disable // TODO: remove and fix errors
5-
64
using System.Diagnostics;
75
using System.Globalization;
86
using Azure.Core;
@@ -14,7 +12,7 @@ internal partial class RequestData
1412
{
1513
public RequestData(int version, Activity activity, ref TagEnumerationState monitorTags) : base(version)
1614
{
17-
string url = null;
15+
string? url = null;
1816

1917
switch (monitorTags.activityType)
2018
{
@@ -35,7 +33,7 @@ public RequestData(int version, Activity activity, ref TagEnumerationState monit
3533
?.ToString().Truncate(SchemaConstants.RequestData_ResponseCode_MaxLength)
3634
?? "0";
3735

38-
Success = isSuccess(activity, ResponseCode, monitorTags.activityType);
36+
Success = IsSuccess(activity, ResponseCode, monitorTags.activityType);
3937

4038
Url = url.Truncate(SchemaConstants.RequestData_Url_MaxLength);
4139
Properties = new ChangeTrackingDictionary<string, string>();
@@ -45,9 +43,11 @@ public RequestData(int version, Activity activity, ref TagEnumerationState monit
4543
TraceHelper.AddPropertiesToTelemetry(Properties, ref monitorTags.UnMappedTags);
4644
}
4745

48-
internal static bool isSuccess(Activity activity, string responseCode, OperationType operationType)
46+
internal static bool IsSuccess(Activity activity, string? responseCode, OperationType operationType)
4947
{
50-
if (operationType == OperationType.Http && int.TryParse(responseCode, out int statusCode))
48+
if (operationType == OperationType.Http
49+
&& responseCode != null
50+
&& int.TryParse(responseCode, out int statusCode))
5151
{
5252
bool isSuccessStatusCode = statusCode != 0 && statusCode < 400;
5353
return activity.Status != ActivityStatusCode.Error && isSuccessStatusCode;

sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Customizations/Models/TelemetryExceptionData.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4-
#nullable disable // TODO: remove and fix errors
5-
64
using System;
75
using System.Collections.Generic;
86
using System.Globalization;
@@ -20,6 +18,11 @@ internal partial class TelemetryExceptionData
2018

2119
public TelemetryExceptionData(int version, LogRecord logRecord) : base(version)
2220
{
21+
if (logRecord.Exception == null)
22+
{
23+
throw new ArgumentNullException(nameof(logRecord), "logRecord.Exception cannot be null.");
24+
}
25+
2326
Properties = new ChangeTrackingDictionary<string, string>();
2427
Measurements = new ChangeTrackingDictionary<string, double>();
2528

@@ -55,16 +58,15 @@ public TelemetryExceptionData(int version, LogRecord logRecord) : base(version)
5558
Exceptions = exceptions;
5659
}
5760

58-
private void ConvertExceptionTree(Exception exception, string message, TelemetryExceptionDetails parentExceptionDetails, List<TelemetryExceptionDetails> exceptions)
61+
private void ConvertExceptionTree(Exception exception, string? message, TelemetryExceptionDetails? parentExceptionDetails, List<TelemetryExceptionDetails> exceptions)
5962
{
6063
// For upper level exception see if message was provided and do not use exception.message in that case
6164
if (parentExceptionDetails != null && string.IsNullOrWhiteSpace(message))
6265
{
63-
message = exception?.Message;
66+
message = exception.Message;
6467
}
6568

66-
TelemetryExceptionDetails exceptionDetails = new TelemetryExceptionDetails(exception, message,
67-
parentExceptionDetails);
69+
TelemetryExceptionDetails exceptionDetails = new TelemetryExceptionDetails(exception, message, parentExceptionDetails);
6870

6971
exceptions.Add(exceptionDetails);
7072

sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Customizations/Models/TelemetryExceptionDetails.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ internal partial class TelemetryExceptionDetails
1414
/// <summary>
1515
/// Creates a new instance of ExceptionDetails from a System.Exception and a parent ExceptionDetails.
1616
/// </summary>
17-
internal TelemetryExceptionDetails(Exception exception, string message, TelemetryExceptionDetails? parentExceptionDetails)
17+
internal TelemetryExceptionDetails(Exception exception, string? message, TelemetryExceptionDetails? parentExceptionDetails)
1818
{
1919
if (exception == null)
2020
{

sdk/monitor/Azure.Monitor.OpenTelemetry.Exporter/src/Internals/StandardMetricsExtractionProcessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ private void ReportRequestDurationMetric(Activity activity, string statusCodeAtt
7373
tags.Add(new KeyValuePair<string, object?>(StandardMetricConstants.IsAutoCollectedKey, "True"));
7474
tags.Add(new KeyValuePair<string, object?>(StandardMetricConstants.CloudRoleInstanceKey, StandardMetricResource?.RoleInstance));
7575
tags.Add(new KeyValuePair<string, object?>(StandardMetricConstants.CloudRoleNameKey, StandardMetricResource?.RoleName));
76-
tags.Add(new KeyValuePair<string, object?>(StandardMetricConstants.RequestSuccessKey, RequestData.isSuccess(activity, statusCodeAttributeValue, OperationType.Http)));
76+
tags.Add(new KeyValuePair<string, object?>(StandardMetricConstants.RequestSuccessKey, RequestData.IsSuccess(activity, statusCodeAttributeValue, OperationType.Http)));
7777

7878
// Report metric
7979
_requestDuration.Record(activity.Duration.TotalMilliseconds, tags);

0 commit comments

Comments
 (0)