-
Notifications
You must be signed in to change notification settings - Fork 166
[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
16
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.
+499
−22
Open
Changes from all commits
Commits
Show all changes
16 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 8f4bfbf
addressing issues
TophrC-dd 42753a2
addressing comments
TophrC-dd 0b9cd47
Merge branch 'master' into TopherC-dd/Azure-frontdoor
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
Some comments aren't visible on the classic Files Changed page.
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
64 changes: 64 additions & 0 deletions
64
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,64 @@ | ||
| // <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 Frontdoor 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 = ParseUtility.ParseString(carrier, carrierGetter, InferredProxyHeaders.StartTime); | ||
| if (StringUtil.IsNullOrEmpty(startTimeHeaderValue)) | ||
| { | ||
| startTimeHeaderValue = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString(); | ||
| } | ||
|
|
||
| // validate the start time is a parseable Unix timestamp in milliseconds | ||
| 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); | ||
| var stage = ParseUtility.ParseString(carrier, carrierGetter, InferredProxyHeaders.Stage); | ||
| data = new InferredProxyData(InferredProxySpanHelper.AzureFrontDoorHeaderValue, startTime, domainName, httpMethod, path, stage, 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.AzureFrontDoorHeaderValue); | ||
| return false; | ||
| } | ||
| } | ||
| } |
57 changes: 57 additions & 0 deletions
57
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,57 @@ | ||
| // <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 Frontdoor. | ||
| /// </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 | ||
| { | ||
| // Azure Front Door currently sends the path relative, without a leading slash (e.g. "api/foo"). | ||
| // Trim any leading slash defensively before prepending our own, so the route/url stay | ||
| // single-slashed ("/api/foo") even if Front Door starts sending an absolute path later. | ||
| var normalizedPath = data.Path?.TrimStart('/'); | ||
| var resourceUrl = normalizedPath is null ? string.Empty : UriHelpers.GetCleanUriPath($"/{normalizedPath}").ToLowerInvariant(); | ||
|
|
||
| var tags = new InferredProxyTags | ||
| { | ||
| HttpMethod = data.HttpMethod, | ||
| InstrumentationName = data.ProxyName, | ||
| HttpUrl = $"{data.DomainName}/{normalizedPath}", | ||
| HttpRoute = resourceUrl, | ||
| InferredSpan = 1, | ||
| Region = data.Region, | ||
| Stage = data.Stage, | ||
| }; | ||
|
|
||
| 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 Frontdoor 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
137 changes: 137 additions & 0 deletions
137
...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,137 @@ | ||
| // <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 AzureFrontDoorExtractor _extractor; | ||
|
|
||
| public AzureFrontDoorExtractorTests() | ||
| { | ||
| _extractor = new AzureFrontDoorExtractor(); | ||
| } | ||
|
|
||
| [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-fd"); | ||
| data.StartTime.Should().Be(start); | ||
| data.DomainName.Should().Be("myapp.azurefd.net"); | ||
| data.HttpMethod.Should().Be("GET"); | ||
| data.Path.Should().Be("/api/test"); | ||
| data.Stage.Should().Be("prod"); | ||
| data.Region.Should().Be("canada central"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TryExtract_WithMinimumValidHeaders_ReturnsTrue() | ||
| { | ||
| var headers = ProxyTestHelpers.CreateValidAzureFrontDoorHeaders(); | ||
| headers.Remove(InferredProxyHeaders.HttpMethod); | ||
| headers.Remove(InferredProxyHeaders.Path); | ||
| headers.Remove(InferredProxyHeaders.Region); | ||
| headers.Remove(InferredProxyHeaders.Stage); | ||
|
|
||
| var success = _extractor.TryExtract(headers, headers.GetAccessor(), out var data); | ||
|
|
||
| success.Should().BeTrue(); | ||
| data.ProxyName.Should().Be("azure-fd"); | ||
| data.DomainName.Should().Be("myapp.azurefd.net"); | ||
| data.HttpMethod.Should().BeNull(); | ||
| data.Path.Should().BeNull(); | ||
| data.Stage.Should().BeNull(); | ||
| data.Region.Should().BeNull(); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("invalid")] | ||
| [InlineData("not-a-number")] | ||
| [InlineData("1111111122222222333333334444444455555555666666667777777788888888")] // too large | ||
| public void TryExtract_WithInvalidStartTime_ReturnsFalseAndDefaultData(string startTime) | ||
| { | ||
| // A non-empty but unparseable start time is not synthesized away; extraction must fail | ||
| // and leave `data` untouched (default) so no partial/garbage span is created. | ||
| 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_ReturnsTrue() | ||
| { | ||
| var headers = ProxyTestHelpers.CreateValidAzureFrontDoorHeaders(); | ||
| headers.Remove(InferredProxyHeaders.StartTime); | ||
|
|
||
| var success = _extractor.TryExtract(headers, headers.GetAccessor(), out var data); | ||
|
|
||
| success.Should().BeTrue(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TryExtract_WithEmptyStartTime_SynthesizesStartTimeAndReturnsTrue() | ||
| { | ||
| // Unlike APIM, Front Door does not emit a start-time header, so a missing/empty value is | ||
| // expected and must be synthesized from "now" rather than causing extraction to fail. | ||
| var before = DateTimeOffset.UtcNow.AddSeconds(-5); | ||
| var headers = ProxyTestHelpers.CreateValidAzureFrontDoorHeaders(); | ||
| headers.Set(InferredProxyHeaders.StartTime, string.Empty); | ||
|
|
||
| var success = _extractor.TryExtract(headers, headers.GetAccessor(), out var data); | ||
| var after = DateTimeOffset.UtcNow.AddSeconds(5); | ||
|
|
||
| success.Should().BeTrue(); | ||
| data.StartTime.Should().NotBe(default); | ||
| data.StartTime.Should().BeOnOrAfter(before).And.BeOnOrBefore(after); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TryExtract_WithLowerCaseHttpMethod_NormalizesToUpperCase() | ||
| { | ||
| // The HTTP method must be normalized so downstream resource names / tags are stable | ||
| // regardless of the casing the proxy happens to send. | ||
| var headers = ProxyTestHelpers.CreateValidAzureFrontDoorHeaders(); | ||
| headers.Set(InferredProxyHeaders.HttpMethod, "post"); | ||
|
|
||
| var success = _extractor.TryExtract(headers, headers.GetAccessor(), out var data); | ||
|
|
||
| success.Should().BeTrue(); | ||
| data.HttpMethod.Should().Be("POST"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TryExtract_WithMissingDomain_ReturnsTrueWithNullDomain() | ||
| { | ||
| // Domain is optional; its absence must not fail extraction, and the field should stay null | ||
| // (not empty string) so the factory can distinguish "not provided". | ||
| var headers = ProxyTestHelpers.CreateValidAzureFrontDoorHeaders(); | ||
| headers.Remove(InferredProxyHeaders.Domain); | ||
|
|
||
| var success = _extractor.TryExtract(headers, headers.GetAccessor(), out var data); | ||
|
|
||
| success.Should().BeTrue(); | ||
| data.DomainName.Should().BeNull(); | ||
| } | ||
| } |
Oops, something went wrong.
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.