apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-promtail-gerrit-configmap
  labels:
    app.kubernetes.io/component: gerrit-replica
    app.kubernetes.io/instance: {{ .Release.Name }}
    chart: {{ template "gerrit-replica.chart" . }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
    {{- if .Values.additionalLabels }}
{{ toYaml .Values.additionalLabels  | indent 4 }}
    {{- end }}
data:
  promtail.yaml: |-
    positions:
      filename: /var/gerrit/logs/promtail-positions.yaml

    client:
        tls_config:
          insecure_skip_verify: {{ .Values.promtailSidecar.tls.skipVerify }}
          {{- if not .Values.promtailSidecar.tls.skipVerify }}
          ca_file: /etc/promtail/promtail.ca.crt
          {{- end }}
        basic_auth:
          username: {{ .Values.promtailSidecar.loki.user }}
          password_file: /etc/promtail/promtail.secret
    scrape_configs:
    - job_name: gerrit_error
      static_configs:
      - targets:
        - localhost
        labels:
          job: gerrit_error
          __path__: /var/gerrit/logs/error_log.json
      entry_parser: raw
      pipeline_stages:
      - json:
          expressions:
            timestamp: '"@timestamp"'
            message:
      - template:
          source: timestamp
          template: {{`'{{ Replace .Value "," "." 1 }}'`}}
      - template:
          source: timestamp
          template: {{`'{{ Replace .Value "Z" " +0000" 1 }}'`}}
      - template:
          source: timestamp
          template: {{`'{{ Replace .Value "T" " " 1 }}'`}}
      - timestamp:
          source: timestamp
          format: "2006-01-02 15:04:05.999 -0700"
      - regex:
          source: message
          expression: "Gerrit Code Review (?P<gerrit_version>.*) ready"
      - labels:
          gerrit_version:
    - job_name: gerrit_httpd
      static_configs:
      - targets:
        - localhost
        labels:
          job: gerrit_httpd
          __path__: /var/gerrit/logs/httpd_log.json
      entry_parser: raw
      pipeline_stages:
      - json:
          expressions:
            timestamp: null
      - template:
          source: timestamp
          template: {{`'{{ Replace .Value "," "." 1 }}'`}}
      - timestamp:
          format: 02/Jan/2006:15:04:05.999 -0700
          source: timestamp
    - job_name: gerrit_sshd
      static_configs:
      - targets:
        - localhost
        labels:
          job: gerrit_sshd
          __path__: /var/gerrit/logs/sshd_log.json
      entry_parser: raw
      pipeline_stages:
      - json:
          expressions:
            timestamp:
      - template:
          source: timestamp
          template: {{`'{{ Replace .Value "," "." 1 }}'`}}
      - timestamp:
          source: timestamp
          format: 2006-01-02 15:04:05.999 -0700
