private void RabbitMqConsumer_Received(object sender, BasicDeliverEventArgs e) { // ExtractActivity creates the Activity setting the parent based on the RabbitMQ "traceparent" header var activity = e.ExtractActivity("Process single RabbitMQ message"); TelemetrySpan span = null; if (tracer != null) { // OpenTelemetry seems to require the Activity to have started, unlike AI SDK activity.Start(); tracer.StartActiveSpanFromActivity(activity.OperationName, activity, SpanKind.Consumer, out span); span.SetAttribute(Telemetry.Constants.RoutingKeyTagName, e.RoutingKey); } try { string message = DeserializeMessage(e.Body, e.RoutingKey); string fullTypeName = e.BasicProperties.GetCustomProperty <string>(TypeNameHeader); OnMessagedReceived(new MessageReceivedEventArgs(message, fullTypeName)); } catch (Exception ex) { logger.LogError(ex, "Unable to proccess message. Routing key: {RoutingKey}", e.RoutingKey); } finally { span?.End(); } }
private async Task ProcessWebQueueMessageAsync(object sender, BasicDeliverEventArgs @event) { // ExtractActivity creates the Activity setting the parent based on the RabbitMQ "traceparent" header var activity = @event.ExtractActivity("Process single RabbitMQ message"); ISpan span = null; IOperationHolder <DependencyTelemetry> operation = null; var processingSucceeded = false; string source = string.Empty; IDisposable loggingScope = null; try { if (tracer != null) { // OpenTelemetry seems to require the Activity to have started, unlike AI SDK activity.Start(); tracer.StartActiveSpanFromActivity(activity.OperationName, activity, SpanKind.Consumer, out span); span.SetAttribute("queue", Constants.WebQueueName); } using (operation = telemetryClient?.StartOperation <DependencyTelemetry>(activity)) { if (operation != null) { operation.Telemetry.Properties.Add("queue", Constants.WebQueueName); operation.Telemetry.Type = ApplicationInformation.Name; operation.Telemetry.Target = this.rabbitMQHostName; } loggingScope = logger.BeginScope("Starting message processing"); // Get the payload var message = JsonSerializer.Deserialize <EnqueuedMessage>(@event.Body, jsonSerializerOptions); if (logger.IsEnabled(LogLevel.Information)) { logger.LogInformation("Processing message from {source}: {message}", message.Source, Encoding.UTF8.GetString(@event.Body)); } source = message.Source; if ("error".Equals(message.EventName, StringComparison.OrdinalIgnoreCase)) { throw new InvalidEventNameException("Invalid event name"); } var apiFullUrl = $"{timeApiURL}/api/time/dbtime"; var time = await httpClientFactory.CreateClient().GetStringAsync(apiFullUrl); if (!string.IsNullOrEmpty(message.EventName)) { span?.AddEvent(message.EventName); telemetryClient?.TrackEvent(message.EventName); } } processingSucceeded = true; } catch (Exception ex) { if (span != null) { span.SetAttribute("error", true); span.Status = Status.Internal.WithDescription(ex.ToString()); } if (operation != null) { operation.Telemetry.Success = false; operation.Telemetry.ResultCode = "500"; // Track exception, adding the connection to the current activity var exOperation = new ExceptionTelemetry(ex); exOperation.Context.Operation.Id = operation.Telemetry.Context.Operation.Id; exOperation.Context.Operation.ParentId = operation.Telemetry.Context.Operation.ParentId; telemetryClient.TrackException(exOperation); } logger.LogError(ex, "Failed to process message from {source}", source); } finally { span?.End(); metrics.TrackItemProcessed(1, source, processingSucceeded); loggingScope?.Dispose(); } }