## Validate using https://www.otelbin.io/#distro=adot%7E&distroVersion=v0.43.1%7E .
##
## Original copyright is Apache 2.0 to AWS (aws-observability on GitHub).
## https://github.com/aws-observability/aws-otel-collector/blob/14833d4af543da709c77cf9dc6827351dbd529b1/config/ecs/container-insights/otel-task-metrics-config.yaml
##
## This is a modified version of `./etc/ecs/container-insights/otel-task-metrics-config.yaml`
## where we add `resource_to_telemetry_conversion` so the otel resource `service.name`
## can be added to the metric. This is useful because you can filter in metrics based on service,
## instead of grouping e.g. all services' memory usage under the same metric.
##
## Search for `# THIS IS WHAT LIFLIG ADDED` to find our modifications.
##
##
## @see https://aws-otel.github.io/docs/setup/ecs/config-through-ssm
extensions:
  health_check:

## These ports are also set in `taskDefinition.defaultContainer` as
## `OTEL_EXPORTER_OTLP_ENDPOINT` and so on.
## Changes must happen in both places.
receivers:
  otlp:
    protocols:
      grpc:
        endpoint: 0.0.0.0:4317
      http:
        endpoint: 0.0.0.0:4318
  awsxray:
    endpoint: 0.0.0.0:2000
    transport: udp
  awsecscontainermetrics:

processors:
  batch/traces:
    timeout: 1s
    send_batch_size: 50
  batch/metrics:
    timeout: 60s
  filter:
    metrics:
      include:
        match_type: strict
        metric_names:
          - ecs.task.memory.reserved
          - ecs.task.memory.utilized
          - ecs.task.cpu.reserved
          - ecs.task.cpu.utilized
          - ecs.task.network.rate.rx
          - ecs.task.network.rate.tx
          - ecs.task.storage.read_bytes
          - ecs.task.storage.write_bytes
          - container.duration
  metricstransform:
    transforms:
      - include: ecs.task.memory.utilized
        action: update
        new_name: MemoryUtilized
      - include: ecs.task.memory.reserved
        action: update
        new_name: MemoryReserved
      - include: ecs.task.cpu.utilized
        action: update
        new_name: CpuUtilized
      - include: ecs.task.cpu.reserved
        action: update
        new_name: CpuReserved
      - include: ecs.task.network.rate.rx
        action: update
        new_name: NetworkRxBytes
      - include: ecs.task.network.rate.tx
        action: update
        new_name: NetworkTxBytes
      - include: ecs.task.storage.read_bytes
        action: update
        new_name: StorageReadBytes
      - include: ecs.task.storage.write_bytes
        action: update
        new_name: StorageWriteBytes
  ## THIS IS WHAT LIFLIG ADDED
  filter/application:
    error_mode: ignore
    metrics:
      metric:
        - 'resource.attributes["http.target"] == "/health"'
        - 'resource.attributes["net.peer.name"] == "169.254.170.2"' # EC2 Metadata Endpoint
  filter/traces:
    error_mode: ignore
    traces:
      span:
        - 'attributes["http.route"] == "health"'
        - 'attributes["http.url"] == "/health"'
        - 'name == "GET health"'
  attributes/application:
    actions:
      # Http adds lots of high cardinality, which xray creates 1 metric per. Expensive.
      - key: http.url
        action: delete
      - key: http.target
        action: delete
      - key: http.client_ip
        action: delete
      - key: http.response_content_length
        action: delete
      - key: http.request_content_length
        action: delete
      - key: net.host.name
        action: delete
      - key: net.sock.peer.addr
        action: delete
      - key: net.sock.peer.port
        action: delete
      - key: net.sock.host.addr
        action: delete
      - key: net.sock.host.port
        action: delete
      - key: user_agent.original
        action: delete
  resource/application:
    attributes:
      - key: cloud.provider
        action: delete
      - key: host.arch
        action: delete
      - key: aws.ecs.container.image.id
        action: delete
      - key: aws.ecs.task.arn
        action: delete
      - key: aws.log.stream.names
        action: delete
      - key: aws.log.stream.arns
        action: delete
      - key: cloud.platform
        action: delete
      - key: container.name
        action: delete
      - key: process.executable.path
        action: delete
      - key: process.runtime.version
        action: delete
      - key: telemetry.auto.version
        action: delete
      - key: telemetry.sdk.name
        action: delete
      - key: container.id
        action: delete
      - key: container.image.tag
        action: delete
      - key: process.runtime.name
        action: delete
      - key: service.namespace
        action: delete
      - key: telemetry.sdk.version
        action: delete
      - key: aws.ecs.task.family
        action: delete
      - key: aws.ecs.task.revision
        action: delete
      - key: aws.log.group.arns
        action: delete
      - key: container.image.name
        action: delete
      - key: os.description
        action: delete
      - key: os.type
        action: delete
      - key: process.runtime.description
        action: delete
      - key: version
        action: delete
      - key: aws.ecs.launchtype
        action: delete
      - key: aws.log.group.names
        action: delete
      - key: host.name
        action: delete
      - key: process.pid
        action: delete
      - key: telemetry.sdk.language
        action: delete
      - key: aws.ecs.container.arn
        action: delete
      - key: service.version
        action: delete
      - key: process.command_args
        action: delete
      - key: service
        action: delete
  ## END OF LIFLIG CHANGES
  resource:
    attributes:
      - key: ClusterName
        from_attribute: aws.ecs.cluster.name
        action: insert
      - key: aws.ecs.cluster.name
        action: delete
      - key: ServiceName
        from_attribute: aws.ecs.service.name
        action: insert
      - key: aws.ecs.service.name
        action: delete
      ## THIS IS WHAT LIFLIG ADDED (bunch of sporadic removals)
      - key: aws.ecs.task.id
        action: delete
      - key: TaskDefinitionFamily
        from_attribute: aws.ecs.task.family
        action: insert
      - key: aws.ecs.task.family
        action: delete
      - key: aws.ecs.task.arn
        action: delete
      - key: aws.ecs.docker.name
        action: delete
      - key: aws.ecs.task.version
        action: delete
      - key: aws.ecs.task.pull_started_at
        action: delete
      - key: aws.ecs.task.pull_stopped_at
        action: delete
      - key: AvailabilityZone
        from_attribute: cloud.zone
        action: insert
      - key: cloud.zone
        action: delete
      - key: aws.ecs.task.launch_type
        action: delete
      - key: Region
        from_attribute: cloud.region
        action: insert
      - key: cloud.region
        action: delete
      - key: AccountId
        from_attribute: cloud.account.id
        action: insert
      - key: cloud.account.id
        action: delete
      - key: container.id
        action: delete
      - key: container.name
        action: delete
      - key: container.image.name
        action: delete
      - key: aws.ecs.container.image.id
        action: delete
      - key: aws.ecs.container.exit_code
        action: delete
      - key: aws.ecs.container.created_at
        action: delete
      - key: aws.ecs.container.started_at
        action: delete
      - key: aws.ecs.container.finished_at
        action: delete
      - key: container.image.tag
        action: delete
      ## END OF LIFLIG CHANGES

