Skip to content

Commit

Permalink
[Instrumentation.ElasticsearchClient] Fix analysis warnings (#961)
Browse files Browse the repository at this point in the history
  • Loading branch information
martincostello authored Feb 8, 2023
1 parent a2960dc commit 429fdbb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class ElasticsearchClientInstrumentationOptions
/// <summary>
/// Gets or sets a value indicating whether the JSON request body should be parsed out of the request debug information and formatted as indented JSON.
/// </summary>
public bool ParseAndFormatRequest { get; set; } = false;
public bool ParseAndFormatRequest { get; set; }

/// <summary>
/// Gets or sets a value indicating whether or not the OpenTelemetry.Instrumentation.ElasticsearchClient
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ public override void OnStartActivity(Activity activity, object payload)
SuppressInstrumentationScope.Enter();
}

var elasticIndex = this.GetElasticIndex(uri);
activity.DisplayName = this.GetDisplayName(activity, method, elasticIndex);
var elasticIndex = GetElasticIndex(uri);
activity.DisplayName = GetDisplayName(activity, method, elasticIndex);
activity.SetTag(SemanticConventions.AttributeDbSystem, DatabaseSystemName);

if (elasticIndex != null)
Expand Down Expand Up @@ -144,7 +144,7 @@ public override void OnStopActivity(Activity activity, object payload)
var debugInformation = this.debugInformationFetcher.Fetch(payload);
if (debugInformation != null && this.options.SetDbStatementForRequest)
{
var dbStatement = this.ParseAndFormatRequest(activity, debugInformation);
var dbStatement = this.ParseAndFormatRequest(debugInformation);
if (this.options.MaxDbStatementLength > 0 && dbStatement.Length > this.options.MaxDbStatementLength)
{
dbStatement = dbStatement.Substring(0, this.options.MaxDbStatementLength);
Expand Down Expand Up @@ -201,7 +201,7 @@ public override void OnStopActivity(Activity activity, object payload)
}
}

private string GetDisplayName(Activity activity, object method, string elasticType = null)
private static string GetDisplayName(Activity activity, object method, string elasticType = null)
{
switch (activity.OperationName)
{
Expand All @@ -223,7 +223,7 @@ private string GetDisplayName(Activity activity, object method, string elasticTy
}
}

private string GetElasticIndex(Uri uri)
private static string GetElasticIndex(Uri uri)
{
// first segment is always /
if (uri.Segments.Length < 2)
Expand All @@ -232,7 +232,7 @@ private string GetElasticIndex(Uri uri)
}

// operations starting with _ are not indices (_cat, _search, etc)
if (uri.Segments[1].StartsWith("_"))
if (uri.Segments[1].StartsWith("_", StringComparison.Ordinal))
{
return null;
}
Expand All @@ -245,15 +245,15 @@ private string GetElasticIndex(Uri uri)
return null;
}

if (elasticType.EndsWith("/"))
if (elasticType.EndsWith("/", StringComparison.Ordinal))
{
elasticType = elasticType.Substring(0, elasticType.Length - 1);
}

return elasticType;
}

private string ParseAndFormatRequest(Activity activity, string debugInformation)
private string ParseAndFormatRequest(string debugInformation)
{
if (!this.options.ParseAndFormatRequest)
{
Expand All @@ -275,9 +275,10 @@ private string ParseAndFormatRequest(Activity activity, string debugInformation)
return debugInformation;
}

using (doc)
using (var stream = new MemoryStream())
{
var writer = new Utf8JsonWriter(stream, new JsonWriterOptions { Indented = true });
using var writer = new Utf8JsonWriter(stream, new JsonWriterOptions { Indented = true });
doc.WriteTo(writer);
writer.Flush();
body = Encoding.UTF8.GetString(stream.ToArray());
Expand Down

0 comments on commit 429fdbb

Please sign in to comment.