Skip to content

Commit

Permalink
Merge pull request #559 from momentohq/add-client-name-to-agent-header
Browse files Browse the repository at this point in the history
feat: add agent header to auth client, add client identifiers to all agent headers
  • Loading branch information
cprice404 authored Jun 18, 2024
2 parents c0541e6 + 9d6a972 commit 35b9808
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Momento.Sdk/AuthClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public AuthClient(IAuthConfiguration config, ICredentialProvider authProvider)
{
scsTokenClient = new ScsTokenClient(config, authProvider.AuthToken, authProvider.TokenEndpoint);
}

/// <inheritdoc />
public async Task<GenerateDisposableTokenResponse> GenerateDisposableTokenAsync(DisposableTokenScope scope, ExpiresIn expiresIn, string? tokenId = null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Momento.Sdk/Internal/ScsDataClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected Metadata MetadataWithCache(string cacheName)
this.hasSentOnetimeHeaders = true;
string sdkVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
string runtimeVer = System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription;
return new Metadata() { { "cache", cacheName }, { "Agent", $"dotnet:{sdkVersion}" }, { "Runtime-Version", runtimeVer } };
return new Metadata() { { "cache", cacheName }, { "Agent", $"dotnet:cache:{sdkVersion}" }, { "Runtime-Version", runtimeVer } };
}
protected DateTime CalculateDeadline()
{
Expand Down
16 changes: 15 additions & 1 deletion src/Momento.Sdk/Internal/ScsTokenClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member

using System;
using System.Reflection;
using System.Threading.Tasks;
using Grpc.Core;
using Microsoft.Extensions.Logging;
Expand All @@ -21,6 +22,7 @@ internal sealed class ScsTokenClient : IDisposable
private readonly string authToken;
private readonly TimeSpan authClientOperationTimeout;
private readonly ILogger _logger;
private bool hasSentOnetimeHeaders = false;
private readonly CacheExceptionMapper _exceptionMapper;
public ScsTokenClient(IAuthConfiguration config, string authToken, string endpoint)
{
Expand All @@ -30,6 +32,17 @@ public ScsTokenClient(IAuthConfiguration config, string authToken, string endpoi
this._logger = config.LoggerFactory.CreateLogger<ScsTokenClient>();
this._exceptionMapper = new CacheExceptionMapper(config.LoggerFactory);
}

private Metadata Metadata()
{
if (this.hasSentOnetimeHeaders) {
return new Metadata();
}
this.hasSentOnetimeHeaders = true;
string sdkVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
string runtimeVer = System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription;
return new Metadata() { { "Agent", $"dotnet:auth:{sdkVersion}" }, { "Runtime-Version", runtimeVer } };
}

private DateTime CalculateDeadline()
{
Expand Down Expand Up @@ -65,9 +78,10 @@ public async Task<GenerateDisposableTokenResponse> GenerateDisposableToken(
Permissions = permissions,
TokenId = tokenId ?? ""
};
var metadata = Metadata();
_logger.LogTraceExecutingGenericRequest(RequestTypeAuthGenerateDisposableToken);
var response = await grpcManager.Client.generateDisposableToken(
request, new CallOptions(deadline: CalculateDeadline())
request, new CallOptions(headers: metadata, deadline: CalculateDeadline())
);
return _logger.LogTraceGenericRequestSuccess(RequestTypeAuthGenerateDisposableToken,
new GenerateDisposableTokenResponse.Success(response));
Expand Down
4 changes: 2 additions & 2 deletions src/Momento.Sdk/Internal/ScsTopicClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ public ScsTopicClientBase(ITopicConfiguration config, string authToken, string e
this._exceptionMapper = new CacheExceptionMapper(config.LoggerFactory);
}

protected Metadata MetadataWithCache(string cacheName)
private Metadata MetadataWithCache(string cacheName)
{
if (this.hasSentOnetimeHeaders) {
return new Metadata() { { "cache", cacheName } };
}
this.hasSentOnetimeHeaders = true;
string sdkVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
string runtimeVer = System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription;
return new Metadata() { { "cache", cacheName }, { "Agent", $"dotnet:{sdkVersion}" }, { "Runtime-Version", runtimeVer } };
return new Metadata() { { "cache", cacheName }, { "Agent", $"dotnet:topic:{sdkVersion}" }, { "Runtime-Version", runtimeVer } };
}

protected DateTime CalculateDeadline()
Expand Down

0 comments on commit 35b9808

Please sign in to comment.