Skip to content

Commit 6d4b7eb

Browse files
dev: added sdk version header to any RPC call
1 parent 445c025 commit 6d4b7eb

File tree

4 files changed

+18
-15
lines changed

4 files changed

+18
-15
lines changed

src/Ydb.Sdk/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
- Added `x-ydb-sdk-build-info` header to any RPC call.
2+
13
## v0.16.0
24

35
- **Breaking Change**: `Ydb.Sdk.Yc.Auth` version <= 0.1.0 is not compatible with newer versions.

src/Ydb.Sdk/src/Driver.cs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Collections.Immutable;
2-
using System.Reflection;
32
using Grpc.Core;
43
using Grpc.Net.Client;
54
using Microsoft.Extensions.Logging;
@@ -16,7 +15,6 @@ public sealed class Driver : BaseDriver
1615
{
1716
private const int AttemptDiscovery = 10;
1817

19-
private readonly string _sdkInfo;
2018
private readonly GrpcChannelFactory _grpcChannelFactory;
2119
private readonly EndpointPool _endpointPool;
2220
private readonly ChannelPool<GrpcChannel> _channelPool;
@@ -35,10 +33,6 @@ public Driver(DriverConfig config, ILoggerFactory? loggerFactory = null)
3533
_grpcChannelFactory
3634
);
3735

38-
var version = Assembly.GetExecutingAssembly().GetName().Version;
39-
var versionStr = version is null ? "unknown" : version.ToString(3);
40-
_sdkInfo = $"ydb-dotnet-sdk/{versionStr}";
41-
4236
CredentialsProvider = Config.User != null
4337
? new CachedCredentialsProvider(
4438
new StaticCredentialsAuthClient(config, _grpcChannelFactory, LoggerFactory),
@@ -134,12 +128,10 @@ private async Task<Status> DiscoverEndpoints()
134128
TransportTimeout = Config.EndpointDiscoveryTimeout
135129
};
136130

137-
var options = await GetCallOptions(requestSettings);
138-
options.Headers?.Add(Metadata.RpcSdkInfoHeader, _sdkInfo);
139-
140131
var response = await client.ListEndpointsAsync(
141132
request: request,
142-
options: options);
133+
options: await GetCallOptions(requestSettings)
134+
);
143135

144136
if (!response.Operation.Ready)
145137
{
@@ -168,7 +160,8 @@ private async Task<Status> DiscoverEndpoints()
168160

169161
Logger.LogDebug(
170162
"Successfully discovered endpoints: {EndpointsCount}, self location: {SelfLocation}, sdk info: {SdkInfo}",
171-
resultProto.Endpoints.Count, resultProto.SelfLocation, _sdkInfo);
163+
resultProto.Endpoints.Count, resultProto.SelfLocation, Config.SdkVersion
164+
);
172165

173166
_endpointPool.Reset(resultProto.Endpoints
174167
.Select(endpointSettings => new EndpointSettings(
@@ -189,15 +182,16 @@ private async Task PeriodicDiscovery()
189182
try
190183
{
191184
await Task.Delay(Config.EndpointDiscoveryInterval);
185+
192186
_ = await DiscoverEndpoints();
193187
}
194188
catch (RpcException e)
195189
{
196-
Logger.LogWarning($"RPC error during endpoint discovery: {e.Status}");
190+
Logger.LogWarning("RPC error during endpoint discovery: {Status}", e.Status);
197191
}
198192
catch (Exception e)
199193
{
200-
Logger.LogError($"Unexpected exception during session pool periodic check: {e}");
194+
Logger.LogError(e, "Unexpected exception during session pool periodic check");
201195
}
202196
}
203197
}

src/Ydb.Sdk/src/DriverConfig.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Security.Cryptography.X509Certificates;
1+
using System.Reflection;
2+
using System.Security.Cryptography.X509Certificates;
23
using Ydb.Sdk.Auth;
34

45
namespace Ydb.Sdk;
@@ -21,6 +22,7 @@ public class DriverConfig
2122
internal X509Certificate2Collection CustomServerCertificates { get; } = new();
2223
internal TimeSpan EndpointDiscoveryInterval = TimeSpan.FromMinutes(1);
2324
internal TimeSpan EndpointDiscoveryTimeout = TimeSpan.FromSeconds(10);
25+
internal string SdkVersion { get; }
2426

2527
public DriverConfig(
2628
string endpoint,
@@ -42,6 +44,10 @@ public DriverConfig(
4244
{
4345
CustomServerCertificates.AddRange(customServerCertificates);
4446
}
47+
48+
var version = Assembly.GetExecutingAssembly().GetName().Version;
49+
var versionStr = version is null ? "unknown" : version.ToString(3);
50+
SdkVersion = $"ydb-dotnet-sdk/{versionStr}";
4551
}
4652

4753
private static string FormatEndpoint(string endpoint)

src/Ydb.Sdk/src/IDriver.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ protected async ValueTask<CallOptions> GetCallOptions(GrpcRequestSettings settin
138138
{
139139
var meta = new Grpc.Core.Metadata
140140
{
141-
{ Metadata.RpcDatabaseHeader, Config.Database }
141+
{ Metadata.RpcDatabaseHeader, Config.Database },
142+
{ Metadata.RpcSdkInfoHeader, Config.SdkVersion }
142143
};
143144

144145
if (CredentialsProvider != null)

0 commit comments

Comments
 (0)