Skip to content

Commit

Permalink
Merge branch 'main' into loggerprovider-resource-iconfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
alanwest authored Nov 11, 2022
2 parents c8e162e + b5e50b2 commit ec419f6
Show file tree
Hide file tree
Showing 14 changed files with 175 additions and 109 deletions.
7 changes: 0 additions & 7 deletions .github/workflows/ci-md.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@ on:

jobs:
build-test:
strategy:
matrix:
os: [ windows-latest, ubuntu-latest ]
version: [ net462, net6.0, net7.0 ]
exclude:
- os: ubuntu-latest
version: net462

runs-on: ubuntu-latest
steps:
Expand Down
2 changes: 1 addition & 1 deletion docs/metrics/customizing-the-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ default boundaries. This requires the use of
new ExplicitBucketHistogramConfiguration { Boundaries = new double[] { 10, 20 } })

// If you provide an empty `double` array as `Boundaries` to the `ExplicitBucketHistogramConfiguration`,
// the SDK will only export the sum and count for the measurements.
// the SDK will only export the sum, count, min and max for the measurements.
// There are no buckets exported in this case.
.AddView(
instrumentName: "MyHistogram",
Expand Down
4 changes: 4 additions & 0 deletions src/OpenTelemetry.Instrumentation.Http/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

* **Breaking change** `http.host` will no longer be populated. `net.peer.name`
and `net.peer.port` attributes will be populated instead.
([#3832](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3832))

## 1.0.0-rc9.9

Released 2022-Nov-07
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,12 @@ public void OnStartActivity(Activity activity, object payload)

activity.SetTag(SemanticConventions.AttributeHttpScheme, request.RequestUri.Scheme);
activity.SetTag(SemanticConventions.AttributeHttpMethod, HttpTagHelper.GetNameForHttpMethod(request.Method));
activity.SetTag(SemanticConventions.AttributeHttpHost, HttpTagHelper.GetHostTagValueFromRequestUri(request.RequestUri));
activity.SetTag(SemanticConventions.AttributeNetPeerName, request.RequestUri.Host);
if (!request.RequestUri.IsDefaultPort)
{
activity.SetTag(SemanticConventions.AttributeNetPeerPort, request.RequestUri.Port);
}

activity.SetTag(SemanticConventions.AttributeHttpUrl, HttpTagHelper.GetUriTagValueFromRequestUri(request.RequestUri));
activity.SetTag(SemanticConventions.AttributeHttpFlavor, HttpTagHelper.GetFlavorTagValueFromProtocolVersion(request.Version));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ internal static class HttpTagHelper
private static readonly ConcurrentDictionary<string, string> MethodOperationNameCache = new();
private static readonly ConcurrentDictionary<HttpMethod, string> HttpMethodOperationNameCache = new();
private static readonly ConcurrentDictionary<HttpMethod, string> HttpMethodNameCache = new();
private static readonly ConcurrentDictionary<string, ConcurrentDictionary<int, string>> HostAndPortToStringCache = new();
private static readonly ConcurrentDictionary<Version, string> ProtocolVersionToStringCache = new();

private static readonly Func<string, string> ConvertMethodToOperationNameRef = ConvertMethodToOperationName;
Expand Down Expand Up @@ -63,37 +62,6 @@ internal static class HttpTagHelper
/// <returns>Span flavor value.</returns>
public static string GetFlavorTagValueFromProtocolVersion(Version protocolVersion) => ProtocolVersionToStringCache.GetOrAdd(protocolVersion, ConvertProtocolVersionToStringRef);

/// <summary>
/// Gets the OpenTelemetry standard host tag value for a span based on its request <see cref="Uri"/>.
/// </summary>
/// <param name="requestUri"><see cref="Uri"/>.</param>
/// <returns>Span host value.</returns>
public static string GetHostTagValueFromRequestUri(Uri requestUri)
{
string host = requestUri.Host;

if (requestUri.IsDefaultPort)
{
return host;
}

int port = requestUri.Port;

if (!HostAndPortToStringCache.TryGetValue(host, out ConcurrentDictionary<int, string> portCache))
{
portCache = new ConcurrentDictionary<int, string>();
HostAndPortToStringCache.TryAdd(host, portCache);
}

if (!portCache.TryGetValue(port, out string hostTagValue))
{
hostTagValue = $"{requestUri.Host}:{requestUri.Port}";
portCache.TryAdd(port, hostTagValue);
}

return hostTagValue;
}

/// <summary>
/// Gets the OpenTelemetry standard uri tag value for a span based on its request <see cref="Uri"/>.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,13 @@ private static void AddRequestTagsAndInstrumentRequest(HttpWebRequest request, A
if (activity.IsAllDataRequested)
{
activity.SetTag(SemanticConventions.AttributeHttpMethod, request.Method);
activity.SetTag(SemanticConventions.AttributeNetPeerName, request.RequestUri.Host);
if (!request.RequestUri.IsDefaultPort)
{
activity.SetTag(SemanticConventions.AttributeNetPeerPort, request.RequestUri.Port);
}

activity.SetTag(SemanticConventions.AttributeHttpScheme, request.RequestUri.Scheme);
activity.SetTag(SemanticConventions.AttributeHttpHost, HttpTagHelper.GetHostTagValueFromRequestUri(request.RequestUri));
activity.SetTag(SemanticConventions.AttributeHttpUrl, HttpTagHelper.GetUriTagValueFromRequestUri(request.RequestUri));
activity.SetTag(SemanticConventions.AttributeHttpFlavor, HttpTagHelper.GetFlavorTagValueFromProtocolVersion(request.ProtocolVersion));

Expand Down
112 changes: 81 additions & 31 deletions test/OpenTelemetry.Instrumentation.AspNetCore.Tests/BasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Moq;
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Instrumentation.AspNetCore.Implementation;
Expand Down Expand Up @@ -57,8 +59,10 @@ public void AddAspNetCoreInstrumentation_BadArgs()
Assert.Throws<ArgumentNullException>(() => builder.AddAspNetCoreInstrumentation());
}

[Fact]
public async Task StatusIsUnsetOn200Response()
[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task StatusIsUnsetOn200Response(bool disableLogging)
{
var exportedItems = new List<Activity>();
void ConfigureTestServices(IServiceCollection services)
Expand All @@ -72,7 +76,13 @@ void ConfigureTestServices(IServiceCollection services)
// Arrange
using (var client = this.factory
.WithWebHostBuilder(builder =>
builder.ConfigureTestServices(ConfigureTestServices))
{
builder.ConfigureTestServices(ConfigureTestServices);
if (disableLogging)
{
builder.ConfigureLogging(loggingBuilder => loggingBuilder.ClearProviders());
}
})
.CreateClient())
{
// Act
Expand Down Expand Up @@ -116,7 +126,10 @@ void ConfigureTestServices(IServiceCollection services)
// Arrange
using (var client = this.factory
.WithWebHostBuilder(builder =>
builder.ConfigureTestServices(ConfigureTestServices))
{
builder.ConfigureTestServices(ConfigureTestServices);
builder.ConfigureLogging(loggingBuilder => loggingBuilder.ClearProviders());
})
.CreateClient())
{
// Act
Expand Down Expand Up @@ -150,12 +163,17 @@ public async Task SuccessfulTemplateControllerCallUsesParentContext()
// Arrange
using (var testFactory = this.factory
.WithWebHostBuilder(builder =>
{
builder.ConfigureTestServices(services =>
{
this.tracerProvider = Sdk.CreateTracerProviderBuilder().AddAspNetCoreInstrumentation()
this.tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddAspNetCoreInstrumentation()
.AddInMemoryExporter(exportedItems)
.Build();
})))
});

builder.ConfigureLogging(loggingBuilder => loggingBuilder.ClearProviders());
}))
{
using var client = testFactory.CreateClient();
var request = new HttpRequestMessage(HttpMethod.Get, "/api/values/2");
Expand Down Expand Up @@ -203,14 +221,14 @@ public async Task CustomPropagator()
// Arrange
using (var testFactory = this.factory
.WithWebHostBuilder(builder =>
builder.ConfigureTestServices(services =>
{
Sdk.SetDefaultTextMapPropagator(propagator.Object);
this.tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddAspNetCoreInstrumentation()
.AddInMemoryExporter(exportedItems)
.Build();
})))
builder.ConfigureTestServices(services =>
{
Sdk.SetDefaultTextMapPropagator(propagator.Object);
this.tracerProvider = Sdk.CreateTracerProviderBuilder().AddAspNetCoreInstrumentation().AddInMemoryExporter(exportedItems).Build();
});
builder.ConfigureLogging(loggingBuilder => loggingBuilder.ClearProviders());
}))
{
using var client = testFactory.CreateClient();
var response = await client.GetAsync("/api/values/2");
Expand Down Expand Up @@ -256,7 +274,10 @@ void ConfigureTestServices(IServiceCollection services)
// Arrange
using (var testFactory = this.factory
.WithWebHostBuilder(builder =>
builder.ConfigureTestServices(ConfigureTestServices)))
{
builder.ConfigureTestServices(ConfigureTestServices);
builder.ConfigureLogging(loggingBuilder => loggingBuilder.ClearProviders());
}))
{
using var client = testFactory.CreateClient();

Expand Down Expand Up @@ -303,7 +324,10 @@ void ConfigureTestServices(IServiceCollection services)
// Arrange
using (var testFactory = this.factory
.WithWebHostBuilder(builder =>
builder.ConfigureTestServices(ConfigureTestServices)))
{
builder.ConfigureTestServices(ConfigureTestServices);
builder.ConfigureLogging(loggingBuilder => loggingBuilder.ClearProviders());
}))
{
using var client = testFactory.CreateClient();

Expand Down Expand Up @@ -347,13 +371,10 @@ public async Task ExtractContextIrrespectiveOfSamplingDecision(SamplingDecision
// Arrange
using var testFactory = this.factory
.WithWebHostBuilder(builder =>
builder.ConfigureTestServices(services =>
{
this.tracerProvider = Sdk.CreateTracerProviderBuilder()
.SetSampler(new TestSampler(samplingDecision))
.AddAspNetCoreInstrumentation()
.Build();
}));
builder.ConfigureTestServices(services => { this.tracerProvider = Sdk.CreateTracerProviderBuilder().SetSampler(new TestSampler(samplingDecision)).AddAspNetCoreInstrumentation().Build(); });
builder.ConfigureLogging(loggingBuilder => loggingBuilder.ClearProviders());
});
using var client = testFactory.CreateClient();

