Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/Grpc.Net.Client/Internal/GrpcCall.NonGeneric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,5 +182,46 @@ private static StatusCode MapHttpStatusToGrpcCode(HttpStatusCode httpStatusCode)
return StatusCode.Unknown;
}
}

protected internal sealed class ActivityStartData
{
#if NET5_0_OR_GREATER
// Common properties. Properties not in this list could be trimmed.
[DynamicDependency(nameof(HttpRequestMessage.RequestUri), typeof(HttpRequestMessage))]
[DynamicDependency(nameof(HttpRequestMessage.Method), typeof(HttpRequestMessage))]
[DynamicDependency(nameof(Uri.Host), typeof(Uri))]
[DynamicDependency(nameof(Uri.Port), typeof(Uri))]
#endif
internal ActivityStartData(HttpRequestMessage request)
{
Request = request;
}

public HttpRequestMessage Request { get; }

public override string ToString() => $"{{ {nameof(Request)} = {Request} }}";
}

protected internal sealed class ActivityStopData
{
#if NET5_0_OR_GREATER
// Common properties. Properties not in this list could be trimmed.
[DynamicDependency(nameof(HttpRequestMessage.RequestUri), typeof(HttpRequestMessage))]
[DynamicDependency(nameof(HttpRequestMessage.Method), typeof(HttpRequestMessage))]
[DynamicDependency(nameof(Uri.Host), typeof(Uri))]
[DynamicDependency(nameof(Uri.Port), typeof(Uri))]
[DynamicDependency(nameof(HttpResponseMessage.StatusCode), typeof(HttpResponseMessage))]
#endif
internal ActivityStopData(HttpResponseMessage? response, HttpRequestMessage request)
{
Response = response;
Request = request;
}

public HttpResponseMessage? Response { get; }
public HttpRequestMessage Request { get; }

public override string ToString() => $"{{ {nameof(Response)} = {Response}, {nameof(Request)} = {Request} }}";
}
}
}
45 changes: 2 additions & 43 deletions src/Grpc.Net.Client/Internal/GrpcCall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ private void CancelCall(Status status)

