Skip to content

Commit 99d0132

Browse files
committed
check content type in preparedata
1 parent 140503f commit 99d0132

File tree

4 files changed

+40
-59
lines changed

4 files changed

+40
-59
lines changed

src/Microsoft.Extensions.Configuration.AzureAppConfiguration/AzureAppConfigurationProvider.cs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using System.Linq;
1414
using System.Net;
1515
using System.Net.Http;
16+
using System.Net.Mime;
1617
using System.Net.Sockets;
1718
using System.Security;
1819
using System.Text;
@@ -600,6 +601,31 @@ private async Task<Dictionary<string, string>> PrepareData(Dictionary<string, Co
600601
foreach (KeyValuePair<string, ConfigurationSetting> kvp in data)
601602
{
602603
IEnumerable<KeyValuePair<string, string>> keyValuePairs = null;
604+
605+
if (_requestTracingEnabled && _requestTracingOptions != null)
606+
{
607+
ContentType contentType = null;
608+
609+
try
610+
{
611+
contentType = new ContentType(kvp.Value.ContentType.Trim());
612+
}
613+
catch (FormatException) { }
614+
615+
if (contentType != null &&
616+
contentType.Parameters.ContainsKey("profile") &&
617+
!string.IsNullOrEmpty(contentType.Parameters["profile"]) &&
618+
contentType.Parameters["profile"].StartsWith(RequestTracingConstants.AIContentTypeProfile))
619+
{
620+
_requestTracingOptions.HasAIProfile = true;
621+
622+
if (contentType.Parameters["profile"].StartsWith(RequestTracingConstants.AIChatCompletionContentTypeProfile))
623+
{
624+
_requestTracingOptions.HasAIChatCompletionProfile = true;
625+
}
626+
}
627+
}
628+
603629
keyValuePairs = await ProcessAdapters(kvp.Value, cancellationToken).ConfigureAwait(false);
604630

605631
foreach (KeyValuePair<string, string> kv in keyValuePairs)
@@ -1053,8 +1079,7 @@ private void SetRequestTracingOptions()
10531079
IsKeyVaultConfigured = _options.IsKeyVaultConfigured,
10541080
IsKeyVaultRefreshConfigured = _options.IsKeyVaultRefreshConfigured,
10551081
FeatureFlagTracing = _options.FeatureFlagTracing,
1056-
IsLoadBalancingEnabled = _options.LoadBalancingEnabled,
1057-
ContentTypeTracing = _options.ContentTypeTracing
1082+
IsLoadBalancingEnabled = _options.LoadBalancingEnabled
10581083
};
10591084
}
10601085

src/Microsoft.Extensions.Configuration.AzureAppConfiguration/ContentTypeTracing.cs

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/Microsoft.Extensions.Configuration.AzureAppConfiguration/JsonKeyValueAdapter.cs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,6 @@ internal class JsonKeyValueAdapter : IKeyValueAdapter
2222
KeyVaultConstants.ContentType
2323
};
2424

25-
private ContentTypeTracing _contentTypeTracing;
26-
27-
public JsonKeyValueAdapter(ContentTypeTracing contentTypeTracing)
28-
{
29-
_contentTypeTracing = contentTypeTracing;
30-
}
31-
3225
public Task<IEnumerable<KeyValuePair<string, string>>> ProcessKeyValue(ConfigurationSetting setting, Uri endpoint, Logger logger, CancellationToken cancellationToken)
3326
{
3427
if (setting == null)
@@ -73,31 +66,11 @@ public bool CanProcess(ConfigurationSetting setting)
7366
{
7467
ContentType contentType = new ContentType(setting.ContentType.Trim());
7568
mediaType = contentType.MediaType;
76-
77-
// Check for profile parameter in the content type
78-
if (_contentTypeTracing != null &&
79-
contentType.Parameters.ContainsKey("profile") &&
80-
!string.IsNullOrEmpty(contentType.Parameters["profile"]) &&
81-
contentType.Parameters["profile"].StartsWith(RequestTracingConstants.AIContentTypeProfile))
82-
{
83-
_contentTypeTracing.HasAIProfile = true;
84-
85-
if (contentType.Parameters["profile"].StartsWith(RequestTracingConstants.AIChatCompletionContentTypeProfile))
86-
{
87-
_contentTypeTracing.HasAIChatCompletionProfile = true;
88-
}
89-
}
9069
}
9170
catch (FormatException)
9271
{
9372
return false;
9473
}
95-
catch (IndexOutOfRangeException)
96-
{
97-
// Bug in System.Net.Mime.ContentType throws this if contentType is "xyz/"
98-
// https://github.com/dotnet/runtime/issues/39337
99-
return false;
100-
}
10174

10275
if (!ExcludedJsonContentTypes.Contains(mediaType, StringComparer.OrdinalIgnoreCase))
10376
{

src/Microsoft.Extensions.Configuration.AzureAppConfiguration/RequestTracingOptions.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,16 @@ internal class RequestTracingOptions
6969
public bool IsPushRefreshUsed { get; set; } = false;
7070

7171
/// <summary>
72-
/// Indicates certain content types used by the application.
72+
/// Flag to indicate whether any key-value uses the json content type and contains
73+
/// a parameter indicating an AI profile.
7374
/// </summary>
74-
public ContentTypeTracing ContentTypeTracing { get; set; } = new ContentTypeTracing();
75+
public bool HasAIProfile { get; set; } = false;
76+
77+
/// <summary>
78+
/// Flag to indicate whether any key-value uses the json content type and contains
79+
/// a parameter indicating an AI chat completion profile.
80+
/// </summary>
81+
public bool HasAIChatCompletionProfile { get; set; } = false;
7582

7683
/// <summary>
7784
/// Checks whether any tracing feature is used.
@@ -81,8 +88,8 @@ public bool UsesAnyTracingFeature()
8188
{
8289
return IsLoadBalancingEnabled ||
8390
IsSignalRUsed ||
84-
ContentTypeTracing.HasAIProfile ||
85-
ContentTypeTracing.HasAIChatCompletionProfile;
91+
HasAIProfile ||
92+
HasAIChatCompletionProfile;
8693
}
8794

8895
/// <summary>
@@ -113,7 +120,7 @@ public string CreateFeaturesString()
113120
sb.Append(RequestTracingConstants.SignalRUsedTag);
114121
}
115122

116-
if (ContentTypeTracing.HasAIProfile)
123+
if (HasAIProfile)
117124
{
118125
if (sb.Length > 0)
119126
{
@@ -123,7 +130,7 @@ public string CreateFeaturesString()
123130
sb.Append(RequestTracingConstants.AIContentTypeProfileTag);
124131
}
125132

126-
if (ContentTypeTracing.HasAIChatCompletionProfile)
133+
if (HasAIChatCompletionProfile)
127134
{
128135
if (sb.Length > 0)
129136
{

0 commit comments

Comments
 (0)