|
| 1 | +# Basic example showing how to record async spans between PRODUCER and CONSUMER applications |
| 2 | + |
| 3 | +This document will show how to implement basic PRODUCER and CONSUMER spans using zipkin4net library. |
| 4 | + |
| 5 | +## Implementation Overview |
| 6 | + |
| 7 | +We have 3 applications to produce example PRODUCER and CONSUMER spans. |
| 8 | + |
| 9 | +- `example.message.center` - Stores and pops messages. The messages contain trace information. |
| 10 | +- `example.message.producer` - Creates a message with trace information and stores it to `example.message.center`. Logs PRODUCER span to zipkin server. |
| 11 | +- `example.message.consumer` - Fetches the message from `example.message.center`. Logs CONSUMER span to zipkin server. |
| 12 | + |
| 13 | +## Pre-requisites |
| 14 | + |
| 15 | +- To build the example, you need to install at least [dotnet 2.2](https://dotnet.microsoft.com/download/dotnet-core/2.2) |
| 16 | +- To run the examples, you need a live zipkin server. |
| 17 | + |
| 18 | +## Running the example |
| 19 | + |
| 20 | +1. Run `example.message.center` app |
| 21 | + - On a command line, navigate to `Examples\async.spans\example.message.center` |
| 22 | + - Run `dotnet run` |
| 23 | +  |
| 24 | + |
| 25 | +2. Run `example.message.producer` app |
| 26 | + - On a command line, navigate to `Examples\async.spans\example.message.producer` |
| 27 | + - Run `dotnet run <base url of live zipkin server>` |
| 28 | +  |
| 29 | + |
| 30 | +3. Run `example.message.consumer` app |
| 31 | + - On a command line, navigate to `Examples\async.spans\example.message.consumer` |
| 32 | + - Run `dotnet run <base url of live zipkin server>` |
| 33 | +  |
| 34 | + |
| 35 | +4. Check the output |
| 36 | + - Go to zipkin UI |
| 37 | + - Search for `message.producer` or `message.consumer` as serviceName |
| 38 | + - Click one of the search result, it should show the PRODUCER and CONSUMER spans |
| 39 | +  |
| 40 | + |
| 41 | +## What to take note on how to create/use PRODUCER and CONSUMER spans |
| 42 | + |
| 43 | +### PRODUCER spans |
| 44 | + |
| 45 | +- To make a PRODUCER span, you need to use `ProducerTrace` class |
| 46 | +- Example code from [example.message.producer](example.message.producer/Program.cs) |
| 47 | + ```csharp |
| 48 | + using (var messageProducerTrace = new ProducerTrace("<Application name>", "<RPC here>")) |
| 49 | + { |
| 50 | + // TracedActionAsync extension method logs error annotation if exception occurs |
| 51 | + await messageProducerTrace.TracedActionAsync(ProduceMessage(messageProducerTrace.Trace.CurrentSpan, text)); |
| 52 | + messageProducerTrace.AddAnnotation(Annotations.Tag("sampleProducerTag", "success!")); |
| 53 | + } |
| 54 | + ``` |
| 55 | +- `TracedActionAsync` is used to run the process that is measured to log error annotation in your zipkin trace if exception is thrown. |
| 56 | +- Make a way that trace information is passed to the consumer. So in the example, the trace information is part of the message which will be parsed by the consumer application to create CONSUMER spans. |
| 57 | +- Also, custom annotations can be added using the ProducerTrace object method `AddAnnotation`. |
| 58 | + |
| 59 | +### CONSUMER spans |
| 60 | + |
| 61 | +- To make a CONSUMER span, you need to use `ConsumerTrace` class |
| 62 | +- Example code from [example.message.consumer](example.message.consumer/Program.cs) |
| 63 | + ```csharp |
| 64 | + static async Task ProcessMessage(Message message) |
| 65 | + { |
| 66 | + // need to supply trace information from producer |
| 67 | + using (var messageConsumerTrace = new ConsumerTrace( |
| 68 | + serviceName: "<Application name>", |
| 69 | + rpc: "<RPC here>", |
| 70 | + encodedTraceId: message.TraceId, |
| 71 | + encodedSpanId: message.SpanId, |
| 72 | + encodedParentSpanId: message.ParentId, |
| 73 | + sampledStr: message.Sampled, |
| 74 | + flagsStr: message.Flags.ToString(CultureInfo.InvariantCulture))) |
| 75 | + { |
| 76 | + await messageConsumerTrace.TracedActionAsync(Task.Delay(600)); // Test delay for mock processing |
| 77 | + messageConsumerTrace.AddAnnotation(Annotations.Tag("sampleConsumerTag", "success!")); |
| 78 | + } |
| 79 | + } |
| 80 | + ``` |
| 81 | +- In the example PRODUCER application passed the trace information through the `message` object. Using the trace information, CONSUMER span is created. |
| 82 | +- `TracedActionAsync` is used to run the process that is measured to log error annotation in your zipkin trace if exception is thrown. |
| 83 | +- Also, custom annotations can be added using the ConsumerTrace object method `AddAnnotation`. |
0 commit comments