// Test TraceContext Propagation
Expand Down Expand Up @@ -404,6 +425,7 @@ public async Task ExtractContextIrrespectiveOfTheFilterApplied()
bool isFilterCalled = false;
using var testFactory = this.factory
.WithWebHostBuilder(builder =>
{
builder.ConfigureTestServices(services =>
{
this.tracerProvider = Sdk.CreateTracerProviderBuilder()
Expand All @@ -416,7 +438,9 @@ public async Task ExtractContextIrrespectiveOfTheFilterApplied()
};
})
.Build();
}));
});
builder.ConfigureLogging(loggingBuilder => loggingBuilder.ClearProviders());
});
using var client = testFactory.CreateClient();

// Test TraceContext Propagation
Expand Down Expand Up @@ -494,7 +518,10 @@ void ConfigureTestServices(IServiceCollection services)
// Arrange
using (var client = this.factory
.WithWebHostBuilder(builder =>
builder.ConfigureTestServices(ConfigureTestServices))
{
builder.ConfigureTestServices(ConfigureTestServices);
builder.ConfigureLogging(loggingBuilder => loggingBuilder.ClearProviders());
})
.CreateClient())
{
using var request = new HttpRequestMessage(HttpMethod.Get, "/api/values");
Expand Down Expand Up @@ -549,7 +576,10 @@ void ConfigureTestServices(IServiceCollection services)
// Arrange
using var client = this.factory
.WithWebHostBuilder(builder =>
builder.ConfigureTestServices(ConfigureTestServices))
{
builder.ConfigureTestServices(ConfigureTestServices);
builder.ConfigureLogging(loggingBuilder => loggingBuilder.ClearProviders());
})
.CreateClient();

