[프로메테우스] Pushgateway

91Savage·2022년 11월 3일
0

Server

목록 보기
11/24

Pushgateway

  • Prometheus 를 push 방식으로 사용하기 위해 필요
  • 단발성 작업 혹은 배치성 작업에 해당하는 메트릭 수집 용도

  • pushgateway 설치
docker run -d --net=host --name pushgateway prom/pushgateway:v1.4.1
  • 포트 확인
ss -nltp
  • 접속 확인
localhost:9091
  • pushgateway.yml 파일생성 및 내용 복사
scrape_configs:
      - job_name: 'pushgateway'
        follow_redirects: false
        scrape_interval: 2s
        scrape_timeout: 1s
        static_configs:
        - targets:
          - localhost:9091
  • pushgateway로 데이터 전송
- 파일로부터 전송할 데이터를 불러오는것 , stdin으로부터 data를 받아오겠다는 말 

echo "test_metric 1" | curl --data-binary @- http://localhost:9091/metrics/job/test_job

localhost:9091 들어가서 데이터 확인
  • error 테스트
echo "test_metric 1 1" | curl --data-binary @- http://localhost:9091/metrics/job/test_job

위처럼 "failed!" 경고창과 [push_failure_time_seconds]에 value 가 생김

  • 현재 동작중인 pushgateway 정보와 설치 시 옵션 나옴
    http://localhost:9091/#

  • 다른 label 들 입력해보기

echo "test_metric 1" | curl --data-binary @- http://localhost:9091/metrics/job/test_job/instance/batch-server
echo "test_metric 1" | curl --data-binary @- http://localhost:9091/metrics/job/test_job/instance/batch-server/region/kr
(region=kr 이라는 label이 새로 추가됨)
  • pushgateway에 저장된 데이터 삭제
curl -XDELETE http://localhost:9091/metrics/job/test_job/instance/batch-server/region/kr
curl -XDELETE http://localhost:9091/metrics/job/test_job/

삭제하려면 모든레이블이 일치하게끔 작성해서 DELETE 해줘야 함.

[batch 코드 실습]

  • batch 작업을 하고 실패하면 -1을 metric 값으로 전달하고 성공하면 batch작업을 실행하는데 걸린시간을 반환

폴더 생성 후 cd $폴더
npm init -y
npm i waait
npm i prom-client

worker.js 파일 생성

const wait = require('waait');
const { Registry, Pushgateway, Gauge } = require('prom-client');

const register = new Registry()
const gauge = new Gauge({
    name:'batch_process_time_second',
    help:'Time taken for batch job to complete',
    registers : [register],
})

async function batchJob(){
    if (Math.random() < 0.3) throw new Error('batch job failed')
    else {
        await wait(2*Math.random() * 1000)
    }
}

const main = (async function() {
    try{
        const startTime = Date.now()
        await batchJob()
        gauge.set((Date.now()- startTime) / 1000)
    }catch (e){
        gauge.set(-1)
    }
    const gateway = new Pushgateway('http://localhost:9091', [], register)
    return gateway.push({
        jobName:'batch',
        groupings: { instance: 'batch-server'}
    })
})()


node worker.js 실행
pushgateway 확인

0개의 댓글