-
Notifications
You must be signed in to change notification settings - Fork 36
Add request tracing for content type #646
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
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
4a7d9dc
add initial content type pattern
amerjusupovic 8c59edb
format fix
amerjusupovic 8faa194
fix comment
amerjusupovic 54a6b1c
update to account for chat completion vs ai profiles
amerjusupovic 92377e3
in progress fix adapter to use existing requesttracingoptions
amerjusupovic dd1d538
use content type tracing to pass to requesttracingoptions
amerjusupovic 51ecabf
fix comments and naming
amerjusupovic a75e9ac
remove unneeded file
amerjusupovic 93a5259
add check for request tracing enabled
amerjusupovic 140503f
Merge branch 'main' of https://github.com/Azure/AppConfiguration-Dotn…
amerjusupovic 99d0132
check content type in preparedata
amerjusupovic 5d98e66
remove errors
amerjusupovic 0b11741
fix spacing
amerjusupovic 52942b7
fix test
amerjusupovic f036f0b
remove unused usings, add back catch for .net framework
amerjusupovic 4ee9199
fix parsing
amerjusupovic 5b06d28
rename constants
amerjusupovic 012256a
fix indent
amerjusupovic e2cfb46
update for PR comments
amerjusupovic f483ca7
PR comments, update if conditions
amerjusupovic ed90256
add isjson check
amerjusupovic 2e0a547
update isjson extension
amerjusupovic f70f91a
Merge branch 'main' into ajusupovic/contenttype-tracing
amerjusupovic 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
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
81 changes: 81 additions & 0 deletions
81
...rosoft.Extensions.Configuration.AzureAppConfiguration/Extensions/ContentTypeExtensions.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,81 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
// | ||
using System.Linq; | ||
using System; | ||
using System.Net.Mime; | ||
using Microsoft.Extensions.Configuration.AzureAppConfiguration.AzureKeyVault; | ||
using Microsoft.Extensions.Configuration.AzureAppConfiguration.FeatureManagement; | ||
using System.Collections.Generic; | ||
|
||
namespace Microsoft.Extensions.Configuration.AzureAppConfiguration.Extensions | ||
{ | ||
internal static class ContentTypeExtensions | ||
{ | ||
private static readonly IEnumerable<string> ExcludedJsonContentTypes = new[] | ||
{ | ||
FeatureManagementConstants.ContentType, | ||
KeyVaultConstants.ContentType | ||
}; | ||
|
||
public static bool IsAi(this ContentType contentType) | ||
{ | ||
return contentType != null && | ||
contentType.IsJson() && | ||
contentType.Parameters.ContainsKey("profile") && | ||
!string.IsNullOrEmpty(contentType.Parameters["profile"]) && | ||
contentType.Parameters["profile"].StartsWith(RequestTracingConstants.AIMimeProfile); | ||
} | ||
|
||
public static bool IsAiChatCompletion(this ContentType contentType) | ||
{ | ||
return contentType != null && | ||
contentType.IsJson() && | ||
contentType.Parameters.ContainsKey("profile") && | ||
!string.IsNullOrEmpty(contentType.Parameters["profile"]) && | ||
contentType.Parameters["profile"].StartsWith(RequestTracingConstants.AIChatCompletionMimeProfile); | ||
jimmyca15 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
public static bool IsJson(this ContentType contentType) | ||
{ | ||
if (contentType == null) | ||
{ | ||
return false; | ||
} | ||
|
||
string acceptedMainType = "application"; | ||
string acceptedSubType = "json"; | ||
string mediaType = contentType.MediaType; | ||
|
||
if (!ExcludedJsonContentTypes.Contains(mediaType, StringComparer.OrdinalIgnoreCase)) | ||
{ | ||
ReadOnlySpan<char> mediaTypeSpan = mediaType.AsSpan(); | ||
|
||
// Since contentType has been validated using System.Net.Mime.ContentType, | ||
// mediaType will always have exactly 2 parts after splitting on '/' | ||
int slashIndex = mediaTypeSpan.IndexOf('/'); | ||
|
||
if (mediaTypeSpan.Slice(0, slashIndex).Equals(acceptedMainType.AsSpan(), StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
ReadOnlySpan<char> subTypeSpan = mediaTypeSpan.Slice(slashIndex + 1); | ||
|
||
while (!subTypeSpan.IsEmpty) | ||
{ | ||
int plusIndex = subTypeSpan.IndexOf('+'); | ||
|
||
ReadOnlySpan<char> currentSubType = plusIndex == -1 ? subTypeSpan : subTypeSpan.Slice(0, plusIndex); | ||
|
||
if (currentSubType.Equals(acceptedSubType.AsSpan(), StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
return true; | ||
} | ||
|
||
subTypeSpan = plusIndex == -1 ? ReadOnlySpan<char>.Empty : subTypeSpan.Slice(plusIndex + 1); | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/Microsoft.Extensions.Configuration.AzureAppConfiguration/Extensions/StringExtensions.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
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
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.