Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rabbitmq output plugin : dynamic values on headers not working #41

Open
MarineW29 opened this issue Jan 8, 2021 · 1 comment
Open

Comments

@MarineW29
Copy link

I would like to set dynamic values to headers in properties of the message published in RabbitMQ, but the field are not replaces with their values.

I saw the feature has been added in 7.1.0 version on RabbitMQ integration plugin : #27 (comment) but it doesn't seem to work.

According to this discussion, it seems that the code sprintf the top-level items in message_properties (such as app_id) but does not iterate over the items nested inside them.

Version of the plugin I use :

    bash-4.2$ logstash-plugin list --verbose
    (...)
    logstash-integration-rabbitmq (7.1.1)
     ├── logstash-input-rabbitmq
     └── logstash-output-rabbitmq

My configuration :

    output {
      rabbitmq {
        id => "mwx-rabbitmq-output"
        host =>"rabbitmq"
        port => "5672"
        user => "***"
        password => "***"
        ssl => "false"
        vhost => "/"
        durable => true 
        automatic_recovery => true
        persistent => true
        exchange => "sys-logstash-out"
        exchange_type => "topic"
        key => "app-%{[properties][app-id]}-default"
        # Fill the payload with "message" field :
        codec => plain { format => "%{[message]}" }
        message_properties => {
          "content_type" => "application/json"
          "priority" => 2
          "app_id" => "%{[properties][app-id]}"
          "headers" => {
            "test" => "%{[message]}"
          }
        }
      }
    }

The resulting message in RabbitMQ :

    Exchange          sys-logstash-out
    Routing Key	      app-app-test-MWX-default
    Redelivered       ●
    Properties	
       app_id:        app-test-MWX
       priority:      2
       delivery_mode: 2
       headers:	
          test:       %{[message]}
       content_type:  application/json
    Payload           Test header 1

As you can see :

  • "%{[message]}" exists and is well replaced in payload part.
  • the "app_id" property is also OK
  • but the "test" header is not OK

Thanks.

@DIFRIN
Copy link

DIFRIN commented Feb 2, 2021

constant_properties = template.reject { |_,v| templated?(v) }
With this headers attributs of message_properties will fall all time in the constant_properties because it's not of type string

A Solution could be to extract constant_properties[:headers] then do the same as the variable_properties and reseting it in constant_properties before freezing. Something like

`def buildHeaders(event)

      if @constant_properties[:headers]

        constant_headers = @constant_properties[:headers].reject { |_, v| templated?(v) }
        variable_headers = @constant_properties[:headers].select { |_, v| templated?(v) }

        headers = variable_headers.each_with_object(constant_headers.dup) do |(k, v), memo|
          memo.store(k, event.sprintf(v))
        end

        @constant_properties[:headers] = headers
        @constant_properties.freez
      end
    end`

@yaauie can you help us fix this issue ? Thank you in advance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants