{{ if .Values.otel.enabled }}
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: opentelemetry-collector
  namespace: argocd
  annotations:
    argocd.argoproj.io/sync-options: SkipDryRunOnMissingResource=true
  finalizers:
  - resources-finalizer.argocd.argoproj.io
spec:
  destination:
    namespace: otel
    server: 'https://kubernetes.default.svc'
  project: sys
  syncPolicy:
    managedNamespaceMetadata:
      labels:
        component: sys
    syncOptions:
      - CreateNamespace=true
      - ApplyOutOfSyncOnly=true
    {{- if .Values.otel.autosync }}
    automated:
      prune: true
      # selfHeal: true
    {{- end }}
  sources:
  - repoURL: '{{ .Values.cluster_config.manifests }}'
    path: {{ .Values.cluster_config.policies }}/otel
    targetRevision: HEAD
  - repoURL: 'https://open-telemetry.github.io/opentelemetry-helm-charts'
    targetRevision: {{ .Values.otel.version }}
    chart: opentelemetry-collector
    helm:
      values: |
        mode: deployment
        image:
          repository: otel/opentelemetry-collector-k8s
        config:
          receivers:
            prometheus/collector:
              config:
                scrape_configs:
                  - job_name: 'opentelemetry-collector'
                    static_configs:
                      - targets:
                         - ${env:MY_POD_IP}:8888
            zipkin:
              endpoint: ${env:MY_POD_IP}:9411
          exporters:
            otlp:
              endpoint: "tempo.tempo.svc:4317"
              tls:
                insecure: true
            ## 
            otlphttp/metrics:
              endpoint: http://prom-prometheus.prometheus:9090/api/v1/otlp
              tls:
                insecure: true
            otlphttp/logs:
              endpoint: http://loki-write-headless.loki:3100/otlp
              tls:
                insecure: true
            ## Detailed? make toggleable with values
            debug/metrics:
              verbosity: detailed
            debug/traces:
              verbosity: detailed
            debug/logs:
              verbosity: detailed
          service:
            telemetry:
              logs:
                level: "info"
            pipelines:
              traces:
                receivers: [otlp,zipkin]
                processors: [batch]
                exporters: [otlp]
                # exporters: [otlphttp/traces,debug/traces]
              metrics:
                receivers: [otlp,prometheus/collector]
                processors: [batch]
                exporters: [otlphttp/metrics]
                # exporters: [otlphttp/metrics,debug/metrics]
              logs:
                receivers: [otlp]
                processors: [batch]
                exporters: [otlphttp/logs]
                # exporters: [otlphttp/logs,debug/logs]
        ports:
          metrics:
            enabled: true
        # presets:
        #   logsCollection:
        #     enabled: true
        ingress:
          enabled: true
          ingressClassName: nginx
          annotations:
              cert-manager.io/cluster-issuer: {{ .Values.cluster_config.ingress_clusterissuer }}
              nginx.ingress.kubernetes.io/ssl-redirect: "true"
              {{- with .Values.cluster_config.ingress_whitelist_ips }}
              nginx.ingress.kubernetes.io/whitelist-source-range: {{ join "," . }}
              {{- end }}
          hosts:
            - host: opentelemetry-collector.{{ .Values.cluster_config.domain }}
              paths:
                - path: /
                  pathType: Prefix
                  port: 4318
          tls:
            - secretName: collector-tls
              hosts:
                - opentelemetry-collector.{{ .Values.cluster_config.domain }}
{{ end }}
