문서 메뉴

문서 홈애플리케이션 개발MongoDB 드라이버Ruby MongoDB Driver

모니터링

이 페이지의 내용

  • 명령 모니터링
  • 서버 검색 및 모니터링
  • 서버 하트비트
  • 연결 풀 및 연결 모니터링
  • 모니터링 비활성화
  • 제외된 이벤트 및 삭제된 이벤트

드라이버를 사용하면 특정 이벤트가 발생할 때 애플리케이션이 알림을 받을 수 있습니다. 이러한 이벤트는 다음 카테고리로 구성됩니다.

  • 명령 모니터링

  • 토폴로지 라이프사이클

  • 서버 수명 주기

  • 서버 하트비트

  • 연결 풀 및 연결

토폴로지 및 서버 이벤트는 SDAM(서버 검색 및 모니터링)의 일부입니다.

서버로 전송되는 모든 사용자 개시 명령은 구독하여 세분화된 정보를 얻을 수 있는 이벤트를 게시합니다. 모니터링 API는 각 명령에 대해 보장된 시작 이벤트를 게시한 다음 성공 또는 실패 이벤트를 게시합니다. 구독자는 started, succeeded, failed 의 3가지 메서드를 구현해야 하며, 각 메서드는 이벤트에 대해 단일 매개변수를 사용합니다. 다음은 드라이버에서 내부적으로 사용하는 로깅 구독자를 기반으로 하는 로깅 구독자의 예시입니다.

class CommandLogSubscriber
include Mongo::Loggable
def started(event)
# The default inspection of a command which is a BSON document gets
# truncated in the middle. To get the full rendering of the command, the
# ``to_json`` method can be called on the document.
log_debug("#{prefix(event)} | STARTED | #{format_command(event.command.to_json)}")
end
def succeeded(event)
log_debug("#{prefix(event)} | SUCCEEDED | #{event.duration}s")
end
def failed(event)
log_debug("#{prefix(event)} | FAILED | #{event.message} | #{event.duration}s")
end
private
def logger
Mongo::Logger.logger
end
def format_command(args)
begin
args.inspect
rescue Exception
'<Unable to inspect arguments>'
end
end
def format_message(message)
format("COMMAND | %s".freeze, message)
end
def prefix(event)
"#{event.address.to_s} | #{event.database_name}.#{event.command_name}"
end
end

사용자 지정 구독자를 등록하려면 모든 클라이언트에 대해 전역적으로 또는 클라이언트별로 등록할 수 있습니다.

subscriber = CommandLogSubscriber.new
Mongo::Monitoring::Global.subscribe(Mongo::Monitoring::COMMAND, subscriber)
client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'test' )
client.subscribe( Mongo::Monitoring::COMMAND, subscriber )

샘플 출력:

