# outputs.conf

module(load="omhttp")
module(load="omstdout")

## TEMPLATES ##

# Schema: "event-flatten:2"
# Expects variables:
# - $!data
# Caller sets all subfields.
template(name="tpl_event_flatten_v2" type="list") {
  constant(value="{\"schema_type\":\"json-schema\",\"schema_id\":\"event-flatten:2\",")
  constant(value="\"data\":") property(name="$!data")
  constant(value="}\n")
}

# Funnel Metric schema.
# Expects variables:
# - $!metric_service
# - $!metric_name
# - $!metric_tags
# - $!metric_value
# - $!metric_timestamp
template(name="tpl_funnel_metrics" type="list") {
  constant(value="{")
  property(name="$!metric_service" outname="service" format="jsonfr")     constant(value=",")
  constant(value="\"metricName\": [\"") property(name="$!metric_name")    constant(value="\"],")
  constant(value="\"tags\":")           property(name="$!metric_tags")    constant(value=",")
  constant(value="\"metricValue\":")    property(name="$!metric_value" format="json") constant(value=",")
  constant(value="\"timestamp\":")      property(name="$!metric_timestamp" format="json")
  constant(value="}\n")
}

template(name="tpl_echo" type="string" string="%msg%")

template(name="tpl_dynafile" type="list") {
  constant(value="/www/data/")
  property(name="$.dynadir")  constant(value="/")
  property(name="$.app_id")   constant(value=".")     # App ID or Drain Token.
  property(name="$.source")   constant(value=".")     # Source.
  property(name="$.dyno")     constant(value=".log")  # Dyno.
}

template(name="tpl_dynafile_metrics" type="list") {
  constant(value="/www/data/")
  property(name="$.dynadir")            constant(value="/")
  property(name="$!metric_service")     constant(value="/")
  property(name="$!metric_name")        constant(value="/")
  property(name="$!metric_tags!device") constant(value=".")
  property(name="$!metric_tags!source") constant(value=".")
  property(name="$!metric_tags!dyno")   constant(value=".log")
}

##############
## RULESETS ##
##############

ruleset(name="rs_stdout") {
  action(
    type="omstdout"
    name="action_logger_omstdout"
  )
}

ruleset(name="rs_funnel_dnr_logs"
  queue.size="125000"
  queue.minDequeueBatchSize="0"
  queue.minDequeueBatchSize.timeout="1000"
  queue.workerthreads="1"
) {
  set $.dynadir = "funnel-dnr";
  action(
    type="omfile"
    template="tpl_event_flatten_v2"
    dynafile="tpl_dynafile"
  )
  action(
    name="action_omhttp_funnel_dnr_logs"
    type="omhttp"
    template="tpl_event_flatten_v2"
    server="ajna-api.salesforce.com"
    serverport="443"
    checkpath="funnel/ping"
    restpath="v1/logs/v1/dnr/heroku/v1"
    restpathtimeout="3000"
    action.resumeInterval="1"
    action.resumeIntervalMax="30"
    httpcontenttype="application/vnd.kafka.json.v1+json"
    batch.format="jsonarray"
    batch="on"
    batch.maxsize="1024"
    batch.maxbytes="10000000"
    tls.cacert="/dev/shm/identity/ca/cacerts.pem"
    tls.mycert="/dev/shm/identity/client/certificates/client.pem"
    tls.myprivkey="/dev/shm/identity/client/keys/client-key.pem"
    usehttps="on"
    compress="on"
    retry="on"
    retry.ruleset="rs_funnel_dnr_logs_retry"
  )
}

ruleset(name="rs_funnel_dnr_logs_retry") {
  action(
    name="action_omhttp_funnel_dnr_logs_retry"
    type="omhttp"
    queue.size="1000"
    template="tpl_echo"
    server="ajna-api.salesforce.com"
    serverport="443"
    checkpath="funnel/ping"
    restpath="v1/logs/v1/dnr/heroku/v1"
    restpathtimeout="3000"
    action.resumeInterval="1"
    action.resumeIntervalMax="30"
    httpcontenttype="application/vnd.kafka.json.v1+json"
    batch.format="jsonarray"
    batch="on"
    batch.maxsize="1024"
    batch.maxbytes="10000000"
    tls.cacert="/dev/shm/identity/ca/cacerts.pem"
    tls.mycert="/dev/shm/identity/client/certificates/client.pem"
    tls.myprivkey="/dev/shm/identity/client/keys/client-key.pem"
    usehttps="on"
    compress="on"
    retry="on"
    retry.ruleset="rs_funnel_dnr_logs_retry"
  )
}

