Skip to content

Commit e1c13b1

Browse files
authored
Remove git hash from client user agent (#471)
1 parent 365c6da commit e1c13b1

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/Grpc.Net.Client/Internal/GrpcProtocolConstants.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,21 @@ static GrpcProtocolConstants()
5858
{
5959
var userAgent = "grpc-dotnet";
6060

61+
// Use the assembly file version in the user agent.
62+
// We are not using `AssemblyInformationalVersionAttribute` because Source Link appends
63+
// the git hash to it, and sending a long user agent has perf implications.
6164
var assemblyVersion = typeof(GrpcProtocolConstants)
6265
.Assembly
63-
.GetCustomAttributes<AssemblyInformationalVersionAttribute>()
66+
.GetCustomAttributes<AssemblyFileVersionAttribute>()
6467
.FirstOrDefault();
6568

6669
Debug.Assert(assemblyVersion != null);
6770

68-
// assembly version attribute should always be present
69-
// but in case it isn't then don't include version in user-agent
71+
// Assembly file version attribute should always be present,
72+
// but in case it isn't then don't include version in user-agent.
7073
if (assemblyVersion != null)
7174
{
72-
userAgent += "/" + assemblyVersion.InformationalVersion;
75+
userAgent += "/" + assemblyVersion.Version;
7376
}
7477

7578
UserAgentHeader = ProductInfoHeaderValue.Parse(userAgent);

test/Grpc.Net.Client.Tests/AsyncUnaryCallTests.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ public async Task AsyncUnaryCall_Success_HttpRequestMessagePopulated()
7575
Assert.AreEqual(GrpcProtocolConstants.UserAgentHeader, userAgent);
7676
Assert.AreEqual("grpc-dotnet", userAgent.Product.Name);
7777
Assert.IsTrue(!string.IsNullOrEmpty(userAgent.Product.Version));
78+
79+
// Santity check that the user agent doesn't have the git hash in it and isn't too long.
80+
// Sending a long user agent with each call has performance implications.
81+
Assert.IsTrue(!userAgent.Product.Version.Contains('+'));
82+
Assert.IsTrue(userAgent.Product.Version.Length <= 10);
7883
}
7984

8085
[Test]

0 commit comments

Comments
 (0)