D, [2018-09-23T13:47:31.258020 #4692] DEBUG -- : COMMAND | 127.0.0.1:27027 | test.hello | STARTED | {"hello"=>1, "$readPreference"=>{"mode"=>"primary"}, "lsid"=>{"id"=><BSON::Binary:0x47111693353080 type=uuid data=0x730341e880dc40a2...>}}
D, [2018-09-23T13:47:31.259145 #4692] DEBUG -- : COMMAND | 127.0.0.1:27027 | test.hello | SUCCEEDED | 0.000791175s

Ruby 드라이버는 서버 검색 및 모니터링(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".freeze, 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 이벤트를 구독하는 것은 클라이언트 구성 중에 이벤트가 게시될 수 있으므로 약간 더 복잡합니다.

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)

샘플 출력:

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.

참고

:sdam_proc 클라이언트 옵션은 구성 중에 옵션이 제공되는 클라이언트에만 적용됩니다. Client#with 호출을 통해 특정 클라이언트 옵션이 변경되면 드라이버가 기본 이벤트 구독자 집합을 사용하여 새 cluster를 생성할 수 있습니다. 이 경우 제공된 :sdam_proc 이(가) 호출되지 않으며 애플리케이션에서 이벤트를 놓칠 수 있습니다.

애플리케이션은 SERVER_HEARTBEAT 주제를 구독하여 각 서버 하트비트에 대한 알림을 받을 수 있습니다. 서버 하트비트 리스너는 started, succeededfailed 의 세 가지 메서드를 구현해야 합니다. 각 하트비트는 리스너에서 started 메서드를 호출한 다음 하트비트 결과에 따라 succeeded 또는 failed 메서드를 호출합니다.

모든 하트비트 이벤트에는 하트비트가 전송된 서버의 주소가 포함되어 있습니다. 성공 및 실패한 이벤트에는 hello 또는 레거시 hello 명령의 왕복 시간이 포함됩니다. 실패 이벤트에는 hello 또는 레거시 hello 명령 실행 중에 발생한 인스턴스도 포함됩니다. 이벤트 속성에 대한 자세한 내용은 ServerHeartbeatStarted, ServerHeartbeatSucceeded 및 ServerHeartbeatFailed에 대한 API 문서를 참조하세요.

다음은 하트비트 이벤트 구독자 로깅의 예시입니다.

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".freeze, message)
end
end

명령 이벤트와 마찬가지로 애플리케이션은 전역적으로 또는 특정 클라이언트에 대해 하트비트 이벤트를 구독할 수 있습니다.

subscriber = HeartbeatLogSubscriber.new
Mongo::Monitoring::Global.subscribe(Mongo::Monitoring::SERVER_HEARTBEAT, subscriber)
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

MongoDB 4.2 이하 서버에 연결된 경우, Ruby 드라이버는 기본적으로 :heartbeat_frequency 초마다 하트비트를 발행하며(Ruby 클라이언트 옵션), 하트비트는 겹치지 않습니다(하트비트의 성공한 이벤트는 시작된 이벤트보다 먼저 게시됩니다). 다음 하트비트가 게시됨). MongoDB 4.4 이상 서버에 연결된 경우 드라이버는 여러 모니터링 스레드와 서버 상태 변화를 더 빠르게 감지하도록 설계된 보다 복잡한 하트비트 프로토콜을 사용합니다. 결과적으로 하트비트 이벤트 간격이 더 고르지 않고 하트비트 이벤트가 겹칠 수 있습니다. 특히 대기 중인 하트 비트는 대기 중인 하트비트 가 진행 중인 동안 시작되거나 완료될 수 있으며, 그 반대의 경우도 마찬가지입니다. ServerHeartbeatStarted#awaited?, ServerHeartbeatSucceeded#awaited?ServerHeartbeatFailed#awaited? 메서드를 사용하여 대기 중이 아닌 하트비트와 대기된 하트비트를 구분합니다.

클라이언트가 작업을 수행하려고 할 때 적절한 서버가 없는 경우 배포서버가 더 자주 스캔되며 각 서버는 최대 500밀리초마다 폴링될 수 있습니다. 애플리케이션이 특정 서버의 수동 스캔을 요청할 수도 있습니다. 드라이버는 스캔 사이에 최소 500밀리초 간격을 적용합니다.

각 클라이언트는 배포 내 각 서버에 대한 연결 풀을 인식하고 유지 관리하고 연결 풀과 개별 연결 모두에 대한 이벤트를 게시합니다. 이러한 이벤트를 구독하려면 게시 중인 이벤트에 대한 단일 매개 변수를 사용하는 pubished 메서드를 구현하는 구독자 클래스를 정의합니다. 이후 버전의 드라이버에서는 이 메커니즘을 통해 추가 이벤트가 게시될 수 있습니다.

다음 이벤트는 현재 CMAP 사양 에 따라 드라이버에 의해 구현됩니다. :

  • 풀 생성

  • 풀 지우기

  • 풀 마감

  • connectionCreated 이벤트

  • connectionReady

  • 연결종료

  • connectionCheckOutStarted 이벤트

  • connectionCheckOutFailed

  • ConnectionCheckOutSucceeded

  • connectionCheckedIn

드라이버는 모든 연결 풀 및 연결 관련 이벤트를 기록하는 데 사용할 수 있는 로깅 구독자를 제공합니다. 이 구독자는 애플리케이션에서 수행하는 각 작업에 대한 로그 항목을 생성하기 때문에 기본적으로 활성화되지 않습니다. 이 구독자를 전역적으로 또는 클라이언트별로 활성화하려면 다음을 수행합니다.

Mongo::Monitoring::Global.subscribe(
Mongo::Monitoring::CONNECTION_POOL,
Mongo::Monitoring::CmapLogSubscriber.new)
client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'test' )
subscriber = Mongo::Monitoring::CmapLogSubscriber.new
client.subscribe( Mongo::Monitoring::CONNECTION_POOL, subscriber )

