コード例 #1
0
        public string SendMessage()
        {
            try
            {
                // Start an activity with a name following the semantic convention of the OpenTelemetry messaging specification.
                // https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/messaging.md#span-name
                var activityName = $"{RabbitMqHelper.TestQueueName} send";

                using (var activity = ActivitySource.StartActivity(activityName, ActivityKind.Producer))
                {
                    var props = this.channel.CreateBasicProperties();

                    // Depending on Sampling (and whether a listener is registered or not), the
                    // activity above may not be created.
                    // If it is created, then propagate its context.
                    // If it is not created, the propagate the Current context,
                    // if any.
                    ActivityContext contextToInject = default;
                    if (activity != null)
                    {
                        this.logger.LogInformation("Injecting Context From RabbitMqHelper Activity");
                        contextToInject = activity.Context;
                    }
                    else if (Activity.Current != null)
                    {
                        this.logger.LogInformation("Injecting Context From Some Existing Activity");
                        contextToInject = Activity.Current.Context;
                    }

                    // Inject the ActivityContext into the message headers to propagate trace context to the receiving service.
                    //Propagator.Inject(new PropagationContext(activity.Context, Baggage.Current), props, this.InjectTraceContextIntoBasicProperties);
                    Propagator.Inject(new PropagationContext(contextToInject, Baggage.Current), props, this.InjectTraceContextIntoBasicProperties);

                    /* ktully
                     * The code below is a back-port to support nuget 0.4.0-beta2
                     * The above code is the way to do it for the currentrelease (0.5.0-beta2)!!
                     */
                    //TextFormat.Inject(activity.Context, props, this.InjectTraceContextIntoBasicProperties);

                    // The OpenTelemetry messaging specification defines a number of attributes. These attributes are added here.
                    RabbitMqHelper.AddMessagingTags(activity);
                    var body = $"Published message: DateTime.Now = {DateTime.Now}.";

                    this.channel.BasicPublish(
                        exchange: RabbitMqHelper.DefaultExchangeName,
                        routingKey: RabbitMqHelper.TestQueueName,
                        basicProperties: props,
                        body: Encoding.UTF8.GetBytes(body));

                    this.logger.LogInformation($"Message sent: [{body}]");

                    return(body);
                }
            }
            catch (Exception ex)
            {
                this.logger.LogError(ex, "Message publishing failed.");
                throw;
            }
        }
コード例 #2
0
        public void StartConsumer()
        {
            this.logger.LogInformation("Waiting ANOTHER 5 seconds for RabbitMQ to boot...");
            Thread.Sleep(5000);
            this.logger.LogInformation("5 seconds elapsed, calling StartConsumer!");

            RabbitMqHelper.StartConsumer(this.channel, this.ReceiveMessage);
        }
コード例 #3
0
        public MessageReceiver(ILogger <MessageReceiver> logger)
        {
            this.logger = logger;

            this.logger.LogInformation("Waiting 20 seconds for RabbitMQ to boot...");
            //Task.Delay(5000).Wait();
            Thread.Sleep(20000);
            this.logger.LogInformation("20 seconds elapsed, initializing RabbitMqReceiver!");

            this.connection = RabbitMqHelper.CreateConnection();
            this.channel    = RabbitMqHelper.CreateModelAndDeclareTestQueue(this.connection);
        }
コード例 #4
0
        public string SendMessage()
        {
            try
            {
                // Start an activity with a name following the semantic convention of the OpenTelemetry messaging specification.
                // https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/messaging.md#span-name
                var activityName = $"{RabbitMqHelper.TestQueueName} send";

                using (var activity = ActivitySource.StartActivity(activityName, ActivityKind.Producer))
                {
                    var props = this.channel.CreateBasicProperties();

                    if (activity != null)
                    {
                        // Inject the ActivityContext into the message headers to propagate trace context to the receiving service.
                        Propagator.Inject(new PropagationContext(activity.Context, Baggage.Current), props, this.InjectTraceContextIntoBasicProperties);

                        // The OpenTelemetry messaging specification defines a number of attributes. These attributes are added here.
                        RabbitMqHelper.AddMessagingTags(activity);
                    }

                    var body = $"Published message: DateTime.Now = {DateTime.Now}.";

                    this.channel.BasicPublish(
                        exchange: RabbitMqHelper.DefaultExchangeName,
                        routingKey: RabbitMqHelper.TestQueueName,
                        basicProperties: props,
                        body: Encoding.UTF8.GetBytes(body));

                    this.logger.LogInformation($"Message sent: [{body}]");

                    return(body);
                }
            }
            catch (Exception ex)
            {
                this.logger.LogError(ex, "Message publishing failed.");
                throw;
            }
        }
コード例 #5
0
 public MessageSender(ILogger <MessageSender> logger)
 {
     this.logger     = logger;
     this.connection = RabbitMqHelper.CreateConnection();
     this.channel    = RabbitMqHelper.CreateModelAndDeclareTestQueue(this.connection);
 }
コード例 #6
0
 public void StartConsumer()
 {
     RabbitMqHelper.StartConsumer(this.channel, this.ReceiveMessage);
 }