본문 바로가기
Back Side/etc

모니터링 시스템 구축하기 (1) prometheus

by developerBeluga 2023. 7. 6.
728x90
반응형

 

 

 

 

 

모니터링 시스템의 필요성

서비스를 운영하는 사람이라면 모니터링 시스템을 구축해야 한다.

작은 서비스일 경우 '굳이'라는 말을 할 수 있고 그럴 수 있다.

하지만 성능을 모니터하고 장애 대응의 중요성을 알고 있는 사람이라면 '당연히'라고 말할 것이다.

 

 

 

Prometheus

왜 모니터링 시스템을 구축해야하는지 알아봤다.

그렇다면 어떻게 구축해야 할까?

prometheus를 이용하면 손쉽게 모니터링 할 수 있는 메트릭을 수집할 수 있다.

 

 

 

실습

1) docker-compose에 prometheus 

  prometheus:
    image: prom/prometheus:v2.29.1
    ports:
      - 9090:9090
    volumes:
      - ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
      - prometheus_storage:/prometheus
    command:
      - --config.file=/etc/prometheus/prometheus.yml

우선 docker-compose에 해당 코드를 작성해준다.

여기에서 주목해야하는 것은 volumes에 첫번째 줄이다.

 

설정파일이 될 prometheus.yml 파일이 proemtheus > prometheus.yml에 위치해야한다.

다른 위치에 있을 경우 꼭 수정해주자.

 

 

2) prometheus.yml 작성해주기

global:
  scrape_interval:     15s 
  evaluation_interval: 15s 

scrape_configs:
  - job_name: 'proxy'
    metrics_path: '/metrics'
    static_configs:
      - targets: ['ip:port']

여기에서 가장 중요한 건 targets다.

ip:port에는 메트릭을 수집할 서비스의 주소를 입력해야 한다.

ip엔 컨테이너 이름을 써줘도 된다.(ex. proxy:3002)

 

 

 

3) 해당 서비스에 엔드포인트 추가하기

const counter = new client.Counter({
  name: 'http_requests_total',
  help: 'Total number of HTTP requests',
  labelNames: ['method', 'code'],
});

app.use((req, res, next) => {
  if (req.path !== '/metrics') {
    res.on('finish', () => {
      counter.labels(req.method, res.statusCode).inc();
    });
  }
  next();
});

app.get('/metrics', async (req, res) => {
  res.set('Content-Type', client.register.contentType);
  res.send(await client.register.metrics());
});

해당 서비스에 /metrics를 만들어줘야지 prometheus가 메트릭을 수집할 수 있다. 

 

잘 수집했는지 확인하기 위해서는 뚫어준 서비스의 ip:port/metrics를 들어가보면 위 캡쳐와 같은 문자열이 보인다.

 

아니면 ip:9090에 접속해보면 prometheus 웹이 보인다.

status > targets에 들어가면 해당 서비스가 잘 동작하는지 알 수 있다.

뭔가 이상하다면 이곳에서 원인(Error에 로그 찍힘)을 찾으면 된다.

 

 

문제점

실제로 prometheus 하나로 수집하려고 하니 문제점을 만났다.

그것은 바로 모든 서비스에 엔드포인트를 뚫어야 한다는거다.

proemtheus는 엔드포인트를 뚫어주지 않으면 제대로 해당 서비스의 메트릭을 수집할 수 없다.

 

하나, 둘이면 괜찮지만 현재 우리 회사만 해도 5개 넘는 서비스를 운영하고 있다.

모든 서비스에 똑같은 코드를 기입하고 뚫는게 상당히 비효율적으로 느껴졌다 🤔

 

그에 대한 해결방법을 찾았는데 그건 2편에서~

 

 

 

 

 

 

 

728x90
반응형

댓글