// Act
Expand Down Expand Up @@ -582,7 +612,10 @@ void ConfigureTestServices(IServiceCollection services)
// Arrange
using (var client = this.factory
.WithWebHostBuilder(builder =>
builder.ConfigureTestServices(ConfigureTestServices))
{
builder.ConfigureTestServices(ConfigureTestServices);
builder.ConfigureLogging(loggingBuilder => loggingBuilder.ClearProviders());
})
.CreateClient())
{
var response = await client.GetAsync("/api/values/2");
Expand Down Expand Up @@ -618,13 +651,16 @@ public async Task ActivitiesStartedInMiddlewareBySettingHostActivityToNullShould
// Arrange
using (var client = this.factory
.WithWebHostBuilder(builder =>
{
builder.ConfigureTestServices((IServiceCollection services) =>
{
services.AddSingleton<ActivityMiddleware.ActivityMiddlewareImpl>(new TestNullHostActivityMiddlewareImpl(activitySourceName, activityName));
services.AddOpenTelemetryTracing((builder) => builder.AddAspNetCoreInstrumentation()
.AddSource(activitySourceName)
.AddInMemoryExporter(exportedItems));
}))
});
builder.ConfigureLogging(loggingBuilder => loggingBuilder.ClearProviders());
})
.CreateClient())
{
var response = await client.GetAsync("/api/values/2");
Expand Down Expand Up @@ -671,7 +707,10 @@ void ConfigureTestServices(IServiceCollection services)
// Arrange
using (var client = this.factory
.WithWebHostBuilder(builder =>
builder.ConfigureTestServices(ConfigureTestServices))
{
builder.ConfigureTestServices(ConfigureTestServices);
builder.ConfigureLogging(loggingBuilder => loggingBuilder.ClearProviders());
})
.CreateClient())
{
// Act
Expand Down Expand Up @@ -699,8 +738,12 @@ public async Task ShouldExportActivityWithOneOrMoreExceptionFilters(int mode)

// Arrange
using (var client = this.factory
.WithWebHostBuilder(builder => builder.ConfigureTestServices(
(s) => this.ConfigureExceptionFilters(s, mode, ref exportedItems)))
.WithWebHostBuilder(builder =>
{
builder.ConfigureTestServices(
(s) => this.ConfigureExceptionFilters(s, mode, ref exportedItems));
builder.ConfigureLogging(loggingBuilder => loggingBuilder.ClearProviders());
})
.CreateClient())
{
// Act
Expand Down Expand Up @@ -745,7 +788,10 @@ void ConfigureTestServices(IServiceCollection services)
// Arrange
using (var client = this.factory
.WithWebHostBuilder(builder =>
builder.ConfigureTestServices(ConfigureTestServices))
{
builder.ConfigureTestServices(ConfigureTestServices);
builder.ConfigureLogging(loggingBuilder => loggingBuilder.ClearProviders());
})
.CreateClient())
{
using var request = new HttpRequestMessage(HttpMethod.Get, "/api/values");
Expand Down Expand Up @@ -790,7 +836,10 @@ void ConfigureTestServices(IServiceCollection services)
// Arrange
using (var client = this.factory
.WithWebHostBuilder(builder =>
builder.ConfigureTestServices(ConfigureTestServices))
{
builder.ConfigureTestServices(ConfigureTestServices);
builder.ConfigureLogging(loggingBuilder => loggingBuilder.ClearProviders());
})
.CreateClient())
{
try
Expand Down Expand Up @@ -836,6 +885,7 @@ public async Task DiagnosticSourceExceptionCallBackIsNotReceivedForExceptionsHan
.Build();

var builder = WebApplication.CreateBuilder();
builder.Logging.ClearProviders();
var app = builder.Build();

app.UseExceptionHandler(handler =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Logging;
using OpenTelemetry.Trace;
using Xunit;

Expand All @@ -37,6 +38,7 @@ public InProcServerTests()
{
this.exportedItems = new List<Activity>();
var builder = WebApplication.CreateBuilder();
builder.Logging.ClearProviders();
var app = builder.Build();

this.tracerProvider = Sdk.CreateTracerProviderBuilder()
Expand Down
Loading

0 comments on commit ec419f6

Please sign in to comment.