Docs Menu
Docs Home
/ / /
루비 드라이버
/

애플리케이션 이벤트 모니터링

이 가이드 에서는 MongoDB Ruby 드라이버 에서 모니터링 설정하다 하고 구성하는 방법을 학습 수 있습니다.

모니터링에는 애플리케이션 성능 관리 라이브러리와 함께 사용할 수 있는 실행 프로그램의 활동에 대한 정보를 수집하는 작업이 포함됩니다.

Ruby 운전자 모니터링하면 드라이버의 리소스 사용량 및 성능을 파악할 수 있습니다. 이 정보는 애플리케이션 설계하고 디버깅할 때 정보에 입각한 결정을 내리는 데 도움이 될 수 있습니다.

이 가이드 에서는 다음 작업을 수행하는 방법을 학습 수 있습니다.

  • 명령 이벤트 모니터링

  • 서버 검색 및 모니터링(SDAM) 이벤트 모니터링

  • 연결 풀 이벤트 모니터링

이 가이드 코드에서 운전자 의 활동에 대한 정보를 사용하는 방법을 보여줍니다. 운전자 에서 이벤트를 기록 방법을 학습하려면 Ruby 드라이버의 로깅 가이드를 참조하세요.

명령 이벤트는 MongoDB 데이터베이스 명령어와 관련된 이벤트입니다. 애플리케이션에서 이벤트를 구독하면 해당 드라이버를 사용하여 하나 이상의 명령 모니터링 이벤트에 액세스할 수 있습니다.

MongoDB database 명령에 대해 자세히 학습하려면 MongoDB Server 매뉴얼의 데이터베이스 명령 가이드를 참조하세요.

애플리케이션 에서 명령 이벤트를 구독 하여 명령 이벤트에 대한 세부 정보 액세스 할 수 있습니다. 클러스터 의 모든 클라이언트를 모니터링하는 글로벌 수준 또는 클라이언트 수준에서 이벤트를 구독 할 수 있습니다. 이 예시 다음 조치를 보여줍니다.

  • 다음을 인스턴스화합니다. CommandLogSubscriber

  • Mongo::Monitoring::Global.subscribe 메서드를 사용하여 글로벌 수준에서 명령 이벤트를 구독 .

  • Mongo::Client.subscribe 메서드를 사용하여 클라이언트 수준에서 명령 이벤트를 구독 .

require 'mongo'
# Creates a subscriber for command monitoring
subscriber = Mongo::Monitoring::CommandLogSubscriber.new
# Globally subscribes to command monitoring events
Mongo::Monitoring::Global.subscribe(Mongo::Monitoring::COMMAND, subscriber)
# Replace with your connection string and connect to your client
uri = '<connection string>'
client = Mongo::Client.new(uri)
# Subscribes to command monitoring events at the client level
client.subscribe( Mongo::Monitoring::COMMAND, subscriber)

다음 명령 모니터링 이벤트 중 하나를 구독할 수 있습니다.

이벤트 이름
설명

명령이 시작될 때 생성됩니다.

명령이 성공할 때 생성됩니다.

명령이 실패할 때 생성됩니다.

Ruby 운전자 연결한 인스턴스 또는 클러스터 의 상태 변경될 때 토폴로지 이벤트(SDAM 이벤트라고도 함)를 생성합니다. 예시 를 들어, 운전자 사용자가 새 연결을 설정하거나 클러스터 새 프라이머리 노드 선택할 때 이벤트 생성합니다.

토폴로지 이벤트에 대해 자세히 학습 서버 매뉴얼의 복제 가이드 참조하세요.

다음 섹션에서는 애플리케이션에서 토폴로지 변경 사항을 기록하고 이러한 이벤트에서 제공된 정보를 탐색하는 방법을 보여 줍니다.

