<%#
 Copyright 2013-2019 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
apiVersion: <%= KUBERNETES_CORE_API_VERSION  %>
kind: ConfigMap
metadata:
  name: logstash-config
  namespace: <%= kubernetesNamespace %>
data:
  logstash.conf: |-
    input {
        udp {
            port => "${INPUT_UDP_PORT}"
            type => syslog
            codec => json
        }
        tcp {
            port => "${INPUT_TCP_PORT}"
            type => syslog
            codec => json_lines
        }
        http {
            port => "${INPUT_HTTP_PORT}"
            codec => "json"
        }
    }

    filter {
        if [logger_name] =~ "metrics" {
            kv {
                source => "message"
                field_split_pattern => ", "
                prefix => "metric_"
            }
            mutate {
                convert => { "metric_value" => "float" }
                convert => { "metric_count" => "integer" }
                convert => { "metric_min" => "float" }
                convert => { "metric_max" => "float" }
                convert => { "metric_mean" => "float" }
                convert => { "metric_stddev" => "float" }
                convert => { "metric_median" => "float" }
                convert => { "metric_p75" => "float" }
                convert => { "metric_p95" => "float" }
                convert => { "metric_p98" => "float" }
                convert => { "metric_p99" => "float" }
                convert => { "metric_p999" => "float" }
                convert => { "metric_mean_rate" => "float" }
                convert => { "metric_m1" => "float" }
                convert => { "metric_m5" => "float" }
                convert => { "metric_m15" => "float" }
                # No need to keep message field after it has been parsed
                remove_field => ["message"]
            }
        }
        if [type] == "syslog" {
            mutate {
                add_field => { "instance_name" => "%{app_name}-%{host}:%{app_port}" }
            }
        }
        mutate {
            # workaround from https://github.com/elastic/logstash/issues/5115
            add_field => { "[@metadata][LOGSTASH_DEBUG]" => "${LOGSTASH_DEBUG:false}" }
        }
    }

    output {
        if [logger_name] =~ "metrics" {
            elasticsearch {
                hosts => ["${ELASTICSEARCH_HOST}:${ELASTICSEARCH_PORT}"]
                index => "metrics-%{+YYYY.MM.dd}"
            }
        } else {
            elasticsearch {
                hosts => ["${ELASTICSEARCH_HOST}:${ELASTICSEARCH_PORT}"]
                index => "logs-%{+YYYY.MM.dd}"
            }
        }
        if [@metadata][LOGSTASH_DEBUG] == "true" {
            stdout {
                codec => rubydebug
            }
        }
    }
