theangryangel / logstash-output-jdbc

JDBC output for Logstash
MIT License
256 stars 101 forks source link

String Expansion / Fields as Parameters #122

Closed cawoodm closed 6 years ago

cawoodm commented 6 years ago

Is there any way to parameterize the connection string with %{[myfield]}?

Say we have a metadata field called dbserver we'd like to do this:

connection_string => "jdbc:sqlserver://%{[@metadata][dbserver]}:1433;databaseName=HybrisMonitoring;user=reporting;password=Hyb!Prod5SQL"

Without this you need if {} statements to separate data into different databases (e.g. for PROD and TEST environments).

theangryangel commented 6 years ago

There are 2 ways you can do this already;

  1. (And my favourite) - using your deployment tool's templating system to generate different files each environment
  2. Logstash itself has support for using environment variables in configurations https://www.elastic.co/guide/en/logstash/current/environment-variables.html
theangryangel commented 6 years ago

Sorry I just re-read your question.

The connection is established and a pool of connections is maintained before any events come through. A connection is not made per-event, so it's impossible to use any fields in an event to alter the connection string.

cawoodm commented 6 years ago

So, if we use the if statement approach with 2 jdbc output plugins it should still work? Would be a pity if the connection pools co-mingled...

if [@metadata][system] == "prod" {
  jdbc {
    driver_jar_path => 'D:\prg\cmd\mssql-jdbc-6.4.0.jre8.jar'
    connection_string => "jdbc:sqlserver://prodserver:1433;databaseName=foo;user=reporting;password=****"
    statement => [ "EXEC LogImport ?, ?", "timestamp", "message"]
  }
} else {
  jdbc {
    driver_jar_path => 'D:\prg\cmd\mssql-jdbc-6.4.0.jre8.jar'
    connection_string => "jdbc:sqlserver://devserver:1433;databaseName=foo;user=reporting;password=****"
    statement => [ "EXEC LogImport ?, ?", "timestamp", "message"]
  }
}
theangryangel commented 6 years ago

The pools wont co-mingle in that example.

Each jdbc{} statement gets treated by logstash as a separate entity and you'll get separate connection pools :)