#
# ╔═╗┌─┐┌─┐┌┐┌╔╦╗┌─┐┬  ┌─┐┌┬┐┌─┐┌┬┐┬─┐┬ ┬
# ║ ║├─┘├┤ │││ ║ ├┤ │  ├┤ │││├┤  │ ├┬┘└┬┘
# ╚═╝┴  └─┘┘└┘ ╩ └─┘┴─┘└─┘┴ ┴└─┘ ┴ ┴└─ ┴
#
# OpenTelemetry Collector Configuration
#
# For more information, see:
# https://opentelemetry.io/docs/collector/configuration/
#

#═══════════════════════════════════════════════════
# INPUTS
#═══════════════════════════════════════════════════
receivers:
  # OTLP
  # Receives data via gRPC or HTTP using OTLP format. For additional information on the OTLP receiver:
  # https://github.com/open-telemetry/opentelemetry-collector/tree/main/receiver/otlpreceiver
  otlp:
    protocols:
      grpc:
        endpoint: localhost:4317
        tls:
          cert_file: ../security/otel.crt
          key_file: ../security/otel.key
        auth:
          authenticator: basicauth

  # prometheus
  # Polls the internal Prometheus exporter embedded with the OpenTelemetry Collector on port 8888 by default.
  # For additional information on the Prometheus receiver:
  # https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/receiver/prometheusreceiver
  # Simply add "prometheus/internal" to the pipeline in the receivers array, to export these internal metrics.
  prometheus/internal:
    config:
      scrape_configs:
        - job_name: otel-collector-internal
          scrape_interval: 60s
          static_configs:
            - targets: [ localhost:8888 ]

#═══════════════════════════════════════════════════
# PROCESSING
#═══════════════════════════════════════════════════
processors:

  # memory_limiter
  # Limits the memory usage of the collector. For additional information on the memory limiter:
  # https://github.com/open-telemetry/opentelemetry-collector/tree/main/processor/memorylimiterprocessor
  memory_limiter:
    check_interval: 1s
    limit_mib: 2000
    spike_limit_mib: 400

  # filter
  # Filters the metrics. Several filter configurations can be specified.
  # For additional information on filtering options:
  # https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/processor/filterprocessor
  # Add your filter entries (e.g. "filter/1") to the pipeline in the processors array to activate it.
  filter/keep1HostOnly:
    metrics:
      include:
        match_type: expr
        expressions:
        - Label("host.name") == "my-server.big-corp.com"

  # batch
  # Processes and sends data in batches of 10s. For additional information on the batch processor:
  # https://github.com/open-telemetry/opentelemetry-collector/tree/main/processor/batchprocessor
  batch:
    timeout: 10s

  # metricstransform
  # Post-processes the metrics before sending them out. For additional information on all possible actions:
  # https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/processor/metricstransformprocessor
  # Add "metricstransform" to the pipeline in the processors array to activate it.
  metricstransform:
    transforms:
      - include: hw_.*
        match_type: regexp
        action: update
        operations:
          - action: add_label
            new_label: prod_level
            new_value: production

  transform/datadog:
    metric_statements:
      - context: datapoint
        statements:
          - set(metric.name, Concat(["metricshub", metric.name], ".")) where not IsMatch(metric.name, "^(metricshub\\.|otelcol_)")