Ruby 드라이버의 subscribe 메서드를 사용하여 이벤트를 구독 할 수 있습니다. 모니터링 이벤트 유형을 정의하는 모니터링 주제 와 인수로 구독자 객체 subscribe 메서드에 전달합니다. 클러스터 의 모든 클라이언트를 모니터링하는 글로벌 수준 또는 클라이언트 수준에서 이벤트를 구독 할 수 있습니다.

이 예시 다음 조치를 보여줍니다.

  • ServerOpeningLogSubscriber 구독자를 인스턴스화합니다.

  • Mongo::Monitoring::Global.subscribe 메서드를 사용하여 글로벌 수준에서 ServerOpening 이벤트를 구독 .

  • Mongo::Client.subscribe 메서드를 사용하여 클라이언트 수준에서 ServerOpening 이벤트를 구독 .

require 'mongo'
subscriber = Mongo::Monitoring::ServerOpeningLogSubscriber.new
# Globally subscribes to ServerOpening events by using the SERVER_OPENING monitoring topic
Mongo::Monitoring::Global.subscribe(Mongo::Monitoring::SERVER_OPENING, subscriber)
# Replace with your connection string and connect to your client
uri = '<connection string>'
client = Mongo::Client.new(uri)
# Subscribes to ServerOpening events at the client level by using the SERVER_OPENING monitoring topic
client.subscribe(Mongo::Monitoring::SERVER_OPENING, subscriber)

다음 표에는 사용 가능한 구독자와 해당 구독자의 모니터링 주제 나와 있습니다.

구독자 이름
모니터링 주제
설명

SERVER_CLOSED

ServerClosed 이벤트를 구독하고 기록합니다.

SERVER_DESCRIPTION_CHANGED

ServerDescriptionChanged 이벤트를 구독하고 기록합니다.

SERVER_OPENING

ServerOpening 이벤트를 구독하고 기록합니다.

TOPOLOGY_CHANGED

TopologyChanged 이벤트를 구독하고 기록합니다.

TOPOLOGY_CLOSED

TopologyClosed 이벤트를 구독하고 기록합니다.

TOPOLOGY_OPENING

TopologyOpening 이벤트를 구독하고 기록합니다.

이 페이지의 이벤트 설명 섹션에서 SDAM 이벤트 설명 표를 찾을 수 있습니다.

사용자 지정 SDAM 구독자를 생성하여 서버 및 토폴로지 이벤트에 대한 세부 정보에 액세스 할 수 있습니다. 각 이벤트 이벤트 에 대해 별도의 클래스를 만듭니다.

모든 이벤트에 대해 구독자는 succeeded 메서드를 호출하고 이벤트 인수로 전달합니다. 간단한 SDAM 로깅 구독자는 다음 코드와 유사할 수 있습니다.

class SDAMLogSubscriber
include Mongo::Loggable
def succeeded(event)
log_debug(format_event(event))
end
private
def logger
Mongo::Logger.logger
end
def format_message(message)
format("SDAM | %s", message)
end
end
class TopologyOpeningLogSubscriber < SDAMLogSubscriber
private
def format_event(event)
"Topology type '#{event.topology.display_name}' initializing."
end
end
class ServerOpeningLogSubscriber < SDAMLogSubscriber
private
def format_event(event)
"Server #{event.address} initializing."
end
end
class ServerDescriptionChangedLogSubscriber < SDAMLogSubscriber
private
def format_event(event)
"Server description for #{event.address} changed from " +
"'#{event.previous_description.server_type}' to '#{event.new_description.server_type}'."
end
end
class TopologyChangedLogSubscriber < SDAMLogSubscriber
private
def format_event(event)
if event.previous_topology != event.new_topology
"Topology type '#{event.previous_topology.display_name}' changed to " +
"type '#{event.new_topology.display_name}'."
else
"There was a change in the members of the '#{event.new_topology.display_name}' " +
"topology."
end
end
end
class ServerClosedLogSubscriber < SDAMLogSubscriber
private
def format_event(event)
"Server #{event.address} connection closed."
end
end
class TopologyClosedLogSubscriber < SDAMLogSubscriber
private
def format_event(event)
"Topology type '#{event.topology.display_name}' closed."
end
end

