Skip to content

Commit

Permalink
Add nullable enable to elasticsearch (#4918)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewlock authored Nov 23, 2023
1 parent 60553cb commit 6fce37c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
// </copyright>

#nullable enable

using System;
using System.Threading;
using Datadog.Trace.Configuration;
Expand All @@ -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<T>(Tracer tracer, IntegrationId integrationId, RequestPipelineStruct pipeline, T requestData)
public static Scope? CreateScope<T>(Tracer tracer, IntegrationId integrationId, RequestPipelineStruct pipeline, T requestData)
where T : IRequestData
{
if (!tracer.Settings.IsIntegrationEnabled(integrationId))
Expand All @@ -34,12 +33,16 @@ public static Scope CreateScope<T>(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))
{
Expand All @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
// </copyright>

#nullable enable

using System;

namespace Datadog.Trace.ClrProfiler.AutoInstrumentation.Elasticsearch.V7
{
internal interface IApiCallDetails
{
public Uri Uri { get; }
public Uri? Uri { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
// </copyright>

#nullable enable

namespace Datadog.Trace.ClrProfiler.AutoInstrumentation.Elasticsearch.V7
{
internal interface IElasticsearchResponse
{
IApiCallDetails ApiCall { get; }
IApiCallDetails? ApiCall { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
// </copyright>

#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
Expand Down Expand Up @@ -44,7 +47,7 @@ public class Transport_RequestAsync_Integration
/// <returns>Calltarget state value</returns>
internal static CallTargetState OnMethodBegin<TTarget, THttpMethod, TPostData, TRequestParameters>(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);
}
Expand All @@ -60,11 +63,9 @@ internal static CallTargetState OnMethodBegin<TTarget, THttpMethod, TPostData, T
/// <param name="state">Calltarget state value</param>
/// <returns>A response value, in an async scenario will be T of Task of T</returns>
internal static TResponse OnAsyncMethodEnd<TTarget, TResponse>(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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
// </copyright>

#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
Expand Down Expand Up @@ -42,7 +45,7 @@ public class Transport_Request_Integration
/// <returns>Calltarget state value</returns>
internal static CallTargetState OnMethodBegin<TTarget, THttpMethod, TPostData, TRequestParameters>(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);
}
Expand All @@ -58,11 +61,9 @@ internal static CallTargetState OnMethodBegin<TTarget, THttpMethod, TPostData, T
/// <param name="state">Calltarget state value</param>
/// <returns>A response value, in an async scenario will be T of Task of T</returns>
internal static CallTargetReturn<TResponse> OnMethodEnd<TTarget, TResponse>(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;

Expand All @@ -74,7 +75,7 @@ internal static CallTargetReturn<TResponse> OnMethodEnd<TTarget, TResponse>(TTar
}

state.Scope.DisposeWithException(exception);
return new CallTargetReturn<TResponse>(response);
return new CallTargetReturn<TResponse>(response!);
}
}
}

0 comments on commit 6fce37c

Please sign in to comment.