#═══════════════════════════════════════════════════
# OUTPUT
#═══════════════════════════════════════════════════
exporters:

  # BMC Helix
  # Using Prometheus Remote Write protocol.
  prometheusremotewrite/helix:
    # endpoint is the URL pointing to your Helix environment, at onbmc.com.
    # apiToken can be retrieved in BMC Helix Operations Management > Administration > Repository.
    endpoint: https://your-helix-env.onbmc.com/metrics-gateway-service/api/v1.0/prometheus
    headers:
      Authorization: Bearer <apiToken>
    resource_to_telemetry_conversion:
      enabled: true
    retry_on_failure:
      enabled: true
      max_elapsed_time: 5m
      initial_interval: 5s
      max_interval: 30s

  # Prometheus Server with Remote Write protocol
  # For additional information on all options:
  # https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/exporter/prometheusremotewriteexporter
  # Note: The Prometheus server must have been started with the --web.enable-remote-write-receiver option.
  prometheusremotewrite/your-server:
    endpoint: http://prom-server:9090/api/v1/write
    resource_to_telemetry_conversion:
      enabled: true
    retry_on_failure:
      enabled: true
      max_elapsed_time: 5m
      initial_interval: 5s
      max_interval: 30s

  # Datadog
  # For more information, visit: <https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/exporter/datadogexporter>
  datadog/api:
    api:
      key: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa # API Key
      # Specify the Datadog site you are using:
      # For US: datadoghq.com (default), us3.datadoghq.com, or us5.datadoghq.com (depending on your Datadog site location)
      # For Europe: datadoghq.eu
      # For Government sites: ddog-gov.com
      # For further details, refer to https://docs.datadoghq.com/getting_started/site/
      # site: datadoghq.eu  # Uncomment and update the site value if required
    metrics:
      resource_attributes_as_tags: true

  # debug
  # https://github.com/open-telemetry/opentelemetry-collector/tree/main/exporter/debugexporter
  debug:
    verbosity: detailed

  # New Relic (OTLP exporter)
  # https://github.com/open-telemetry/opentelemetry-collector/tree/main/exporter/otlpexporter
  otlp/newrelic:
    # endpoint is the URL pointing to the New Relic OTLP receiver.
    # Endpoints are region-specific, so use the one according to where your account is based.
    # For US OTLP: the <new-relic-endpoint-url> is https://otlp.nr-data.net:4318
    # For EU OTLP: the <new-relic-endpoint-url> is https://otlp.eu01.nr-data.net:4318
    # Refer to https://docs.newrelic.com/docs/more-integrations/open-source-telemetry-integrations/opentelemetry/get-started/opentelemetry-set-up-your-app/#review-settings for more details
    endpoint: https://otlp.eu01.nr-data.net:4318
    headers:
      # Configure the exporter to add the api-key header.
      # Replace <license_key> with your Account License Key available at https://one.newrelic.com/launcher/api-keys-ui.launcher
      api-key: <license_key>

  # Prometheus exporter
  # Exports data to a Prometheus back-end.
  # https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/exporter/prometheusexporter
  prometheus:
    endpoint: "0.0.0.0:24375"
    send_timestamps: true
    metric_expiration: 15m
    resource_to_telemetry_conversion:
      enabled: true

  # Splunk SignalFx
  # https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/exporter/signalfxexporter
  signalfx:
    # Access token to send data to SignalFx.
    access_token: <access_token>
    # SignalFx realm where the data will be received.
    realm: eu0
    # Timeout for the send operations.
    timeout: 10s
    # Defines if the exporter should scrape host metadata.
    sync_host_metadata: true

  # BMC Helix
  # https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/exporter/bmchelixexporter
  bmchelix:
    endpoint: https://company.onbmc.com # Replace with your BMC Helix endpoint
    api_key: <api-key>

#═══════════════════════════════════════════════════
# EXTRAS
#═══════════════════════════════════════════════════
extensions:

  # healthcheck
  # https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/extension/healthcheckextension
  health_check:

  # zPages
  # https://github.com/open-telemetry/opentelemetry-collector/tree/main/extension/zpagesextension
  zpages:

  # basicauth
  # https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/extension/basicauthextension
  basicauth:
    htpasswd:
      file: ../security/.htpasswd

#═══════════════════════════════════════════════════
# ACTUAL COLLECTOR PIPELINE DESCRIPTION
#═══════════════════════════════════════════════════
service:
  telemetry:
    logs:
      level: info # Change to debug for more details
    metrics:
      level: basic
      readers:
        - pull:
            exporter:
              prometheus:
                host: '0.0.0.0'
                port: 8888
  extensions: [health_check, basicauth]
  pipelines:
    # Main pipeline: OTLP --> batching --> Prometheus
    metrics:
      receivers: [otlp, prometheus/internal]
      processors: [memory_limiter, batch, metricstransform]
      exporters: [prometheusremotewrite/your-server, prometheus] # List here the platform of your choice

    # Dedicated Datadog pipeline: OTLP --> batching + prefixing --> Datadog
    # metrics/datadog:
    #   receivers: [otlp, prometheus/internal]
    #   processors: [memory_limiter, batch, transform/datadog]
    #   exporters: [datadog/api]

    # Uncomment the section below to enable traces for the MetricsHub Agent.
    # traces:
    #   receivers: [otlp]
    #   processors: [memory_limiter, batch]
    #   exporters: [debug, datadog/api] # List here the platforms on which you want to see the traces

    # Uncomment the section below to enable debug of alerts.
    # logs:
    #   receivers: [otlp]
    #   processors: [memory_limiter, batch]
    #   exporters: [debug] # List here the platform of your choice