ruleset(
  name="rs_funnel_logs"
  queue.size="125000"
  queue.minDequeueBatchSize="0"
  queue.minDequeueBatchSize.timeout="1000"
  queue.workerthreads="1"
) {
  set $.dynadir = "funnel-logs";
  action(
    type="omfile"
    template="tpl_event_flatten_v2"
    dynafile="tpl_dynafile"
  )
  action(
    name="action_omhttp_funnel_logs"
    type="omhttp"
    template="tpl_event_flatten_v2"
    server="ajna-api.salesforce.com"
    serverport="443"
    checkpath="funnel/ping"
    restpath="v1/logs/v1"
    action.resumeInterval="1"
    action.resumeIntervalMax="30"
    httpcontenttype="application/vnd.kafka.json.v1+json"
    batch.format="jsonarray"
    batch="on"
    batch.maxsize="1024"
    batch.maxbytes="10000000"
    tls.cacert="/dev/shm/identity/ca/cacerts.pem"
    tls.mycert="/dev/shm/identity/client/certificates/client.pem"
    tls.myprivkey="/dev/shm/identity/client/keys/client-key.pem"
    usehttps="on"
    compress="on"
    retry="on"
    retry.ruleset="rs_funnel_logs_retry"
  )
}

ruleset(name="rs_funnel_logs_retry") {
  action(
    name="action_omhttp_funnel_logs_retry"
    type="omhttp"
    queue.size="1000"
    template="tpl_echo"
    server="ajna-api.salesforce.com"
    serverport="443"
    checkpath="funnel/ping"
    restpath="v1/logs/v1"
    action.resumeInterval="1"
    action.resumeIntervalMax="30"
    httpcontenttype="application/vnd.kafka.json.v1+json"
    batch.format="jsonarray"
    batch="on"
    batch.maxsize="1024"
    batch.maxbytes="10000000"
    tls.cacert="/dev/shm/identity/ca/cacerts.pem"
    tls.mycert="/dev/shm/identity/client/certificates/client.pem"
    tls.myprivkey="/dev/shm/identity/client/keys/client-key.pem"
    usehttps="on"
    compress="on"
    retry="on"
    retry.ruleset="rs_funnel_logs_retry"
  )
}

# Ruleset to publish metrics to Funnel with expected scoping tags.
# Sends 2 metrics for each call to maintain backwards compatibility with current scopes.
# 1. "<service>.<SHORT_REGION>.HEROKU.<app_name>"
# 2. "<service>.EXT.HEROKU.<app_name>"
# TODO: Remove (2) after some amount of time.
# Reference https://salesforce.quip.com/PKsXAasFQQ8R
ruleset(name="rs_funnel_metrics") {
  reset $!metric_tags!datacenter = "-";
  reset $!metric_tags!superpod = "heroku";
  reset $!metric_tags!pod = "trust-nonprod-log-drain";
  call rs_funnel_metrics_internal

  reset $!metric_tags!datacenter = "ext";
  call rs_funnel_metrics_internal
}

# Internal ruleset to publish metrics to Funnel.
# Note! It's important that the omhttp action sets action.copymsg="on",
# since we call it multiple times for the same event.
ruleset(name="rs_funnel_metrics_internal") {
  set $.dynadir = "funnel-metrics";
  action(
    type="omfile"
    template="tpl_funnel_metrics"
    dynafile="tpl_dynafile_metrics"
  )
  action(
    name="action_omhttp_funnel_metrics"
    type="omhttp"
    action.copymsg="on"
    queue.size="10000"
    template="tpl_funnel_metrics"
    server="ajna-api.salesforce.com"
    serverport="443"
    checkpath="funnel/ping"
    restpath="v1/metrics/v2"
    restpathtimeout="3000"
    action.resumeInterval="1"
    action.resumeIntervalMax="30"
    httpcontenttype="application/json"
    batch.format="jsonarray"
    batch="on"
    batch.maxsize="500"
    batch.maxbytes="1000000"
    tls.cacert="/dev/shm/identity/ca/cacerts.pem"
    tls.mycert="/dev/shm/identity/client/certificates/client.pem"
    tls.myprivkey="/dev/shm/identity/client/keys/client-key.pem"
    usehttps="on"
    compress="on"
    retry="off"
  )
}