public async Task Handle(ProducerPipelineContext context, ProducerBehaviorHandler next) { if (GlobalTracer.Instance?.ActiveSpan == null) { await next(context); return; } var envelope = context.Envelope; var spanBuilder = GlobalTracer.Instance.BuildSpan($"Publish message on topic {envelope.Endpoint.Name}") .WithTag(Tags.SpanKind, Tags.SpanKindProducer) .WithTag("endpoint", envelope.Endpoint.Name); using (spanBuilder.StartActive()) { GlobalTracer.Instance.Inject( GlobalTracer.Instance.ActiveSpan.Context, BuiltinFormats.TextMap, new SilverbackTextMapInjectAdapter(envelope)); await next(context); } }
/// <inheritdoc cref="IProducerBehavior.HandleAsync" /> public async Task HandleAsync(ProducerPipelineContext context, ProducerBehaviorHandler next) { Check.NotNull(context, nameof(context)); Check.NotNull(next, nameof(next)); foreach (var sequenceWriter in _sequenceWriters) { if (!sequenceWriter.CanHandle(context.Envelope)) { continue; } var envelopesEnumerable = sequenceWriter.ProcessMessageAsync(context.Envelope); await foreach (var envelope in envelopesEnumerable.ConfigureAwait(false)) { var newContext = new ProducerPipelineContext(envelope, context.Producer, context.ServiceProvider); await next(newContext).ConfigureAwait(false); } return; } await next(context).ConfigureAwait(false); }
/// <inheritdoc cref="IProducerBehavior.HandleAsync" /> public async Task HandleAsync(ProducerPipelineContext context, ProducerBehaviorHandler next) { Check.NotNull(context, nameof(context)); Check.NotNull(next, nameof(next)); MessageIdProvider.EnsureMessageIdIsInitialized(context.Envelope.Headers); await next(context).ConfigureAwait(false); }
public Task HandleAsync(ProducerPipelineContext context, ProducerBehaviorHandler next) { lock (_outboundEnvelopes) { _outboundEnvelopes.Add(context.Envelope); } return(next(context)); }
/// <inheritdoc cref="IProducerBehavior.HandleAsync" /> public async Task HandleAsync(ProducerPipelineContext context, ProducerBehaviorHandler next) { Check.NotNull(context, nameof(context)); Check.NotNull(next, nameof(next)); _mappings?.Apply(context.Envelope.Headers); await next(context).ConfigureAwait(false); }
/// <inheritdoc cref="IProducerBehavior.HandleAsync" /> public Task HandleAsync(ProducerPipelineContext context, ProducerBehaviorHandler next) { Check.NotNull(context, nameof(context)); Check.NotNull(next, nameof(next)); _integrationSpy.AddOutboundEnvelope(context.Envelope); return(next(context)); }
/// <inheritdoc cref="IProducerBehavior.HandleAsync" /> public async Task HandleAsync(ProducerPipelineContext context, ProducerBehaviorHandler next) { Check.NotNull(context, nameof(context)); Check.NotNull(next, nameof(next)); // TODO: Could avoid? Or make it optional (for interop with legacy versions)? MessageIdProvider.EnsureMessageIdIsInitialized(context.Envelope.Headers); await next(context).ConfigureAwait(false); }
/// <inheritdoc cref="IProducerBehavior.HandleAsync" /> public async Task HandleAsync(ProducerPipelineContext context, ProducerBehaviorHandler next) { Check.NotNull(context, nameof(context)); Check.NotNull(next, nameof(next)); HeaderAttributeHelper.GetHeaders(context.Envelope.Message) .ForEach(header => context.Envelope.Headers.AddOrReplace(header.Name, header.Value)); await next(context).ConfigureAwait(false); }
/// <inheritdoc cref="IProducerBehavior.HandleAsync" /> public async Task HandleAsync(ProducerPipelineContext context, ProducerBehaviorHandler next) { Check.NotNull(context, nameof(context)); Check.NotNull(next, nameof(next)); string key = GetKafkaKey(context); context.Envelope.Headers.AddOrReplace(KafkaMessageHeaders.KafkaMessageKey, key); await next(context).ConfigureAwait(false); }
private Task ExecutePipelineIfNeededAsync( ProducerPipelineContext context, ProducerBehaviorHandler finalAction) { if (context.Envelope is ProcessedOutboundEnvelope) { return(finalAction(context)); } return(ExecutePipelineAsync(context, finalAction)); }
/// <inheritdoc cref="IProducerBehavior.HandleAsync" /> public async Task HandleAsync(ProducerPipelineContext context, ProducerBehaviorHandler next) { Check.NotNull(context, nameof(context)); Check.NotNull(next, nameof(next)); using var activity = ActivitySources.StartProduceActivity(context.Envelope); _activityEnricherFactory .GetActivityEnricher(context.Envelope.Endpoint) .EnrichOutboundActivity(activity, context); await next(context).ConfigureAwait(false); }
/// <inheritdoc cref="IProducerBehavior.HandleAsync" /> public async Task HandleAsync(ProducerPipelineContext context, ProducerBehaviorHandler next) { Check.NotNull(context, nameof(context)); Check.NotNull(next, nameof(next)); foreach (var enricher in context.Envelope.Endpoint.MessageEnrichers) { enricher.Enrich(context.Envelope); } await next(context).ConfigureAwait(false); }
/// <inheritdoc cref="IProducerBehavior.HandleAsync" /> public async Task HandleAsync(ProducerPipelineContext context, ProducerBehaviorHandler next) { Check.NotNull(context, nameof(context)); Check.NotNull(next, nameof(next)); context.Envelope.RawMessage ??= await context.Envelope.Endpoint.Serializer.SerializeAsync( context.Envelope.Message, context.Envelope.Headers, new MessageSerializationContext(context.Envelope.Endpoint)) .ConfigureAwait(false); await next(context).ConfigureAwait(false); }
/// <inheritdoc cref="IProducerBehavior.HandleAsync" /> public async Task HandleAsync(ProducerPipelineContext context, ProducerBehaviorHandler next) { Check.NotNull(context, nameof(context)); Check.NotNull(next, nameof(next)); if (context.Envelope is OutboundEnvelope outboundEnvelope) { outboundEnvelope.ActualEndpointName = outboundEnvelope.Endpoint.GetActualName( outboundEnvelope, context.ServiceProvider); } await next(context).ConfigureAwait(false); }
/// <inheritdoc cref="IProducerBehavior.HandleAsync" /> public async Task HandleAsync(ProducerPipelineContext context, ProducerBehaviorHandler next) { Check.NotNull(context, nameof(context)); Check.NotNull(next, nameof(next)); if (context.Envelope.Endpoint.Encryption != null && context.Envelope.RawMessage != null) { context.Envelope.RawMessage = _streamFactory.GetEncryptStream( context.Envelope.RawMessage, context.Envelope.Endpoint.Encryption); } await next(context).ConfigureAwait(false); }
/// <inheritdoc cref="IProducerBehavior.HandleAsync" /> public async Task HandleAsync(ProducerPipelineContext context, ProducerBehaviorHandler next) { Check.NotNull(context, nameof(context)); Check.NotNull(next, nameof(next)); if (context.Envelope.Message != null) { var key = RabbitRoutingKeyHelper.GetRoutingKey(context.Envelope.Message); if (key != null) { context.Envelope.Headers.AddOrReplace(RabbitMessageHeaders.RoutingKey, key); } } await next(context).ConfigureAwait(false); }
/// <inheritdoc cref="IProducerBehavior.HandleAsync" /> public async Task HandleAsync(ProducerPipelineContext context, ProducerBehaviorHandler next) { Check.NotNull(context, nameof(context)); Check.NotNull(next, nameof(next)); if (context.Envelope.Message is IBinaryFileMessage && !(context.Envelope.Endpoint.Serializer is BinaryFileMessageSerializer)) { context.Envelope.RawMessage = await _binaryFileMessageSerializer.SerializeAsync( context.Envelope.Message, context.Envelope.Headers, MessageSerializationContext.Empty) .ConfigureAwait(false); } await next(context).ConfigureAwait(false); }
/// <inheritdoc cref="IProducerBehavior.HandleAsync" /> public async Task HandleAsync(ProducerPipelineContext context, ProducerBehaviorHandler next) { Check.NotNull(context, nameof(context)); Check.NotNull(next, nameof(next)); var activity = new Activity(DiagnosticsConstants.ActivityNameMessageProducing); try { activity.Start(); activity.SetMessageHeaders(context.Envelope.Headers); await next(context).ConfigureAwait(false); } finally { activity.Stop(); } }
/// <inheritdoc cref="IProducerBehavior.HandleAsync" /> public async Task HandleAsync(ProducerPipelineContext context, ProducerBehaviorHandler next) { Check.NotNull(context, nameof(context)); Check.NotNull(next, nameof(next)); if (context.Envelope.Endpoint is KafkaProducerEndpoint kafkaProducerEndpoint) { var partition = kafkaProducerEndpoint.GetPartition(context.Envelope, context.ServiceProvider); if (partition != Partition.Any) { context.Envelope.Headers.AddOrReplace( KafkaMessageHeaders.KafkaPartitionIndex, partition.Value); } } await next(context).ConfigureAwait(false); }
/// <inheritdoc cref="IProducerBehavior.HandleAsync" /> public async Task HandleAsync(ProducerPipelineContext context, ProducerBehaviorHandler next) { Check.NotNull(context, nameof(context)); Check.NotNull(next, nameof(next)); if (context.Envelope.Message != null && context.Envelope.Endpoint.MessageValidationMode != MessageValidationMode.None) { (bool isValid, string?validationErrors) = MessageValidator.CheckMessageIsValid( context.Envelope.Message, context.Envelope.Endpoint.MessageValidationMode); if (!isValid) { _logger.LogInvalidMessageProduced(validationErrors !); } } await next(context).ConfigureAwait(false); }
/// <inheritdoc cref="IProducerBehavior.HandleAsync" /> public async Task HandleAsync(ProducerPipelineContext context, ProducerBehaviorHandler next) { Check.NotNull(context, nameof(context)); Check.NotNull(next, nameof(next)); if (context.Envelope.Endpoint.Encryption != null && context.Envelope.RawMessage != null) { context.Envelope.RawMessage = _streamFactory.GetEncryptStream( context.Envelope.RawMessage, context.Envelope.Endpoint.Encryption); if (context.Envelope.Endpoint.Encryption is SymmetricEncryptionSettings settings && settings.KeyIdentifier != null) { context.Envelope.Headers.AddOrReplace( DefaultMessageHeaders.EncryptionKeyId, settings.KeyIdentifier); } } await next(context).ConfigureAwait(false); }
private async Task ExecutePipelineAsync( ProducerPipelineContext context, ProducerBehaviorHandler finalAction, int stepIndex = 0) { if (_behaviors.Count > 0 && stepIndex < _behaviors.Count) { await _behaviors[stepIndex].HandleAsync( context, nextContext => ExecutePipelineAsync(nextContext, finalAction, stepIndex + 1)) .ConfigureAwait(false); } else { await finalAction(context).ConfigureAwait(false); _logger.LogInformationWithMessageInfo( IntegrationEventIds.MessageProduced, "Message produced.", context.Envelope); } }
/// <inheritdoc cref="IProducerBehavior.HandleAsync" /> public async Task HandleAsync(ProducerPipelineContext context, ProducerBehaviorHandler next) { Check.NotNull(context, nameof(context)); Check.NotNull(next, nameof(next)); if (context.Envelope.Message is not Tombstone) { context.Envelope.RawMessage ??= await context.Envelope.Endpoint.Serializer.SerializeAsync( context.Envelope.Message, context.Envelope.Headers, new MessageSerializationContext(context.Envelope.Endpoint)) .ConfigureAwait(false); } else if (context.Envelope.Message.GetType().GenericTypeArguments.Length == 1) { context.Envelope.Headers.AddOrReplace( DefaultMessageHeaders.MessageType, context.Envelope.Message.GetType().GenericTypeArguments[0].AssemblyQualifiedName); } await next(context).ConfigureAwait(false); }
public Task HandleAsync(ProducerPipelineContext context, ProducerBehaviorHandler next) => next(context);
public Task HandleAsync(ProducerPipelineContext context, ProducerBehaviorHandler next) { context.Envelope.Headers.Remove(DefaultMessageHeaders.MessageType); return(next(context)); }