3장. 계측
이 작품은 AI를 사용하여 번역되었습니다. 여러분의 피드백과 의견을 환영합니다: translation-feedback@oreilly.com
Prometheus를 통해 얻을 수 있는 가장 큰 혜택은 직접 계 측과 클라이언트 라이브러리를 사용하여 자체 애플리케이션을 계측하는 것입니다. 클라이언트 라이브러리는 다양한 언어로 제공되며, 공식 클라이언트 라이브러리는 Go, Python, Java, Rust 및 Ruby로 제공됩니다.
여기서는 Python 3을 예로 사용했지만 구문과 유틸리티 방법은 다르지만 다른 언어와 런타임에도 동일한 일반 원칙이 적용됩니다.
대부분의 최신 OS에는 Python 3이 함께 제공됩니다. 아직 설치되어 있지 않다면 Python 3을 다운로드하여 설치하세요.
또한 최신 Python 클라이언트 라이브러리를 설치해야 합니다. 이 작업은 pip install prometheus_client. 계측 예제는 GitHub에서 찾을 수 있습니다.
간단한 프로그램
먼저예제 3-1에 표시된 간단한 HTTP 서버를 작성했습니다. Python 3으로 실행한 다음 브라우저에서http://localhost:8001/ 을 방문하면 Hello World 응답이 표시됩니다.
예 3-1. Prometheus 메트릭도 노출하는 간단한 Hello World 프로그램
importhttp.serverfromprometheus_clientimportstart_http_serverclassMyHandler(http.server.BaseHTTPRequestHandler):defdo_GET(self):self.send_response(200)self.end_headers()self.wfile.write(b"Hello World")if__name__=="__main__":start_http_server(8000)server=http.server.HTTPServer(('localhost',8001),MyHandler)server.serve_forever()
start_http_server(8000) 은 포트 8000에서 HTTP 서버를 시작하여 Prometheus에 메트릭을 제공합니다. 이러한 메트릭은 그림 3-1과 같이 http://localhost:8000/ 에서 볼 수 있습니다. 기본적으로 반환되는 메트릭은 플랫폼에 따라 다르며, Linux 플랫폼에 가장 많은 메트릭이 있는 경향이 있습니다.
그림 3-1. 간단한 프로그램이 Linux에서 CPython으로 실행되는 경우의 /metrics 페이지
metrics 페이지를 수동으로 검토할 수도 있지만, 실제로 원하는 것은 메트릭을 Prometheus로 가져오는 것입니다. 이렇게 하려면 예제 3-2의 구성으로 Prometheus를 설정하고 실행하세요.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access