-
Notifications
You must be signed in to change notification settings - Fork 33
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
Populate feature flag telemetry metadata when feature flag telemetry is enabled #517
Changes from all commits
429ec6e
8ce6409
e068121
9f5f126
8ed359d
1a95a53
5e36fe3
74553d8
d208f2e
35e2741
07f7c7f
c166081
d916ad4
e7ba889
c068492
5815b5c
790e412
a47f147
80178fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
// | ||
using System.Text; | ||
using System; | ||
|
||
namespace Microsoft.Extensions.Configuration.AzureAppConfiguration.Extensions | ||
{ | ||
internal static class BytesExtensions | ||
{ | ||
/// <summary> | ||
/// Converts a byte array to Base64URL string with optional padding ('=') characters removed. | ||
/// Base64 description: https://datatracker.ietf.org/doc/html/rfc4648.html#section-4 | ||
/// </summary> | ||
public static string ToBase64Url(this byte[] bytes) | ||
{ | ||
string bytesBase64 = Convert.ToBase64String(bytes); | ||
|
||
int indexOfEquals = bytesBase64.IndexOf("="); | ||
|
||
// Remove all instances of "=" at the end of the string that were added as padding | ||
int stringBuilderCapacity = indexOfEquals != -1 ? indexOfEquals : bytesBase64.Length; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code trims everything after the first occurrence of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Base64 doesn't use equals as any value other than padding at the end, so it should work effectively the same. I had it as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see why TrimEnd would be used, it's unnecessary allocation. Otherwise it would just be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Implementation is fine. The comment can be updated to clarify that we choose to skip the padding (if any) based on the spec. |
||
|
||
StringBuilder stringBuilder = new StringBuilder(stringBuilderCapacity); | ||
|
||
// Construct Base64URL string by replacing characters in Base64 conversion that are not URL safe | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason the featureFlagId needs to be URL safe? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For use in REST resource IDs (/experiment/{id}) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, sounds like we should have a comment in CalculateFeatureFlagId to explain why we need base64url encoding. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That seems more like a design doc/specification territory rather than a code comment. The code just implements the spec of a feature flag id, which uses b64url encoding. |
||
for (int i = 0; i < stringBuilderCapacity; i++) | ||
{ | ||
if (bytesBase64[i] == '+') | ||
{ | ||
stringBuilder.Append('-'); | ||
} | ||
else if (bytesBase64[i] == '/') | ||
{ | ||
stringBuilder.Append('_'); | ||
} | ||
else | ||
{ | ||
stringBuilder.Append(bytesBase64[i]); | ||
} | ||
} | ||
|
||
return stringBuilder.ToString(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,12 @@ | |
// Licensed under the MIT license. | ||
// | ||
using Azure.Data.AppConfiguration; | ||
using Microsoft.Extensions.Configuration.AzureAppConfiguration.Extensions; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
using System.Text.Json; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
@@ -20,7 +23,7 @@ public FeatureManagementKeyValueAdapter(FeatureFilterTracing featureFilterTracin | |
_featureFilterTracing = featureFilterTracing ?? throw new ArgumentNullException(nameof(featureFilterTracing)); | ||
} | ||
|
||
public Task<IEnumerable<KeyValuePair<string, string>>> ProcessKeyValue(ConfigurationSetting setting, Logger logger, CancellationToken cancellationToken) | ||
public Task<IEnumerable<KeyValuePair<string, string>>> ProcessKeyValue(ConfigurationSetting setting, Uri endpoint, Logger logger, CancellationToken cancellationToken) | ||
{ | ||
FeatureFlag featureFlag; | ||
try | ||
|
@@ -197,15 +200,28 @@ public FeatureManagementKeyValueAdapter(FeatureFilterTracing featureFilterTracin | |
|
||
if (telemetry.Enabled) | ||
{ | ||
keyValues.Add(new KeyValuePair<string, string>($"{telemetryPath}:{FeatureManagementConstants.Enabled}", telemetry.Enabled.ToString())); | ||
} | ||
if (telemetry.Metadata != null) | ||
{ | ||
foreach (KeyValuePair<string, string> kvp in telemetry.Metadata) | ||
{ | ||
keyValues.Add(new KeyValuePair<string, string>($"{telemetryPath}:{FeatureManagementConstants.Metadata}:{kvp.Key}", kvp.Value)); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add some comments to explain how the featureFlagId is supposed to be calculated? It's not clear why we are doing some of the operations we are doing here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would also add clarity if we move the b64url into an extension method. ToBase64UrlString(this bytes[]); At that point the code will be more self explanatory. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, won't mind if we move the whole featureFlagId calculation to a separate method. This function is getting way too long. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I'll go with the extension for now since it makes sense to me conceptually, but let me know if you have any other concerns There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will the extension method be reusable somewhere? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just want to point out that the featureFlagId calculation is "by convention". It's not persisted anywhere. Portal and all our other providers will do the same calculation on-fly. Since the .NET provider is the first one doing this, it will make everyone's life easier if we can encapsulate it in one function with detailed comments so others can review/reference. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @amerjusupovic Can we please go with b64url extension method and private static method for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sounds good to me There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @zhenlan do you think the extension method is unnecessary? I thought it made sense to be an extension just because it's a known conversion, but it's true that we're only using it here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would recommend keeping that extension method. b64url is a spec and encapsulates well as utility method. Similar to frameworks |
||
|
||
if (telemetry.Metadata != null) | ||
{ | ||
foreach (KeyValuePair<string, string> kvp in telemetry.Metadata) | ||
string featureFlagId = CalculateFeatureFlagId(setting.Key, setting.Label); | ||
|
||
keyValues.Add(new KeyValuePair<string, string>($"{telemetryPath}:{FeatureManagementConstants.Metadata}:{FeatureManagementConstants.FeatureFlagId}", featureFlagId)); | ||
|
||
if (endpoint != null) | ||
{ | ||
keyValues.Add(new KeyValuePair<string, string>($"{telemetryPath}:{FeatureManagementConstants.Metadata}:{kvp.Key}", kvp.Value)); | ||
string featureFlagReference = $"{endpoint.AbsoluteUri}kv/{setting.Key}{(!string.IsNullOrWhiteSpace(setting.Label) ? $"?label={setting.Label}" : "")}"; | ||
|
||
keyValues.Add(new KeyValuePair<string, string>($"{telemetryPath}:{FeatureManagementConstants.Metadata}:{FeatureManagementConstants.FeatureFlagReference}", featureFlagReference)); | ||
} | ||
|
||
keyValues.Add(new KeyValuePair<string, string>($"{telemetryPath}:{FeatureManagementConstants.Metadata}:{FeatureManagementConstants.ETag}", setting.ETag.ToString())); | ||
|
||
keyValues.Add(new KeyValuePair<string, string>($"{telemetryPath}:{FeatureManagementConstants.Enabled}", telemetry.Enabled.ToString())); | ||
} | ||
} | ||
|
||
|
@@ -229,5 +245,19 @@ public bool NeedsRefresh() | |
{ | ||
return false; | ||
} | ||
|
||
private static string CalculateFeatureFlagId(string key, string label) | ||
{ | ||
byte[] featureFlagIdHash; | ||
|
||
// Convert the value consisting of key, newline character, and label to a byte array using UTF8 encoding to hash it using SHA 256 | ||
using (HashAlgorithm hashAlgorithm = SHA256.Create()) | ||
{ | ||
featureFlagIdHash = hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes($"{key}\n{(string.IsNullOrWhiteSpace(label) ? null : label)}")); | ||
} | ||
|
||
// Convert the hashed byte array to Base64Url | ||
return featureFlagIdHash.ToBase64Url(); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you put a summary that mentions it converts byte array to b64 URL. Using the trim trailing
=
characters strategy and also link to b64 spec.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The link to the spec: https://datatracker.ietf.org/doc/html/rfc4648#section-5