샘플 출력:

D, [2019-05-06T17:23:21.595412 #8576] DEBUG -- : MONGODB | EVENT: #<PoolCreated address=127.0.0.1:27741 options={...}>
D, [2019-05-06T17:23:21.595584 #8576] DEBUG -- : MONGODB | EVENT: #<PoolCleared address=127.0.0.1:27741>
D, [2019-05-06T17:23:21.603549 #8576] DEBUG -- : MONGODB | EVENT: #<PoolCreated address=localhost:27741 options={...}>
D, [2019-05-06T17:23:21.603616 #8576] DEBUG -- : MONGODB | EVENT: #<ConnectionCheckOutStarted address=localhost:27741>
D, [2019-05-06T17:23:21.603684 #8576] DEBUG -- : MONGODB | EVENT: #<ConnectionCreated address=localhost:27741 connection_id=1>
D, [2019-05-06T17:23:21.604079 #8576] DEBUG -- : MONGODB | EVENT: #<ConnectionCheckedOut address=localhost:27741 connection_id=1>
D, [2019-05-06T17:23:21.605759 #8576] DEBUG -- : MONGODB | EVENT: #<ConnectionReady address=localhost:27741 connection_id=1>
D, [2019-05-06T17:23:21.605784 #8576] DEBUG -- : MONGODB | EVENT: #<ConnectionCheckedIn address=localhost:27741 connection_id=1>
D, [2019-05-06T17:23:21.605817 #8576] DEBUG -- : MONGODB | EVENT: #<PoolCleared address=localhost:27741>
D, [2019-05-06T17:23:21.605852 #8576] DEBUG -- : MONGODB | EVENT: #<ConnectionClosed address=localhost:27741 connection_id=1 reason=stale>

모니터링을 끄려면 클라이언트 모니터링 옵션을 false 로 설정합니다.

client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'test', :monitoring => false )

Ruby 드라이버는 명령 모니터링 메커니즘을 통해 일부 이벤트를 게시하지 않으며 때때로 수정합니다.

  1. 명령이 수정된 명령의 특정 하위 집합에 속하거나 페이로드 수정을 trigger 키를 포함하는 경우 보안상의 이유로 빈 페이로드가 제공됩니다. 전체 페이로드는 MONGO_RUBY_DRIVER_UNREDACT_EVENTS 환경 변수를 1, true 또는 yes 로 설정하여 액세스할 수 있습니다. 다음 명령이 삭제됩니다:

    • authenticate

    • saslStart

    • saslContinue

    • getnonce

    • createUser

    • updateUser

    • copydbgetnonce

    • copydbsaslstart

    • copydb

  2. 명령이 핸드셰이크 명령( ismaster 또는 hello)인 경우 모니터링하지 않는 연결에서 이벤트가 전혀 게시되지 않습니다.

  3. 모니터링 연결을 통해 전송된 명령(예: ismaster 및 hello)은 명령 모니터링 이벤트를 게시하지 않습니다. 대신 서버를 확인할 때마다 서버 하트비트 이벤트가 게시됩니다. 서버 하트비트 이벤트에는 명령 또는 회신 페이로드가 포함되지 않습니다.

  4. 명령이 핸드셰이크 명령이고 speculativeAuthenticate 옵션이 true 인 경우 명령이 삭제되고 빈 페이로드가 제공됩니다.

← 인증