-
Notifications
You must be signed in to change notification settings - Fork 165
[APMSVLS-442] Adding azure frontdoor support #8861
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TophrC-dd
wants to merge
13
commits into
master
Choose a base branch
from
TopherC-dd/Azure-frontdoor
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+299
−13
Open
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d28580b
Adding azure frontdoor support
TophrC-dd 8443b29
fixing linter issues; code issues
TophrC-dd 822f391
clean up and bug fixes
TophrC-dd 61f77ec
fixing tests
TophrC-dd dee2b01
fixing issues
TophrC-dd be93909
linting
TophrC-dd ce0d27b
linting
TophrC-dd d503057
hard coding a timestamp for stable testing
TophrC-dd 9696d6a
test linting
TophrC-dd 1d4246f
fixing tests
TophrC-dd 025d9ec
fixing comments
TophrC-dd 9ebdc48
fixing missing slash in resource path
TophrC-dd dd648ca
fixing bug where CreateScope can overwrite the root span; test case
TophrC-dd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Proxy/AzureFrontDoorExtractor.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| // <copyright file="AzureFrontDoorExtractor.cs" company="Datadog"> | ||
| // Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License. | ||
| // This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc. | ||
| // </copyright> | ||
|
|
||
| #nullable enable | ||
|
|
||
| using System; | ||
| using Datadog.Trace.Logging; | ||
| using Datadog.Trace.Propagators; | ||
| using Datadog.Trace.Vendors.Serilog.Events; | ||
|
|
||
| namespace Datadog.Trace.ClrProfiler.AutoInstrumentation.Proxy; | ||
|
|
||
| /// <summary> | ||
| /// Extracts proxy metadata from Azure API Management headers. | ||
| /// </summary> | ||
| internal sealed class AzureFrontDoorExtractor : IInferredProxyExtractor | ||
| { | ||
| private static readonly IDatadogLogger Log = DatadogLogging.GetLoggerFor<AzureFrontDoorExtractor>(); | ||
|
|
||
| public bool TryExtract<TCarrier, TCarrierGetter>(TCarrier carrier, TCarrierGetter carrierGetter, out InferredProxyData data) | ||
| where TCarrierGetter : struct, ICarrierGetter<TCarrier> | ||
| { | ||
| data = default; | ||
|
|
||
| try | ||
| { | ||
|
|
||
| var startTimeHeaderValue = InferredProxyHeaders.StartTime ? | ||
|
TophrC-dd marked this conversation as resolved.
Outdated
|
||
| ParseUtility.ParseString(carrier, carrierGetter, InferredProxyHeaders.StartTime ) : DateTimeOffset.FromUnixTimeMilliseconds().ToString(); | ||
|
|
||
| // we also need to validate that we have the start time header otherwise we won't be able to create the span | ||
| if (!InferredProxySpanHelper.GetStartTime(startTimeHeaderValue, out var startTime)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| // the remaining headers aren't necessarily required | ||
| var domainName = ParseUtility.ParseString(carrier, carrierGetter, InferredProxyHeaders.Domain); | ||
| var httpMethod = ParseUtility.ParseString(carrier, carrierGetter, InferredProxyHeaders.HttpMethod); | ||
| var path = ParseUtility.ParseString(carrier, carrierGetter, InferredProxyHeaders.Path); | ||
| var region = ParseUtility.ParseString(carrier, carrierGetter, InferredProxyHeaders.Region); | ||
| data = new InferredProxyData(InferredProxySpanHelper.AzureProxyHeaderValue, startTime, domainName, httpMethod, path, null, region); | ||
|
|
||
| if (Log.IsEnabled(LogEventLevel.Debug)) | ||
| { | ||
| Log.Debug( | ||
| "Successfully extracted proxy data: StartTime={StartTime}, Domain={Domain}, Method={Method}, Path={Path}, Region={Region}", | ||
| [startTimeHeaderValue, domainName, httpMethod, path, region]); | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| Log.Error(ex, "Error extracting proxy data from {Proxy} headers", InferredProxySpanHelper.AzureProxyHeaderValue); | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
52 changes: 52 additions & 0 deletions
52
tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Proxy/AzureFrontDoorSpanFactory.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| // <copyright file="AzureFrontDoorSpanFactory.cs" company="Datadog"> | ||
| // Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License. | ||
| // This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc. | ||
| // </copyright> | ||
|
|
||
| #nullable enable | ||
|
|
||
| using System; | ||
| using Datadog.Trace.ClrProfiler.AutoInstrumentation.Azure.Shared; | ||
| using Datadog.Trace.Logging; | ||
| using Datadog.Trace.Tagging; | ||
| using Datadog.Trace.Util; | ||
|
|
||
| namespace Datadog.Trace.ClrProfiler.AutoInstrumentation.Proxy; | ||
|
|
||
| /// <summary> | ||
| /// Creates spans representing requests handled by Azure API Management. | ||
| /// </summary> | ||
| internal sealed class AzureFrontDoorSpanFactory : IInferredSpanFactory | ||
| { | ||
| private const string OperationName = AzureFunctionsConstants.AzureFrontDoorName; | ||
| private static readonly IDatadogLogger Log = DatadogLogging.GetLoggerFor<AzureFrontDoorSpanFactory>(); | ||
|
|
||
| public Scope? CreateSpan(Tracer tracer, InferredProxyData data, ISpanContext? parent = null) | ||
| { | ||
| try | ||
| { | ||
| var resourceUrl = data.Path is null ? string.Empty : UriHelpers.GetCleanUriPath(data.Path).ToLowerInvariant(); | ||
|
|
||
| var tags = new InferredProxyTags | ||
| { | ||
| HttpMethod = data.HttpMethod, | ||
| InstrumentationName = data.ProxyName, | ||
| HttpUrl = $"{data.DomainName}{data.Path}", | ||
| HttpRoute = resourceUrl, | ||
| InferredSpan = 1, | ||
| Region = data.Region, | ||
| }; | ||
|
|
||
| var scope = tracer.StartActiveInternal(operationName: OperationName, parent: parent, startTime: data.StartTime, tags: tags, serviceName: data.DomainName, serviceNameSource: "azure-frontdoor"); | ||
| scope.Span.ResourceName = data.HttpMethod is null ? resourceUrl : $"{data.HttpMethod} {resourceUrl}"; | ||
| scope.Span.Type = SpanTypes.Web; | ||
|
|
||
| return scope; | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| Log.Error(ex, "Error creating Azure API Management span"); | ||
| return null; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
...Trace.ClrProfiler.Managed.Tests/AutoInstrumentation/Proxy/AzureFrontDoorExtractorTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| // <copyright file="AzureFrontDoorExtractorTests.cs" company="Datadog"> | ||
| // Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License. | ||
| // This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc. | ||
| // </copyright> | ||
|
|
||
| using System; | ||
| using System.Globalization; | ||
| using Datadog.Trace.ClrProfiler.AutoInstrumentation.Proxy; | ||
| using Datadog.Trace.Headers; | ||
| using FluentAssertions; | ||
| using Xunit; | ||
|
|
||
| namespace Datadog.Trace.ClrProfiler.Managed.Tests.AutoInstrumentation.Proxy; | ||
|
|
||
| public class AzureFrontDoorExtractorTests | ||
| { | ||
| private readonly AzureApiManagementExtractor _extractor; | ||
|
|
||
| public AzureApiManagementExtractorTests() | ||
| { | ||
| _extractor = new AzureApiManagementExtractor(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TryExtract_WithAllValidHeaders_ReturnsTrue() | ||
| { | ||
| // this reduces precision to 1ms, so we can't compare extracted value to the original DateTimeOffset directly | ||
| var unixTimeMilliseconds = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); | ||
| var start = DateTimeOffset.FromUnixTimeMilliseconds(unixTimeMilliseconds); | ||
|
|
||
| var headers = ProxyTestHelpers.CreateValidAzureFrontDoorHeaders(unixTimeMilliseconds.ToString()); | ||
|
|
||
| var success = _extractor.TryExtract(headers, headers.GetAccessor(), out var data); | ||
|
|
||
| success.Should().BeTrue(); | ||
| data.ProxyName.Should().Be("azure-frontdoor"); | ||
| data.StartTime.Should().Be(start); | ||
| data.DomainName.Should().BeNull(); // Azure doesn't use domain | ||
| data.HttpMethod.Should().Be("POST"); | ||
| data.Path.Should().Be("/api/v1/users"); | ||
| data.Stage.Should().Be("prod"); | ||
| data.Region.Should().Be("canada central"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TryExtract_WithMinimumValidHeaders_ReturnsTrue() | ||
| { | ||
| // this reduces precision to 1ms, so we can't compare extracted value to the original DateTimeOffset directly | ||
| var unixTimeMilliseconds = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); | ||
| var start = DateTimeOffset.FromUnixTimeMilliseconds(unixTimeMilliseconds); | ||
|
|
||
| var headers = ProxyTestHelpers.CreateValidAzureFrontDoorHeaders(unixTimeMilliseconds.ToString()); | ||
| headers.Remove(InferredProxyHeaders.HttpMethod); | ||
| headers.Remove(InferredProxyHeaders.Path); | ||
| headers.Remove(InferredProxyHeaders.Region); | ||
|
|
||
| var success = _extractor.TryExtract(headers, headers.GetAccessor(), out var data); | ||
|
|
||
| success.Should().BeTrue(); | ||
| data.ProxyName.Should().Be("azure-frontdoor"); | ||
| data.StartTime.Should().Be(start); | ||
| data.DomainName.Should().BeNull(); | ||
| data.HttpMethod.Should().BeNull(); | ||
| data.Path.Should().BeNull(); | ||
| data.Stage.Should().BeNull(); | ||
| data.Region.Should().BeNull(); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(null)] | ||
| [InlineData("")] | ||
| [InlineData("invalid")] | ||
| [InlineData("1111111122222222333333334444444455555555666666667777777788888888")] // too large | ||
| public void TryExtract_WithInvalidStartTime_ReturnsFalse(string startTime) | ||
| { | ||
| var headers = ProxyTestHelpers.CreateValidAzureFrontDoorHeaders(); | ||
| headers.Set(InferredProxyHeaders.StartTime, startTime); | ||
|
|
||
| var success = _extractor.TryExtract(headers, headers.GetAccessor(), out var data); | ||
|
|
||
| success.Should().BeFalse(); | ||
| data.Should().Be(default(InferredProxyData)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TryExtract_WithMissingStartTime_ReturnsFalse() | ||
| { | ||
| var headers = ProxyTestHelpers.CreateValidAzureHeaders(); | ||
| headers.Remove(InferredProxyHeaders.StartTime); | ||
|
|
||
| var success = _extractor.TryExtract(headers, headers.GetAccessor(), out var data); | ||
|
|
||
| success.Should().BeFalse(); | ||
| data.Should().Be(default(InferredProxyData)); | ||
| } | ||
| } |
44 changes: 44 additions & 0 deletions
44
...ace.ClrProfiler.Managed.Tests/AutoInstrumentation/Proxy/AzureFrontDoorSpanFactoryTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| // <copyright file="AzureFrontDoorSpanFactoryTests.cs" company="Datadog"> | ||
| // Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License. | ||
| // This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc. | ||
| // </copyright> | ||
|
|
||
| #nullable enable | ||
|
|
||
| using System; | ||
| using System.Threading.Tasks; | ||
| using Datadog.Trace.ClrProfiler.AutoInstrumentation.Proxy; | ||
| using Datadog.Trace.TestHelpers.TestTracer; | ||
| using FluentAssertions; | ||
| using Xunit; | ||
|
|
||
| namespace Datadog.Trace.ClrProfiler.Managed.Tests.AutoInstrumentation.Proxy; | ||
|
|
||
| public class AzureFrontDoorSpanFactoryTests | ||
| { | ||
| [Fact] | ||
| public async Task CreateSpan_CreatesSpanWithCorrectProperties() | ||
| { | ||
| var factory = new AzureFrontDoorSpanFactory(); | ||
| await using var tracer = ProxyTestHelpers.GetMockTracer(); | ||
| var startTime = DateTimeOffset.UtcNow; | ||
| var data = new InferredProxyData("azure-frontdoor", startTime, "myapp.azurefd.net", "GET", "/api/v1/users", null, null); | ||
|
|
||
| var scope = factory.CreateSpan(tracer, data); | ||
|
|
||
| scope.Should().NotBeNull(); | ||
| var span = scope!.Span; | ||
| span.Should().NotBeNull(); | ||
| span.OperationName.Should().Be("azure.frontdoor"); | ||
| span.ResourceName.Should().Be("GET /api/v1/users"); // TODO obfuscation and quantization | ||
| span.Type.Should().Be("web"); | ||
| span.StartTime.Should().Be(startTime); | ||
|
|
||
| var tags = scope.Span.Tags; | ||
| tags.Should().NotBeNull(); | ||
| tags.GetTag(Tags.HttpMethod).Should().Be("GET"); | ||
| tags.GetTag(Tags.InstrumentationName).Should().Be("azure-frontdoor"); | ||
| tags.GetTag(Tags.HttpUrl).Should().Be("myapp.azurefd.net/api/v1/users"); | ||
| tags.GetTag(Tags.HttpRoute).Should().Be("/api/v1/users"); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.