diff --git a/tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Elasticsearch/ElasticsearchNetCommon.cs b/tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Elasticsearch/ElasticsearchNetCommon.cs index 9fdf2cdffd04..612a8492e6ea 100644 --- a/tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Elasticsearch/ElasticsearchNetCommon.cs +++ b/tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Elasticsearch/ElasticsearchNetCommon.cs @@ -3,6 +3,8 @@ // This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc. // +#nullable enable + using System; using System.Threading; using Datadog.Trace.Configuration; @@ -16,12 +18,9 @@ internal static class ElasticsearchNetCommon public const string DatabaseType = "elasticsearch"; public const string ComponentValue = "elasticsearch-net"; - public static readonly Type CancellationTokenType = typeof(CancellationToken); - public static readonly Type RequestPipelineType = Type.GetType("Elasticsearch.Net.IRequestPipeline, Elasticsearch.Net"); - private static readonly IDatadogLogger Log = DatadogLogging.GetLoggerFor(typeof(ElasticsearchNetCommon)); - public static Scope CreateScope(Tracer tracer, IntegrationId integrationId, RequestPipelineStruct pipeline, T requestData) + public static Scope? CreateScope(Tracer tracer, IntegrationId integrationId, RequestPipelineStruct pipeline, T requestData) where T : IRequestData { if (!tracer.Settings.IsIntegrationEnabled(integrationId)) @@ -34,12 +33,16 @@ public static Scope CreateScope(Tracer tracer, IntegrationId integrationId, R var url = requestData.Uri?.ToString(); var scope = CreateScope(tracer, integrationId, method, pipeline.RequestParameters, out var tags); - tags.Url = url; - tags.Host = HttpRequestUtils.GetNormalizedHost(requestData.Uri?.Host); + if (tags is not null) + { + tags.Url = url; + tags.Host = HttpRequestUtils.GetNormalizedHost(requestData.Uri?.Host); + } + return scope; } - public static Scope CreateScope(Tracer tracer, IntegrationId integrationId, string method, object requestParameters, out ElasticsearchTags tags) + public static Scope? CreateScope(Tracer tracer, IntegrationId integrationId, string? method, object? requestParameters, out ElasticsearchTags? tags) { if (!tracer.Settings.IsIntegrationEnabled(integrationId)) { @@ -48,13 +51,13 @@ public static Scope CreateScope(Tracer tracer, IntegrationId integrationId, stri return null; } - string requestName = requestParameters?.GetType().Name.Replace("RequestParameters", string.Empty); + var requestName = requestParameters?.GetType().Name.Replace("RequestParameters", string.Empty); - string operationName = tracer.CurrentTraceSettings.Schema.Database.GetOperationName(DatabaseType); - string serviceName = tracer.CurrentTraceSettings.Schema.Database.GetServiceName(DatabaseType); + var operationName = tracer.CurrentTraceSettings.Schema.Database.GetOperationName(DatabaseType); + var serviceName = tracer.CurrentTraceSettings.Schema.Database.GetServiceName(DatabaseType); tags = tracer.CurrentTraceSettings.Schema.Database.CreateElasticsearchTags(); - Scope scope = null; + Scope? scope = null; try { diff --git a/tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Elasticsearch/V7/IApiCallDetails.cs b/tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Elasticsearch/V7/IApiCallDetails.cs index 2a8df49ccd3a..fb09988b26bc 100644 --- a/tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Elasticsearch/V7/IApiCallDetails.cs +++ b/tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Elasticsearch/V7/IApiCallDetails.cs @@ -3,12 +3,14 @@ // This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc. // +#nullable enable + using System; namespace Datadog.Trace.ClrProfiler.AutoInstrumentation.Elasticsearch.V7 { internal interface IApiCallDetails { - public Uri Uri { get; } + public Uri? Uri { get; } } } diff --git a/tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Elasticsearch/V7/IElasticsearchResponse.cs b/tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Elasticsearch/V7/IElasticsearchResponse.cs index 126d782ff5f3..82868026216b 100644 --- a/tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Elasticsearch/V7/IElasticsearchResponse.cs +++ b/tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Elasticsearch/V7/IElasticsearchResponse.cs @@ -3,10 +3,12 @@ // This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc. // +#nullable enable + namespace Datadog.Trace.ClrProfiler.AutoInstrumentation.Elasticsearch.V7 { internal interface IElasticsearchResponse { - IApiCallDetails ApiCall { get; } + IApiCallDetails? ApiCall { get; } } } diff --git a/tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Elasticsearch/V7/Transport_RequestAsync_Integration.cs b/tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Elasticsearch/V7/Transport_RequestAsync_Integration.cs index b9d0ca19e45b..fc13d7b31192 100644 --- a/tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Elasticsearch/V7/Transport_RequestAsync_Integration.cs +++ b/tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Elasticsearch/V7/Transport_RequestAsync_Integration.cs @@ -3,10 +3,13 @@ // This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc. // +#nullable enable + using System; using System.ComponentModel; using System.Threading; using Datadog.Trace.ClrProfiler.CallTarget; +using Datadog.Trace.DuckTyping; using Datadog.Trace.Util.Http; namespace Datadog.Trace.ClrProfiler.AutoInstrumentation.Elasticsearch.V7 @@ -44,7 +47,7 @@ public class Transport_RequestAsync_Integration /// Calltarget state value internal static CallTargetState OnMethodBegin(TTarget instance, THttpMethod method, string path, CancellationToken cancellationToken, TPostData postData, TRequestParameters requestParameters) { - var scope = ElasticsearchNetCommon.CreateScope(Tracer.Instance, ElasticsearchV7Constants.IntegrationId, method.ToString(), requestParameters, out var tags); + var scope = ElasticsearchNetCommon.CreateScope(Tracer.Instance, ElasticsearchV7Constants.IntegrationId, method?.ToString(), requestParameters, out var tags); return new CallTargetState(scope, tags); } @@ -60,11 +63,9 @@ internal static CallTargetState OnMethodBeginCalltarget state value /// A response value, in an async scenario will be T of Task of T internal static TResponse OnAsyncMethodEnd(TTarget instance, TResponse response, Exception exception, in CallTargetState state) - where TResponse : IElasticsearchResponse + where TResponse : IElasticsearchResponse, IDuckType { - var uri = response?.ApiCall?.Uri; - - if (uri != null) + if (response.Instance is not null && response.ApiCall?.Uri is { } uri) { var tags = (ElasticsearchTags)state.State; diff --git a/tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Elasticsearch/V7/Transport_Request_Integration.cs b/tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Elasticsearch/V7/Transport_Request_Integration.cs index 635e14cc26b1..78a23fc3ddc7 100644 --- a/tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Elasticsearch/V7/Transport_Request_Integration.cs +++ b/tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Elasticsearch/V7/Transport_Request_Integration.cs @@ -3,9 +3,12 @@ // This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc. // +#nullable enable + using System; using System.ComponentModel; using Datadog.Trace.ClrProfiler.CallTarget; +using Datadog.Trace.DuckTyping; using Datadog.Trace.Util.Http; namespace Datadog.Trace.ClrProfiler.AutoInstrumentation.Elasticsearch.V7 @@ -42,7 +45,7 @@ public class Transport_Request_Integration /// Calltarget state value internal static CallTargetState OnMethodBegin(TTarget instance, THttpMethod method, string path, TPostData postData, TRequestParameters requestParameters) { - var scope = ElasticsearchNetCommon.CreateScope(Tracer.Instance, ElasticsearchV7Constants.IntegrationId, method.ToString(), requestParameters, out var tags); + var scope = ElasticsearchNetCommon.CreateScope(Tracer.Instance, ElasticsearchV7Constants.IntegrationId, method?.ToString(), requestParameters, out var tags); return new CallTargetState(scope, tags); } @@ -58,11 +61,9 @@ internal static CallTargetState OnMethodBeginCalltarget state value /// A response value, in an async scenario will be T of Task of T internal static CallTargetReturn OnMethodEnd(TTarget instance, TResponse response, Exception exception, in CallTargetState state) - where TResponse : IElasticsearchResponse + where TResponse : IElasticsearchResponse, IDuckType { - var uri = response?.ApiCall?.Uri; - - if (uri != null) + if (response.Instance is not null && response.ApiCall?.Uri is { } uri) { var tags = (ElasticsearchTags)state.State; @@ -74,7 +75,7 @@ internal static CallTargetReturn OnMethodEnd(TTar } state.Scope.DisposeWithException(exception); - return new CallTargetReturn(response); + return new CallTargetReturn(response!); } } }