Skip to content
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

Merged
merged 19 commits into from
Feb 14, 2024

Conversation

amerjusupovic
Copy link
Member

Adding 3 values to feature flag telemetry metadata when a flag has telemetry enabled: ETag, FeatureFlagId, and FeatureFlagReference.

featureFlagIdHash = hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes($"{setting.Key}\n{setting.Label}"));
}

string featureFlagId = WebUtility.UrlEncode(Convert.ToBase64String(featureFlagIdHash));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't quite accurate for what Jimmy purposed. He suggested Base64Url encoding- which is different than Base64 + Url encoding (see https://stackoverflow.com/a/55389212/5937135).

I see an AspNetCore method for it here: https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.webutilities.webencoders.base64urlencode?view=aspnetcore-8.0

You could achieve base64url encoding with

string base64UrlStr = Convert.ToBase64String(Encoding.UTF8.GetBytes(target))
    .TrimEnd('=') // Remove padding (optional)
    .Replace('+', '-') // Replace '+' with '-'
    .Replace('/', '_'); // Replace '/' with '_'

But using a built-in method is probably preferred.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see, I think I misunderstood how the URL conversion works. I did see that AspNetCore method, but it's not compatible with the target frameworks in the main project, so I can't find another built in method that exists. I can change it to this for now.


using (HashAlgorithm hashAlgorithm = SHA256.Create())
{
featureFlagIdHash = hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes($"{setting.Key}\n{setting.Label}"));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't recall what the plan was- but if label is null- are we omitting the \n and null? I think currently this would give the string:

Key
null

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should evaluate to "Key\n", but I'm not sure if we clarified whether we want to keep the newline character as part of the value even if there's a null label? I think it's fine as it is but just wanted to call it out.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we differentiate null vs. empty label?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assumed we wouldn't, when I tried on portal I couldn't pass an empty string, and we also treat whitespace/null as the same for label filters in calls to Select.

Copy link

@rossgrambo rossgrambo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 remaining comment but otherwise lgtm


string featureFlagIdBase64 = Convert.ToBase64String(featureFlagIdHash);

StringBuilder featureFlagIdBuilder = new StringBuilder(featureFlagIdBase64[featureFlagIdBase64.Length - 1] == '=' ? featureFlagIdBase64.Length - 1 : featureFlagIdBase64.Length);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't b64 have two equal signs at the end?

also, seems like you should be using IndexOf('=') and judge based off that.

Copy link

@rossgrambo rossgrambo Feb 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. There's an appendix about implementing it we could follow https://datatracker.ietf.org/doc/html/rfc7515#appendix-C, they split to remove all "=", but index would work too (and probably better)

{
keyValues.Add(new KeyValuePair<string, string>($"{telemetryPath}:{FeatureManagementConstants.Enabled}", telemetry.Enabled.ToString()));
}

if (telemetry.Metadata != null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we move this inside the if (telemetry.Enabled)? No one cares about it if telemetry is not enabled.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see now, I thought there was a scenario in feature management where it could still be used but I don't see how. I can move it inside the enabled block too.

{
featureFlagIdBuilder.Append(featureFlagIdBase64[i]);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

Choose a reason for hiding this comment

The 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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will the extension method be reusable somewhere?

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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 CalculateFeatureFlagId(string key, string label)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good to me

Copy link
Member Author

Choose a reason for hiding this comment

The 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.

Copy link
Member

@jimmyca15 jimmyca15 Feb 14, 2024

Choose a reason for hiding this comment

The 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 System.Convert.ToBase64String


string featureFlagIdBase64 = Convert.ToBase64String(featureFlagIdHash);

int indexOfEquals = featureFlagIdBase64.IndexOf("=");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that I think of it- the input to this function is consistent. We know the indexof will always be missing or the 44th character. We could just stop after 43 characters.

I think the indexOf makes it clearer what's happening so we can keep it for now. And if we ever change the underlying hash, indexOf would continue to work.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment will be obsoleted by ToBase64Url extension method which takes unknown bytes as input.

{
internal static class BytesExtensions
{
public static string ToBase64Url(this byte[] bytes)
Copy link
Member

@jimmyca15 jimmyca15 Feb 14, 2024

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

…xtensions/BytesExtensions.cs

Co-authored-by: Jimmy Campbell <jimmyca@microsoft.com>
@amerjusupovic amerjusupovic merged commit 1d9ee4d into preview Feb 14, 2024
2 checks passed
@amerjusupovic amerjusupovic deleted the ajusupovic/populate-telemetry-metadata branch February 14, 2024 22:01
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code trims everything after the first occurrence of the =. It doesn't match your comment. You assume the = is at the end of the string. I'm wondering why we don't simply use TrimEnd.

Copy link
Member Author

Choose a reason for hiding this comment

The 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 TrimEnd initially but I got lost with this other implementation that was suggested and I didn't reconsider it. I think it makes more sense to be TrimEnd to save iteration and be more clear. Jumped the gun on the merge, so I can make another PR.

Copy link
Member

Choose a reason for hiding this comment

The 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 Convert.ToBase64String(bytes).Replace('+', '-').Replace('/', '_').TrimEnd('='). But that creates copies a bit carelessly.

Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason the featureFlagId needs to be URL safe?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For use in REST resource IDs (/experiment/{id})

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants