forked from open-telemetry/opentelemetry-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestGrpcNetClient.cs
60 lines (51 loc) · 1.95 KB
/
TestGrpcNetClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
using System.Diagnostics;
using Examples.GrpcService;
using Grpc.Core;
using Grpc.Net.Client;
using OpenTelemetry;
using OpenTelemetry.Trace;
namespace Examples.Console;
internal class TestGrpcNetClient
{
internal static object Run()
{
// Prerequisite for running this example.
// In a separate console window, start the example
// ASP.NET Core gRPC service by running the following command
// from the reporoot\examples\GrpcService\.
// (eg: C:\repos\opentelemetry-dotnet\examples\GrpcService\)
//
// dotnet run
// To run this example, run the following command from
// the reporoot\examples\Console\.
// (eg: C:\repos\opentelemetry-dotnet\examples\Console\)
//
// dotnet run grpc
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddGrpcClientInstrumentation()
.AddSource("grpc-net-client-test")
.AddConsoleExporter()
.Build();
using var source = new ActivitySource("grpc-net-client-test");
using (var parent = source.StartActivity("Main", ActivityKind.Server))
{
using var channel = GrpcChannel.ForAddress("https://localhost:44335");
var client = new Greeter.GreeterClient(channel);
try
{
var reply = client.SayHelloAsync(new HelloRequest { Name = "GreeterClient" }).GetAwaiter().GetResult();
System.Console.WriteLine($"Message received: {reply.Message}");
}
catch (RpcException)
{
System.Console.Error.WriteLine($"To run this Grpc.Net.Client example, first start the Examples.GrpcService project.");
throw;
}
}
System.Console.WriteLine("Press Enter key to exit.");
System.Console.ReadLine();
return null;
}
}