Example #1
0
        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);
            }
        }
Example #2
0
        private static string GetKafkaKey(ProducerPipelineContext context)
        {
            string?kafkaKeyHeaderValue = context.Envelope.Headers.GetValue(KafkaMessageHeaders.KafkaMessageKey);

            if (kafkaKeyHeaderValue != null)
            {
                return(kafkaKeyHeaderValue);
            }

            string?keyFromMessage = KafkaKeyHelper.GetMessageKey(context.Envelope.Message);

            if (keyFromMessage != null)
            {
                return(keyFromMessage);
            }

            var messageIdHeaderValue = context.Envelope.Headers.GetValue(DefaultMessageHeaders.MessageId);

            if (messageIdHeaderValue != null)
            {
                return(messageIdHeaderValue);
            }

            return(Guid.NewGuid().ToString());
        }
Example #3
0
        /// <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);
        }
Example #4
0
        /// <inheritdoc cref="IBrokerActivityEnricher.EnrichOutboundActivity" />
        public void EnrichOutboundActivity(Activity activity, ProducerPipelineContext producerContext)
        {
            Check.NotNull(activity, nameof(activity));
            Check.NotNull(producerContext, nameof(producerContext));

            SetMessageId(activity, producerContext.Envelope.BrokerMessageIdentifier);
            SetMessageKey(activity, producerContext.Envelope.Headers);
        }
Example #5
0
        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));
        }
Example #8
0
        /// <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);
        }
Example #9
0
        /// <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);
        }
Example #10
0
        private Task ExecutePipelineIfNeededAsync(
            ProducerPipelineContext context,
            ProducerBehaviorHandler finalAction)
        {
            if (context.Envelope is ProcessedOutboundEnvelope)
            {
                return(finalAction(context));
            }

            return(ExecutePipelineAsync(context, finalAction));
        }
Example #11
0
        /// <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);
        }
        /// <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));

            if (context.Envelope is OutboundEnvelope outboundEnvelope)
            {
                outboundEnvelope.ActualEndpointName = outboundEnvelope.Endpoint.GetActualName(
                    outboundEnvelope,
                    context.ServiceProvider);
            }

            await next(context).ConfigureAwait(false);
        }
Example #14
0
        /// <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));

            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.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));

            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));

            var activity = new Activity(DiagnosticsConstants.ActivityNameMessageProducing);

            try
            {
                activity.Start();
                activity.SetMessageHeaders(context.Envelope.Headers);
                await next(context).ConfigureAwait(false);
            }
            finally
            {
                activity.Stop();
            }
        }
Example #19
0
        /// <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);
        }
Example #22
0
        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 void EnrichOutboundActivity(Activity activity, ProducerPipelineContext producerContext)
 {
     // Do nothing
 }
        public Task HandleAsync(ProducerPipelineContext context, ProducerBehaviorHandler next)
        {
            context.Envelope.Headers.Remove(DefaultMessageHeaders.MessageType);

            return(next(context));
        }
Example #26
0
 public Task HandleAsync(ProducerPipelineContext context, ProducerBehaviorHandler next) => next(context);