Skip to content

Commit

Permalink
[Instrumentation.AspNet] Fix analysis warnings
Browse files Browse the repository at this point in the history
Fix/suppress code analysis warnings in OpenTelemetry.Instrumentation.AspNet.
Contributes to #950.
  • Loading branch information
martincostello committed Feb 3, 2023
1 parent d835d5d commit 12ffe63
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System.Diagnostics;
using System.Reflection;
using System.Web;
using OpenTelemetry.Internal;

namespace OpenTelemetry.Instrumentation.AspNet;

Expand Down Expand Up @@ -57,6 +58,8 @@ public void Dispose()
/// <inheritdoc />
public void Init(HttpApplication context)
{
Guard.ThrowIfNull(context);

context.BeginRequest += this.Application_BeginRequest;
context.EndRequest += this.Application_EndRequest;
context.Error += this.Application_Error;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ public static MeterProviderBuilder AddAspNetInstrumentation(
{
Guard.ThrowIfNull(builder);

#pragma warning disable CA2000 // Dispose objects before losing scope
var instrumentation = new AspNetMetrics();
#pragma warning restore CA2000 // Dispose objects before losing scope
builder.AddMeter(AspNetMetrics.InstrumentationName);
return builder.AddInstrumentation(() => instrumentation);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,9 @@ public void Can_Create_RootActivity_And_Saved_In_HttContext()
}

[Fact]
#pragma warning disable CA1030 // Use events where appropriate
public void Fire_Exception_Events()
#pragma warning restore CA1030 // Use events where appropriate
{
int callbacksFired = 0;

Expand Down Expand Up @@ -529,13 +531,11 @@ public TestHttpContext(Exception error = null)

private class NoopTextMapPropagator : TextMapPropagator
{
private static readonly PropagationContext DefaultPropagationContext = default;

public override ISet<string> Fields => null;

public override PropagationContext Extract<T>(PropagationContext context, T carrier, Func<T, string, IEnumerable<string>> getter)
{
return DefaultPropagationContext;
return default;
}

public override void Inject<T>(PropagationContext context, T carrier, Action<T, string, string> setter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ public override string[][] GetUnknownRequestHeaders()

public override string GetUnknownRequestHeader(string name)
{
if (this.headers.ContainsKey(name))
if (this.headers.TryGetValue(name, out var value))
{
return this.headers[name];
return value;
}

return base.GetUnknownRequestHeader(name);
Expand All @@ -91,9 +91,9 @@ public override string GetKnownRequestHeader(int index)
{
var name = GetKnownRequestHeaderName(index);

if (this.headers.ContainsKey(name))
if (this.headers.TryGetValue(name, out var value))
{
return this.headers[name];
return value;
}

return base.GetKnownRequestHeader(index);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,16 +391,17 @@ private XDocument ApplyTransformation(string originalConfiguration, string trans
var document = new XmlTransformableDocument();
using var transformation = new XmlTransformation(stream, null);
stream = null;
#pragma warning disable CA3075 // Insecure DTD processing in XML
document.LoadXml(originalConfiguration);
#pragma warning restore CA3075 // Insecure DTD processing in XML
transformation.Apply(document);
result = XDocument.Parse(document.OuterXml);
}
finally
{
if (stream != null)
{
stream.Dispose();
}
#pragma warning disable CA1508 // Avoid dead conditional code
stream?.Dispose();
#pragma warning restore CA1508 // Avoid dead conditional code
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,16 +421,17 @@ private XDocument ApplyTransformation(string originalConfiguration, string trans
var document = new XmlTransformableDocument();
using var transformation = new XmlTransformation(stream, null);
stream = null;
#pragma warning disable CA3075 // Insecure DTD processing in XML
document.LoadXml(originalConfiguration);
#pragma warning restore CA3075 // Insecure DTD processing in XML
transformation.Apply(document);
result = XDocument.Parse(document.OuterXml);
}
finally
{
if (stream != null)
{
stream.Dispose();
}
#pragma warning disable CA1508 // Avoid dead conditional code
stream?.Dispose();
#pragma warning restore CA1508 // Avoid dead conditional code
}

return result;
Expand Down

0 comments on commit 12ffe63

Please sign in to comment.