exporters:
  awsxray:
    ## THIS IS WHAT LIFLIG ADDED
    indexed_attributes: ["otel.resource.service.name"] # Max 50 attributes
    ## END OF LIFLIG CHANGES
  awsemf/application:
    namespace: ECS/AWSOTel/Application
    log_group_name: '/aws/ecs/application/metrics'
    ## THIS IS WHAT LIFLIG ADDED
    ## Config docs at https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/exporter/awsemfexporter/README.md
    ## log retention is days to keep the emf metric logs.
    log_retention: 365
    resource_to_telemetry_conversion:
      enabled: true
    dimension_rollup_option: NoDimensionRollup
    ## No rollup means we keep all the dimensions on the metric, instead of merging similar metrics to one in CloudWatch
    ## END OF LIFLIG CHANGES
  awsemf/performance:
    namespace: ECS/ContainerInsights
    log_group_name: '/aws/ecs/containerinsights/{ClusterName}/performance'
    log_stream_name: '{TaskId}'
    resource_to_telemetry_conversion:
      enabled: true
    dimension_rollup_option: NoDimensionRollup
    metric_declarations:
      - dimensions: [ [ ClusterName ], [ ClusterName, TaskDefinitionFamily ] ]
        metric_name_selectors:
          - MemoryUtilized
          - MemoryReserved
          - CpuUtilized
          - CpuReserved
          - NetworkRxBytes
          - NetworkTxBytes
          - StorageReadBytes
          - StorageWriteBytes
      - metric_name_selectors: [container.*]

service:
  pipelines:
    traces:
      receivers: [otlp,awsxray]
      ## THIS IS WHAT LIFLIG ADDED
      processors: [filter/traces, batch/traces]
      exporters: [awsxray]
    metrics/application:
      receivers: [otlp]
      processors: [filter/application, resource/application, attributes/application, batch/metrics]
      exporters: [awsemf/application]
      ## END OF LIFLIG CHANGES
    metrics/performance:
      receivers: [awsecscontainermetrics ]
      processors: [filter, metricstransform, resource]
      exporters: [ awsemf/performance ]

  extensions: [health_check]