이벤트를 구독 하려면 적절한 구독자를 생성하고 올바른 모니터링 주제 를 구독 . 다음 코드는 SDAM 이벤트를 전역적으로 구독 방법을 보여줍니다.

topology_opening_subscriber = TopologyOpeningLogSubscriber.new
server_opening_subscriber = ServerOpeningLogSubscriber.new
server_description_changed_subscriber = ServerDescriptionChangedLogSubscriber.new
topology_changed_subscriber = TopologyChangedLogSubscriber.new
server_closed_subscriber = ServerClosedLogSubscriber.new
topology_closed_subscriber = TopologyClosedLogSubscriber.new
Mongo::Monitoring::Global.subscribe(Mongo::Monitoring::TOPOLOGY_OPENING,
topology_opening_subscriber)
Mongo::Monitoring::Global.subscribe(Mongo::Monitoring::SERVER_OPENING,
server_opening_subscriber)
Mongo::Monitoring::Global.subscribe(Mongo::Monitoring::SERVER_DESCRIPTION_CHANGED,
server_description_changed_subscriber)
Mongo::Monitoring::Global.subscribe(Mongo::Monitoring::TOPOLOGY_CHANGED,
topology_changed_subscriber)
Mongo::Monitoring::Global.subscribe(Mongo::Monitoring::SERVER_CLOSED,
server_closed_subscriber)
Mongo::Monitoring::Global.subscribe(Mongo::Monitoring::TOPOLOGY_CLOSED,
topology_closed_subscriber)

다음 코드는 sdam-proc 클라이언트 옵션을 사용하여 단일 클라이언트 의 SDAM 이벤트를 구독 방법을 보여줍니다.

topology_opening_subscriber = TopologyOpeningLogSubscriber.new
server_opening_subscriber = ServerOpeningLogSubscriber.new
server_description_changed_subscriber = ServerDescriptionChangedLogSubscriber.new
topology_changed_subscriber = TopologyChangedLogSubscriber.new
server_closed_subscriber = ServerClosedLogSubscriber.new
topology_closed_subscriber = TopologyClosedLogSubscriber.new
sdam_proc = Proc.new do |client|
client.subscribe(Mongo::Monitoring::TOPOLOGY_OPENING,
topology_opening_subscriber)
client.subscribe(Mongo::Monitoring::SERVER_OPENING,
server_opening_subscriber)
client.subscribe(Mongo::Monitoring::SERVER_DESCRIPTION_CHANGED,
server_description_changed_subscriber)
client.subscribe(Mongo::Monitoring::TOPOLOGY_CHANGED,
topology_changed_subscriber)
client.subscribe(Mongo::Monitoring::SERVER_CLOSED,
server_closed_subscriber)
client.subscribe(Mongo::Monitoring::TOPOLOGY_CLOSED,
topology_closed_subscriber)
end
client = Mongo::Client.new(['127.0.0.1:27017'], database: 'test',
sdam_proc: sdam_proc)

참고

:sdam_proc 클라이언트 옵션은 지정된 클라이언트 에만 적용됩니다. Client#with 호출을 사용하여 특정 클라이언트 옵션을 변경하면 운전자 기본값 이벤트 구독자 설정하다 사용하여 새 클러스터 만들 수 있습니다. 이 경우 제공된 :sdam_proc 가 호출되지 않으며 애플리케이션 이벤트를 놓칠 수 있습니다.

애플리케이션 을 실행 하면 구독자는 SDAM 이벤트 를 기록하고 다음과 같은 메시지를 출력합니다.