internal IDisposable? StartScope()
{
// Only return a scope if the logger is enabled to log
// Only return a scope if the logger is enabled to log
// in at least Critical level for performance
if (Logger.IsEnabled(LogLevel.Critical))
{
Expand Down Expand Up @@ -811,7 +811,7 @@ private bool FinishCall(HttpRequestMessage request, bool diagnosticSourceEnabled
if (diagnosticSourceEnabled)
{
// Stop sets the end time if it was unset, but we want it set before we issue the write
// so we do it now.
// so we do it now.
if (activity.Duration == TimeSpan.Zero)
{
activity.SetEndTime(DateTime.UtcNow);
Expand Down Expand Up @@ -1058,46 +1058,5 @@ private static void WriteDiagnosticEvent<
{
diagnosticSource.Write(name, value);
}

private sealed class ActivityStartData
{
#if NET5_0_OR_GREATER
// Common properties. Properties not in this list could be trimmed.
[DynamicDependency(nameof(HttpRequestMessage.RequestUri), typeof(HttpRequestMessage))]
[DynamicDependency(nameof(HttpRequestMessage.Method), typeof(HttpRequestMessage))]
[DynamicDependency(nameof(Uri.Host), typeof(Uri))]
[DynamicDependency(nameof(Uri.Port), typeof(Uri))]
#endif
internal ActivityStartData(HttpRequestMessage request)
{
Request = request;
}

public HttpRequestMessage Request { get; }

public override string ToString() => $"{{ {nameof(Request)} = {Request} }}";
}

private sealed class ActivityStopData
{
#if NET5_0_OR_GREATER
// Common properties. Properties not in this list could be trimmed.
[DynamicDependency(nameof(HttpRequestMessage.RequestUri), typeof(HttpRequestMessage))]
[DynamicDependency(nameof(HttpRequestMessage.Method), typeof(HttpRequestMessage))]
[DynamicDependency(nameof(Uri.Host), typeof(Uri))]
[DynamicDependency(nameof(Uri.Port), typeof(Uri))]
[DynamicDependency(nameof(HttpResponseMessage.StatusCode), typeof(HttpResponseMessage))]
#endif
internal ActivityStopData(HttpResponseMessage? response, HttpRequestMessage request)
{
Response = response;
Request = request;
}

public HttpResponseMessage? Response { get; }
public HttpRequestMessage Request { get; }

public override string ToString() => $"{{ {nameof(Response)} = {Response}, {nameof(Request)} = {Request} }}";
}
}
}
54 changes: 47 additions & 7 deletions test/Grpc.Net.Client.Tests/DiagnosticsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

using System.Diagnostics;
using System.Net;
using Google.Protobuf;
using Greet;
using Grpc.Core;
using Grpc.Net.Client.Internal;
Expand Down Expand Up @@ -83,20 +84,59 @@ public void DiagnosticListener_MakeCall_ActivityWritten()

var result = new List<KeyValuePair<string, object?>>();

var dataMessageMarshaller = new Marshaller<DataMessage>(m => m.ToByteArray(), data => DataMessage.Parser.ParseFrom(data));
var dataMessageMethod = ClientTestHelpers.GetServiceMethod<DataMessage, DataMessage>(
MethodType.DuplexStreaming,
dataMessageMarshaller,
dataMessageMarshaller);

// Act
HttpRequestMessage? requestMessage1 = null;
HttpResponseMessage? responseMessage1 = null;
HttpRequestMessage? requestMessage2 = null;
HttpResponseMessage? responseMessage2 = null;

using (GrpcDiagnostics.DiagnosticListener.Subscribe(new ObserverToList<KeyValuePair<string, object?>>(result)))
{
var c = invoker.AsyncDuplexStreamingCall<HelloRequest, HelloReply>(ClientTestHelpers.ServiceMethod, string.Empty, new CallOptions());
c.Dispose();
var c1 = invoker.AsyncDuplexStreamingCall<HelloRequest, HelloReply>(ClientTestHelpers.ServiceMethod, string.Empty, new CallOptions());
c1.Dispose();

requestMessage1 = requestMessage;
responseMessage1 = responseMessage;

var c2 = invoker.AsyncDuplexStreamingCall<DataMessage, DataMessage>(dataMessageMethod, string.Empty, new CallOptions());
c2.Dispose();

requestMessage2 = requestMessage;
responseMessage2 = responseMessage;
}

// Assert
Assert.AreEqual(2, result.Count);
Assert.AreEqual(4, result.Count);

// First call
Assert.AreEqual(GrpcDiagnostics.ActivityStartKey, result[0].Key);
Assert.AreEqual(requestMessage, GetValueFromAnonymousType<HttpRequestMessage>(result[0].Value!, "Request"));
Assert.AreEqual(requestMessage1, GetValueFromAnonymousType<HttpRequestMessage>(result[0].Value!, "Request"));
Assert.AreEqual(GrpcDiagnostics.ActivityStopKey, result[1].Key);
Assert.AreEqual(requestMessage, GetValueFromAnonymousType<HttpRequestMessage>(result[1].Value!, "Request"));
Assert.AreEqual(responseMessage, GetValueFromAnonymousType<HttpResponseMessage>(result[1].Value!, "Response"));
Assert.AreEqual(requestMessage1, GetValueFromAnonymousType<HttpRequestMessage>(result[1].Value!, "Request"));
Assert.AreEqual(responseMessage1, GetValueFromAnonymousType<HttpResponseMessage>(result[1].Value!, "Response"));

// Second call
Assert.AreEqual(GrpcDiagnostics.ActivityStartKey, result[2].Key);
Assert.AreEqual(requestMessage2, GetValueFromAnonymousType<HttpRequestMessage>(result[2].Value!, "Request"));
Assert.AreEqual(GrpcDiagnostics.ActivityStopKey, result[3].Key);
Assert.AreEqual(requestMessage2, GetValueFromAnonymousType<HttpRequestMessage>(result[3].Value!, "Request"));
Assert.AreEqual(responseMessage2, GetValueFromAnonymousType<HttpResponseMessage>(result[3].Value!, "Response"));

// Check types are expected
Assert.AreEqual(typeof(GrpcCall.ActivityStartData), result[0].Value!.GetType());
Assert.AreEqual(typeof(GrpcCall.ActivityStopData), result[1].Value!.GetType());
Assert.AreEqual(result[0].Value!.GetType(), result[2].Value!.GetType());
Assert.AreEqual(result[1].Value!.GetType(), result[3].Value!.GetType());

// Check values are unique for each call
Assert.AreNotEqual(result[0].Value, result[2].Value);
Assert.AreNotEqual(result[1].Value, result[3].Value);
}

[Test]
Expand Down Expand Up @@ -204,7 +244,7 @@ public void OnNext(T value)

private readonly List<T> _output;
private readonly Predicate<T>? _filter;
private readonly string? _name; // for debugging
private readonly string? _name; // for debugging
#endregion
}
}
Expand Down
4 changes: 4 additions & 0 deletions test/Grpc.Net.Client.Tests/Proto/greet.proto
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ message HelloRequest {
message HelloReply {
string message = 1;
}

message DataMessage {
bytes data = 1;
}
5 changes: 5 additions & 0 deletions test/Shared/ClientTestHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public static Method<HelloRequest, HelloReply> GetServiceMethod(MethodType? meth
return new Method<HelloRequest, HelloReply>(methodType ?? MethodType.Unary, "ServiceName", "MethodName", requestMarshaller ?? HelloRequestMarshaller, HelloReplyMarshaller);
}

public static Method<TRequest, TResponse> GetServiceMethod<TRequest, TResponse>(MethodType methodType, Marshaller<TRequest> requestMarshaller, Marshaller<TResponse> responseMarshaller)
{
return new Method<TRequest, TResponse>(methodType, "ServiceName", "MethodName", requestMarshaller, responseMarshaller);
}

public static TestHttpMessageHandler CreateTestMessageHandler(HelloReply reply)
{
return TestHttpMessageHandler.Create(async r =>
Expand Down