diff --git a/src/OpenTelemetry.Instrumentation.ElasticsearchClient/ElasticsearchClientInstrumentationOptions.cs b/src/OpenTelemetry.Instrumentation.ElasticsearchClient/ElasticsearchClientInstrumentationOptions.cs index 43d42bc6d0..6030bc4a80 100644 --- a/src/OpenTelemetry.Instrumentation.ElasticsearchClient/ElasticsearchClientInstrumentationOptions.cs +++ b/src/OpenTelemetry.Instrumentation.ElasticsearchClient/ElasticsearchClientInstrumentationOptions.cs @@ -32,7 +32,7 @@ public class ElasticsearchClientInstrumentationOptions /// /// 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. /// - public bool ParseAndFormatRequest { get; set; } = false; + public bool ParseAndFormatRequest { get; set; } /// /// Gets or sets a value indicating whether or not the OpenTelemetry.Instrumentation.ElasticsearchClient diff --git a/src/OpenTelemetry.Instrumentation.ElasticsearchClient/Implementation/ElasticsearchRequestPipelineDiagnosticListener.cs b/src/OpenTelemetry.Instrumentation.ElasticsearchClient/Implementation/ElasticsearchRequestPipelineDiagnosticListener.cs index c2c8414ff2..75a2803600 100644 --- a/src/OpenTelemetry.Instrumentation.ElasticsearchClient/Implementation/ElasticsearchRequestPipelineDiagnosticListener.cs +++ b/src/OpenTelemetry.Instrumentation.ElasticsearchClient/Implementation/ElasticsearchRequestPipelineDiagnosticListener.cs @@ -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) @@ -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); @@ -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) { @@ -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) @@ -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; } @@ -245,7 +245,7 @@ private string GetElasticIndex(Uri uri) return null; } - if (elasticType.EndsWith("/")) + if (elasticType.EndsWith("/", StringComparison.Ordinal)) { elasticType = elasticType.Substring(0, elasticType.Length - 1); } @@ -253,7 +253,7 @@ private string GetElasticIndex(Uri uri) return elasticType; } - private string ParseAndFormatRequest(Activity activity, string debugInformation) + private string ParseAndFormatRequest(string debugInformation) { if (!this.options.ParseAndFormatRequest) { @@ -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());