D, [2018-10-09T13:58:03.489461 #22079] DEBUG -- : SDAM | Topology type 'Unknown' initializing.
D, [2018-10-09T13:58:03.489699 #22079] DEBUG -- : SDAM | Server 127.0.0.1:27100 initializing.
D, [2018-10-09T13:58:03.491384 #22079] DEBUG -- : SDAM | Server description for 127.0.0.1:27100 changed from 'unknown' to 'unknown'.
D, [2018-10-09T13:58:03.491642 #22079] DEBUG -- : SDAM | Server localhost:27100 initializing.
D, [2018-10-09T13:58:03.493199 #22079] DEBUG -- : SDAM | Server description for localhost:27100 changed from 'unknown' to 'primary'.
D, [2018-10-09T13:58:03.493473 #22079] DEBUG -- : SDAM | Server localhost:27101 initializing.
D, [2018-10-09T13:58:03.494874 #22079] DEBUG -- : SDAM | Server description for localhost:27101 changed from 'unknown' to 'secondary'.
D, [2018-10-09T13:58:03.495139 #22079] DEBUG -- : SDAM | Server localhost:27102 initializing.
D, [2018-10-09T13:58:03.496504 #22079] DEBUG -- : SDAM | Server description for localhost:27102 changed from 'unknown' to 'secondary'.
D, [2018-10-09T13:58:03.496777 #22079] DEBUG -- : SDAM | Topology type 'Unknown' changed to type 'ReplicaSetNoPrimary'.
D, [2018-10-09T13:58:03.497306 #22079] DEBUG -- : SDAM | Server 127.0.0.1:27100 connection closed.
D, [2018-10-09T13:58:03.497606 #22079] DEBUG -- : SDAM | Topology type 'ReplicaSetNoPrimary' changed to type 'ReplicaSetWithPrimary'.
# client.close
D, [2018-10-09T13:58:05.342057 #22079] DEBUG -- : SDAM | Server localhost:27100 connection closed.
D, [2018-10-09T13:58:05.342299 #22079] DEBUG -- : SDAM | Server localhost:27101 connection closed.
D, [2018-10-09T13:58:05.342565 #22079] DEBUG -- : SDAM | Server localhost:27102 connection closed.
D, [2018-10-09T13:58:05.342693 #22079] DEBUG -- : SDAM | Topology type 'ReplicaSetWithPrimary' closed.

사용자 지정 구독자를 만들어 서버 모니터 서버 에 hello 명령을 보낼 때 발생하는 서버 하트비트를 모니터 할 수도 있습니다.

사용자 지정 서버 하트비트 구독자는 다음 세 가지 방법을 구현 해야 한다는 점에서 다른 SDAM 구독자와 다릅니다.

  • started: 리스너가 하트비트를 수신할 때 호출됩니다.

  • succeeded: 성공적인 하트비트 결과에 대한 응답

  • failed: 실패한 하트비트 결과에 대한 응답

다음 예시 하트비트 이벤트 구독자를 보여줍니다.

class HeartbeatLogSubscriber
include Mongo::Loggable
def started(event)
log_debug("#{event.address} | STARTED")
end
def succeeded(event)
log_debug("#{event.address} | SUCCEEDED | #{event.duration}s")
end
def failed(event)
log_debug("#{event.address} | FAILED | #{event.error.class}: #{event.error.message} | #{event.duration}s")
end
private
def logger
Mongo::Logger.logger
end
def format_message(message)
format("HEARTBEAT | %s", message)
end
end

다음 예시 와 같이 전역적으로 또는 특정 클라이언트 에 대해 하트비트 이벤트를 구독 할 수 있습니다.

subscriber = HeartbeatLogSubscriber.new
# Globally subscribes to Server Opening events
Mongo::Monitoring::Global.subscribe(Mongo::Monitoring::SERVER_HEARTBEAT, subscriber)
# Subscribes to Server Opening events at the client level
client = Mongo::Client.new([ '127.0.0.1:27017' ], database: 'test' )
client.subscribe( Mongo::Monitoring::SERVER_HEARTBEAT, subscriber )

애플리케이션 실행 하면 구독자는 하트비트 이벤트 기록하고 다음과 같은 메시지를 출력합니다.

D, [2018-09-23T13:44:10.707018 #1739] DEBUG -- : HEARTBEAT | 127.0.0.1:27027 | STARTED
D, [2018-09-23T13:44:10.707778 #1739] DEBUG -- : HEARTBEAT | 127.0.0.1:27027 | SUCCEEDED | 0.000772381s

다음 표에는 각 SDAM 이벤트 의 이름과 설명이 나와 있습니다.

eventType
설명

서버 인스턴스 가 닫힐 때 생성되는 이벤트입니다.

서버의 설명이 변경될 때 생성되는 이벤트입니다.

서버 하트비트가 실패할 때 생성되는 이벤트입니다.

리스너가 서버 하트비트를 수신할 때 생성되는 이벤트입니다.

서버 하트비트가 성공하면 생성되는 이벤트입니다.

운전자 서버 에 연결할 때 생성되는 이벤트입니다.

토폴로지 변경될 때 생성되는 이벤트입니다.

토폴로지 의 모든 인스턴스 연결이 닫힐 때 생성되는 이벤트입니다.

운전자 인스턴스 에 연결을 시도하기 전에 생성된 이벤트입니다.

연결 풀은 드라이버가 MongoDB 인스턴스를 사용하여 유지 관리하는 개방형 TCP 연결입니다. 연결 풀은 애플리케이션이 수행해야 하는 네트워크 핸드셰이크 수를 줄이고 애플리케이션을 더 빠르게 실행하는 데 도움이 될 수 있습니다.

다음 섹션에서는 애플리케이션에서 연결 풀 이벤트를 기록하고 이러한 이벤트에서 제공된 정보를 탐색하는 방법을 보여 줍니다.

애플리케이션 에서 모든 연결 풀 이벤트를 구독 하면 해당 이벤트에 대한 세부 정보 액세스 할 수 있습니다. 클러스터 의 모든 클라이언트를 모니터링하는 글로벌 수준 또는 클라이언트 수준에서 이벤트를 구독 할 수 있습니다.

이 예시 다음 조치를 보여줍니다.

  • CmapLogSubscriber 구독자를 인스턴스화합니다.

  • Mongo::Monitoring::Global.subscribe 메서드를 사용하여 글로벌 수준에서 모든 연결 풀 이벤트를 구독 .

  • Mongo::Client.subscribe 메서드를 사용하여 클라이언트 수준에서 모든 연결 풀 이벤트를 구독 .

require 'mongo'
# Creates a subscriber for connection pool monitoring
subscriber = Mongo::Monitoring::CmapLogSubscriber.new
# Globally subscribes to connection pool monitoring events
Mongo::Monitoring::Global.subscribe(Mongo::Monitoring::CONNECTION_POOL, subscriber)
# Replace with your connection string and connect to your client
uri = '<connection string>'
client = Mongo::Client.new(uri)
# Subscribes to connection pool monitoring events at the client level
client.subscribe(Mongo::Monitoring::CONNECTION_POOL, subscriber)

다음 연결 풀 모니터링 이벤트 중 하나를 구독할 수 있습니다:

이벤트 이름
설명

작업이 실행을 위한 연결을 획득하지 못할 때 생성됩니다.

작업이 실행을 위해 연결을 획득하려고 시도할 때 생성됩니다.

작업이 실행된 후 연결이 풀에 다시 체크인될 때 생성됩니다.

작업이 실행을 위한 연결을 성공적으로 획득할 때 생성됩니다.

연결이 닫힐 때 생성됩니다.

연결이 생성될 때 생성되지만 작업에 사용될 때 반드시 생성되지는 않습니다.

연결이 성공적으로 핸드셰이크를 완료하고 작업에 사용할 준비가 된 후에 생성됩니다.

이 가이드 에 설명된 클래스 또는 메서드에 학습 보려면 다음 API 설명서를 참조하세요.

돌아가기

Atlas Vector Search

이 페이지의 내용