Carlos Aguni

Highly motivated self-taught IT analyst. Always learning and ready to explore new skills. An eternal apprentice.


Prometheus Discovery Study

12 Nov 2022 »

Prometheus-ecs-sd Python

https://github.com/signal-ai/prometheus-ecs-sd/blob/master/discoverecs.py

- job_name: 'ecs-1m'
  scrape_interval: 1m
  file_sd_configs:
      - files:
            - /opt/prometheus-ecs/1m-tasks.json
  relabel_configs:
      - source_labels: [metrics_path]
        action: replace
        target_label: __metrics_path__
        regex: (.+)

python3 discovery.py --directory ./ export AWS_DEFAULT_REGION=us-east-1 for boto3.client

Starting...
Directory: "./"
Refresh interval: "60s"
Default scrape interval prefix: "1m"
Tags to convert to labels: []
Clusters to query: []

/usr/local/lib/python3.6/site-packages/boto3/compat.py:88: PythonDeprecationWarning: Boto3 will no longer support Python 3.6 starting May 30, 2022. To continue receiving service updates, bug fixes, and security updates please upgrade to Python 3.7 or later. More information can be found here: https://aws.amazon.com/blogs/developer/python-support-policy-updates-for-aws-sdks-and-tools/
  warnings.warn(warning, PythonDeprecationWarning)
task_cache 0 1 task_definition_cache 0 1 1 container_instance_cache 0 0 ec2_instance_cache 0 0 0
Targets: 1
Discovered Job: {'targets': ['172.31.28.205:8080'], 'labels': {'instance': '172.31.28.205:8080', 'job': 'taskA', 'metrics_path': '/metrics', 'ecs_task_id': '79be7361636b470abbed5d2d56a263c8', 'ecs_task_version': '9', 'ecs_cluster': 'clusterB'}}

cat 1m-tasks.json

[
    {
        "targets": [
            "172.31.28.205:8080"
        ],
        "labels": {
            "instance": "172.31.28.205:8080",
            "job": "taskA",
            "metrics_path": "/metrics",
            "ecs_task_id": "79be7361636b470abbed5d2d56a263c8",
            "ecs_task_version": "9",
            "ecs_cluster": "clusterB"
        }
    }
]

terraform ecs

must be environment variables

resource "aws_ecs_task_definition" "clusterAsvcAtask" {
  family = var.clusterAsvcAtask

  container_definitions = jsonencode( 
  [
    {
      "name": "pythonlocal",
      "image": "691262992979.dkr.ecr.us-east-1.amazonaws.com/pythonlocal",
      #"portMappings": [
      #  {
      #    "containerPort": 3000
      #  }
      #],
      "environment": [
        {
            "name": "PROMETHEUS",
            "value": "true",
        },
        {
            "name": "PROMETHEUS_PORT",
            "value": "8080",
        },
      ],
      #"dockerLabels": {
      #  "PROMETHEUS": "true",
      #  "PROMETHEUS_ENDPOINT": "8080",
      #},
      "logConfiguration": {
        "logDriver": "awslogs",
        "options": {
          "awslogs-region": "us-east-1",
          "awslogs-group": "/ecs/${var.clusterA}/${var.clusterAsvcA}",
          "awslogs-stream-prefix": "ecs"
        }
      }
    }
  ]
  )

./prometheus --config.file ./prometheus.yml

prometheus.yml

# my global config
global:
  scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
    - static_configs:
        - targets:
          # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: "prometheus"

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ["localhost:9090"]

  - job_name: 'ecs-15s'
    scrape_interval: 15s
    file_sd_configs:
        - files:
              - /root/1m-tasks.json
    relabel_configs:
        - source_labels: [metrics_path]
          action: replace
          target_label: __metrics_path__
          regex: (.+)

Prometheus-ecs-discovery Golang

https://github.com/teralytics/prometheus-ecs-discovery

scrape_configs:
- job_name: ecs
  file_sd_configs:
    - files:
      - /path/to/ecs_file_sd.yml
      refresh_interval: 10m
  # Drop unwanted labels using the labeldrop action
  metric_relabel_configs:
    - regex: task_arn
      action: labeldrop