Skip to content

Commit

Permalink
feat: update agent header and endpoint for gRPC web (#443)
Browse files Browse the repository at this point in the history
* feat: use web prefix for endpoints when using gRPC-web

The domain for our HTTP/1.1 endpoints now has a "web." prefix. We
adjust the endpoints appropriately.

* feat: use appropriate agent header moniker when using gRPC-web
  • Loading branch information
malandis authored May 23, 2023
1 parent b215a2e commit af01751
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
14 changes: 12 additions & 2 deletions src/Momento.Sdk/Internal/ControlGrpcManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,24 @@ internal sealed class ControlGrpcManager : IDisposable
{
private readonly GrpcChannel channel;
public IControlClient Client { get; }
private readonly string version = "dotnet:" + GetAssembly(typeof(Momento.Sdk.Responses.CacheGetResponse)).GetName().Version.ToString();

#if USE_GRPC_WEB
private readonly static string moniker = "dotnet-web";
#else
private readonly static string moniker = "dotnet";
#endif
private readonly string version = $"{moniker}:{GetAssembly(typeof(Momento.Sdk.Responses.CacheGetResponse)).GetName().Version.ToString()}";
// Some System.Environment.Version remarks to be aware of
// https://learn.microsoft.com/en-us/dotnet/api/system.environment.version?view=netstandard-2.0#remarks
private readonly string runtimeVersion = "dotnet:" + System.Environment.Version;
private readonly string runtimeVersion = $"{moniker}:{System.Environment.Version}";
private readonly ILogger _logger;

public ControlGrpcManager(ILoggerFactory loggerFactory, string authToken, string endpoint)
{
#if USE_GRPC_WEB
// Note: all web SDK requests are routed to a `web.` subdomain to allow us flexibility on the server
endpoint = $"web.{endpoint}";
#endif
var uri = $"https://{endpoint}";
this.channel = GrpcChannel.ForAddress(uri, new GrpcChannelOptions()
{
Expand Down
19 changes: 14 additions & 5 deletions src/Momento.Sdk/Internal/DataGrpcManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,15 +226,24 @@ public class DataGrpcManager : IDisposable

public readonly IDataClient Client;

private readonly string version = "dotnet:" + GetAssembly(typeof(Responses.CacheGetResponse)).GetName().Version.ToString();
#if USE_GRPC_WEB
private readonly static string moniker = "dotnet-web";
#else
private readonly static string moniker = "dotnet";
#endif
private readonly string version = $"{moniker}:{GetAssembly(typeof(Responses.CacheGetResponse)).GetName().Version.ToString()}";
// Some System.Environment.Version remarks to be aware of
// https://learn.microsoft.com/en-us/dotnet/api/system.environment.version?view=netstandard-2.0#remarks
private readonly string runtimeVersion = "dotnet:" + Environment.Version;
private readonly string runtimeVersion = $"{moniker}:{Environment.Version}";
private readonly ILogger _logger;

internal DataGrpcManager(IConfiguration config, string authToken, string host)
internal DataGrpcManager(IConfiguration config, string authToken, string endpoint)
{
var url = $"https://{host}";
#if USE_GRPC_WEB
// Note: all web SDK requests are routed to a `web.` subdomain to allow us flexibility on the server
endpoint = $"web.{endpoint}";
#endif
var uri = $"https://{endpoint}";
var channelOptions = config.TransportStrategy.GrpcConfig.GrpcChannelOptions;
if (channelOptions.LoggerFactory == null)
{
Expand All @@ -245,7 +254,7 @@ internal DataGrpcManager(IConfiguration config, string authToken, string host)
channelOptions.HttpHandler = new GrpcWebHandler(new HttpClientHandler());
#endif

this.channel = GrpcChannel.ForAddress(url, channelOptions);
this.channel = GrpcChannel.ForAddress(uri, channelOptions);
List<Header> headers = new List<Header> { new Header(name: Header.AuthorizationKey, value: authToken), new Header(name: Header.AgentKey, value: version), new Header(name: Header.RuntimeVersionKey, value: runtimeVersion) };

this._logger = config.LoggerFactory.CreateLogger<DataGrpcManager>();
Expand Down

0 comments on commit af01751

Please sign in to comment.