# 1. Namespace for the collector
apiVersion: v1
kind: Namespace
metadata:
  name: monitoring
  labels:
    name: monitoring

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: otel-collector-config
  namespace: monitoring
data:
  collector-config.yaml: |
    receivers:
      otlp:
        protocols:
          grpc:
            endpoint: 0.0.0.0:4317
          http:
            endpoint: 0.0.0.0:4318

    processors:
      batch: {}
      resource:
        attributes:
          - key: k8s.namespace.name
            from_attribute: k8s.namespace.name
            action: upsert
          - key: k8s.pod.name
            from_attribute: k8s.pod.name
            action: upsert

      # Correct metricstransform processor configuration
      metricstransform:
        transforms:
          - include: arc_metrics_.*
            match_type: regexp
            action: update
            operations:
              - action: add_label
                new_label: metric_source
                new_value: otel_collector

      filter/drop_internal_metrics:
        metrics:
          exclude:
            match_type: regexp
            metric_names:
              - ^prometheus_.*
              - ^process_.*
              - ^go_.*

    exporters:
      prometheusremotewrite:
        endpoint: "<Applicare_Ip>:9090/api/v1/write"
        namespace: "arc_metrics"
        resource_to_telemetry_conversion:
          enabled: true

      debug:
        verbosity: detailed

      otlphttp:
        endpoint: "<Applicare_Ip>:4318"

    service:
      pipelines:
        metrics:
          receivers: [otlp]
          processors: [resource, metricstransform, filter/drop_internal_metrics, batch]
          exporters: [prometheusremotewrite]
        traces:
          receivers: [otlp]
          processors: [batch]
          exporters: [otlphttp]

---
apiVersion: v1
kind: Service
metadata:
  name: otel-collector
  namespace: monitoring
spec:
  selector:
    app: otel-collector
  ports:
    - name: prometheus
      protocol: TCP
      port: 8889
      targetPort: 8889
    - name: otlp-grpc
      protocol: TCP
      port: 4317
      targetPort: 4317
    - name: otlp-http
      protocol: TCP
      port: 4318
      targetPort: 4318

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: otel-collector
  namespace: monitoring
spec:
  replicas: 1
  selector:
    matchLabels:
      app: otel-collector
  template:
    metadata:
      labels:
        app: otel-collector
    spec:
      containers:
        - name: otel-collector
          image: otel/opentelemetry-collector-contrib:latest
          command:
            - "/otelcol-contrib"
            - "--config=/conf/collector-config.yaml"
          ports:
            - containerPort: 8889
            - containerPort: 4317
            - containerPort: 4318
          volumeMounts:
            - name: config
              mountPath: /conf
      volumes:
        - name: config
          configMap:
            name: otel-collector-config