#
# Vector configuration for merging Netty AccessLogs with Gateway accesslogs
#

api:
  enabled: true
  address: 0.0.0.0:8686
sources:
  opentelemetry:
    type: opentelemetry
    grpc:
      address: 0.0.0.0:4317
    http:
      address: 0.0.0.0:4318
      headers: []
      keepalive:
        max_connection_age_jitter_factor: 0.1
        max_connection_age_secs: 300
transforms:
  sp_access_logs_filter:
    # Accept only access logs from Security proxy
    type: filter
    inputs:
      - opentelemetry.logs
    condition:
      type: "vrl"
      source: '.scope.name == "org.georchestra.security-proxy.accesslog"'
      # source: match!(.scope.name, r'org.georchestra.*.accesslog') 

  gw_access_logs_filter:
    # Accept only access logs from Gateway
    type: filter
    inputs:
      - opentelemetry.logs
    condition:
      type: "vrl"
      source: '.scope.name == "org.georchestra.gateway.accesslog" '
      # source: match!(.scope.name, r'org.georchestra.*.accesslog')

  netty_logs_filter:
    # Accept only access logs
    type: filter
    inputs:
      - opentelemetry.logs
    condition:
      type: "vrl"
      source: .scope.name == "reactor.netty.http.server.AccessLog"

  netty_log_pre_reduce:
    # Drop any fields that we won't be interested in when merging with accesslogs
    type: "remap"
    inputs:
      - netty_logs_filter
    source: |
      del(.attributes)
      del(.dropped_attributes_count)
      del(.flags)
      del(.observed_timestamp)
      del(.resources)
      del(.severity_number)
      del(.severity_text)
      del(.source_type)
      del(.timestamp)
      del(.trace_id)

  gateway_log_pre_reduce:
    # Drop any fields that we won't be interested in when merging with accesslogs
    type: "remap"
    inputs:
      - gw_access_logs_filter
    source: |
      del(.message)

  gw_log_reduce:
    # Combine GW access logs and matching netty accesslogs
    type: reduce
    inputs:
      - netty_log_pre_reduce
      - gateway_log_pre_reduce
    expire_after_ms: 500
    flush_period_ms: 100
    group_by:
      - span_id
    merge_strategies:
      message: concat_newline
      scope.name: concat
sinks:
  console:
    type: console
    # https://vector.dev/docs/reference/configuration/sinks/console/
    inputs:
      - gw_access_logs_filter
      # - sp_access_logs_filter
      # - opentelemetry.logs
    target: stdout
    encoding:
      codec: json
  tsdb:
    type: postgres
    # https://vector.dev/docs/reference/configuration/sinks/postgres/
    inputs:
#      - gw_access_logs_filter
      - gw_log_reduce
    endpoint: "postgres://${TSDB_USER}:${TSDB_PASSWORD}@${TSDB_HOST}:${TSDB_PORT}/${TSDB_NAME}"
    table: "${TSDB_OTEL_TABLE:-analytics.opentelemetry_buffer}"
    pool_size: 5