개요
이 가이드 에서는 MongoDB 고 (Go) 드라이버 에서 모니터링 설정하다 하고 구성하는 방법을 학습 수 있습니다. 고 (Go) 운전자를 사용하여 애플리케이션에서 이벤트 를 구독하여 이벤트를 모니터할 수 있습니다.
모니터링에는 애플리케이션 성능 관리 라이브러리와 함께 사용할 수 있는 실행 프로그램의 활동에 대한 정보를 수집하는 작업이 포함됩니다.
이벤트 작동 중에 운전자 내에서 발생하는 모든 조치 입니다. 고 (Go) 운전자 이러한 이벤트의 하위 집합을 수신하는 기능이 포함되어 있습니다.
고 (Go) 운전자 모니터링하면 드라이버의 리소스 사용량 및 성능을 이해하고 애플리케이션 설계하고 디버깅할 때 정보에 입각한 결정을 내리는 데 도움이 될 수 있습니다.
이 가이드에서는 이와 같은 작업을 수행하는 방법애 대해 학습합니다.
이 가이드 코드에서 운전자 의 활동에 대한 정보를 사용하는 방법을 보여줍니다. 운전자 에서 이벤트를 기록 방법을 학습 고 (Go) 드라이버의 로깅 가이드 참조하세요.
명령 이벤트 모니터링
명령 이벤트 는 MongoDB database 명령과 관련된 이벤트 입니다. 애플리케이션 에서 운전자 사용하여 이벤트를 구독 하면 하나 이상의 명령 모니터링 이벤트에 액세스 할 수 있습니다.
MongoDB database 명령에 대해 자세히 학습 서버 매뉴얼의 데이터베이스 명령 가이드 참조하세요.
이벤트 구독
애플리케이션에서 명령 이벤트를 구독하여 해당 이벤트에 대한 세부 정보에 액세스할 수 있습니다. 다음 예제에서는 CommandMonitor
를 인스턴스화하고 배포서버에 연결하여 CommandStartedEvent
이벤트를 구독하는 방법을 보여 줍니다.
var eventArray []*event.CommandStartedEvent cmdMonitor := &event.CommandMonitor{ Started: func(ctx context.Context, e *event.CommandStartedEvent) { eventArray = append(eventArray, e) }, } clientOpts := options.Client().ApplyURI(uri).SetMonitor(cmdMonitor) client, err := mongo.Connect(clientOpts)
이벤트 설명
다음 명령 모니터링 이벤트 중 하나 이상을 구독할 수 있습니다.
이벤트 이름 | 설명 |
---|---|
| 명령이 시작될 때 생성됩니다. |
| 명령이 성공하면 생성됩니다. |
| 명령이 성공하지 못했을 때 생성됩니다. |
이벤트 문서 예시
다음 섹션에서는 각 유형의 명령 모니터링 이벤트에 대한 샘플 출력을 보여줍니다.
CommandStartedEvent
*event.CommandStartedEvent { "Command": "...", "DatabaseName": "...", "CommandName": "...", "RequestID": ..., "ConnectionID": "...", "ServerConnectionID": ..., "ServiceID": "..." }
CommandSucceededEvent
*event.CommandSucceededEvent { "DurationNanos": 38717583, "Duration": 38717583, "CommandName": "insert", "RequestID": 13, "ConnectionID": "...", "ServerConnectionID": ..., "ServiceID": null, "Reply": "..." }
CommandFailedEvent
*event.CommandFailedEvent { "DurationNanos": 38717583, "Duration": 38717583, "CommandName": "insert", "RequestID": 13, "ConnectionID": "...", "ServerConnectionID": ..., "ServiceID": null, "Failure": "..." }
서버 검색 및 모니터링 이벤트 모니터링
고 (Go) 운전자 연결한 인스턴스 또는 클러스터 의 상태 변경될 때 토폴로지 이벤트(SDAM 이벤트라고도 함)를 생성합니다. 예시 를 들어, 운전자 사용자가 새 연결을 설정하거나 클러스터 새 프라이머리 노드 선택할 때 이벤트 생성합니다.
다음 섹션에서는 애플리케이션에서 토폴로지 변경 사항을 기록하고 이러한 이벤트에서 제공된 정보를 탐색하는 방법을 보여 줍니다.
이벤트 구독
애플리케이션에서 SDAM 이벤트를 구독하면 해당 이벤트에 대한 세부 정보에 액세스할 수 있습니다. 다음 예제에서는 ServerMonitor
를 인스턴스화하고 배포서버에 연결하여 ServerClosed
이벤트를 구독하는 방법을 보여 줍니다.
var eventArray []*event.ServerClosedEvent srvMonitor := &event.ServerMonitor{ ServerClosed: func(e *event.ServerClosedEvent) { eventArray = append(eventArray, e) }, } clientOpts := options.Client().ApplyURI(uri).SetServerMonitor(srvMonitor) client, err := mongo.Connect(clientOpts)
이벤트 설명
ServerMonitor
인스턴스의 속성을 지정하여 다음 SDAM 이벤트를 구독할 수 있습니다.
이벤트 이름 | 설명 |
---|---|
| 인스턴스 상태가 변경될 때 생성됩니다(예: 세컨더리에서 프라이머리로). |
| 서버가 초기화될 때 생성됩니다. |
| 서버가 닫힐 때 생성됩니다. |
| 새 기본 프록시가 투표되거나 |
| 토폴로지가 초기화될 때 생성됩니다. |
| 토폴로지가 닫힐 때 생성됩니다. |
| 하트비트가 시작될 때 생성됩니다. |
| 하트비트가 성공할 때 생성됩니다. |
| 하트비트가 실패할 때 생성됩니다. |
이벤트 문서 예시
다음 섹션에서는 각 유형의 SDAM 이벤트에 대한 샘플 출력을 보여줍니다.
ServerDescriptionChangedEvent
*event.ServerDescriptionChangedEvent { "Address": "...", "TopologyID": "...", "PreviousDescription": { "Addr": "...", "Arbiters": null, "AverageRTT": 0, "AverageRTTSet": false, "Compression": null, "CanonicalAddr": "...", "ElectionID": "...", "HeartbeatInterval": 0, "HelloOK": false, "Hosts": null, "LastError": null, "LastUpdateTime": "...", "LastWriteTime": "...", "MaxBatchCount": 0, "MaxDocumentSize": 0, "MaxMessageSize": 0, "Members": null, "Passives": null, "Passive": false, "Primary": "...", "ReadOnly": false, "ServiceID": null, "SessionTimeoutMinutes": 0, "SetName": "...", "SetVersion": 0, "Tags": null, "TopologyVersion": null, "Kind": 0, "WireVersion": null }, "NewDescription": { "Addr": "...", "Arbiters": null, "AverageRTT": ..., "AverageRTTSet": true, "Compression": null, "CanonicalAddr": "...", "ElectionID": "...", "HeartbeatInterval": ..., "HelloOK": true, "Hosts": [...], "LastError": null, "LastUpdateTime": "...", "LastWriteTime": "...", "MaxBatchCount": ..., "MaxDocumentSize": ..., "MaxMessageSize": ..., "Members": [...], "Passives": null, "Passive": false, "Primary": "...", "ReadOnly": false, "ServiceID": null, "SessionTimeoutMinutes": 30, "SetName": "...", "SetVersion": 9, "Tags": [...], "TopologyVersion": {...}, "Kind": 10, "WireVersion": {...} } }
Kind
필드 값
이벤트 문서의 Kind
필드는 토폴로지의 단일 서버 유형을 나타내며 다음 값을 가질 수 있습니다.
값 | 설명 |
---|---|
| 알 수 없는 인스턴스 |
| 독립형 인스턴스 |
| 복제본 세트 멤버 |
| 프라이머리 인스턴스 |
| 세컨더리 인스턴스 |
| 중재자 인스턴스 |
| 복제본 세트 고스트(쿼리할 수 없는 멤버) |
|
|
| 로드 밸런서 인스턴스 |
ServerOpeningEvent
*event.ServerOpeningEvent { "Address": "...", "TopologyID": "..." }
ServerClosedEvent
*event.ServerClosedEvent { "Address": "...", "TopologyID": "..." }
토폴로지 설명 변경 이벤트
중요
배포 토폴로지가 잠겨 있을 때 드라이버는 TopologyDescriptionChangedEvent
를 호출하므로 이 이벤트에 대한 콜백(함수 인수)은 동일한 클라이언트에서 서버 선택이 필요한 작업을 시도해서는 안 됩니다.
*event.TopologyDescriptionChangedEvent { "TopologyID": "...", "PreviousDescription": { "Servers": [ { "Addr": "...", "Arbiters": null, "AverageRTT": 0, "AverageRTTSet": false, "Compression": null, "CanonicalAddr": "...", "ElectionID": "...", "HeartbeatInterval": 0, "HelloOK": false, "Hosts": null, "LastError": null, "LastUpdateTime": "...", "LastWriteTime": "...", "MaxBatchCount": 0, "MaxDocumentSize": 0, "MaxMessageSize": 0, "Members": null, "Passives": null, "Passive": false, "Primary": "...", "ReadOnly": false, "ServiceID": null, "SessionTimeoutMinutes": 0, "SetName": "...", "SetVersion": 0, "Tags": null, "TopologyVersion": null, "Kind": 0, "WireVersion": null }, ... ], "SetName": "...", "Kind": 10, "SessionTimeoutMinutes": 30, "CompatibilityErr": null }, "NewDescription": { "Servers": [...], "SetName": "...", "Kind": 10, "SessionTimeoutMinutes": 30, "CompatibilityErr": null } }
Kind
필드의 값을 해석하려면 Kind 필드 값 섹션을 참조하세요.
토폴로지OpeningEvent
*event.TopologyOpeningEvent { "TopologyID": "..." }
토폴로지 닫힘 이벤트
*event.TopologyClosedEvent { "TopologyID": "..." }
ServerHeartbeatStartedEvent
*event.ServerHeartbeatStartedEvent { "ConnectionID": "...", "Awaited": true }
ServerHeartbeatSucceededEvent
*event.ServerHeartbeatSucceededEvent { "DurationNanos": ..., "Reply": { "Addr": "...", "Arbiters": null, "AverageRTT": 0, "AverageRTTSet": false, "Compression": null, "CanonicalAddr": "...", "ElectionID": "...", "HeartbeatInterval": 0, "HelloOK": true, "Hosts": [...], "LastError": null, "LastUpdateTime": "...", "LastWriteTime": "...", "MaxBatchCount": ..., "MaxDocumentSize": ..., "MaxMessageSize": ..., "Members": [...], "Passives": null, "Passive": false, "Primary": "...", "ReadOnly": false, "ServiceID": null, "SessionTimeoutMinutes": 30, "SetName": "...", "SetVersion": 9, "Tags": [...], "TopologyVersion": {...}, "Kind": 6, "WireVersion": {...} }, "ConnectionID": "...", "Awaited": true }
Kind
필드의 값을 해석하려면 Kind 필드 값 섹션을 참조하세요.
ServerHeartbeatFailedEvent
*event.ServerHeartbeatFailedEvent { "DurationNanos": ..., "Failure": "<error message>" "ConnectionID": "...", "Awaited": true }
연결 풀 이벤트 모니터링
연결 풀 운전자 MongoDB 인스턴스 사용하여 유지 관리하는 개방형 TCP 연결의 설정하다 입니다. 연결 풀은 애플리케이션 수행해야 하는 네트워크 핸드셰이크 횟수를 줄이고 애플리케이션 더 빠르게 실행 도움이 될 수 있습니다.
다음 섹션에서는 애플리케이션에서 연결 풀 이벤트를 기록하고 이러한 이벤트에서 제공된 정보를 탐색하는 방법을 보여 줍니다.
이벤트 구독
애플리케이션에서 연결 풀 이벤트를 구독하여 해당 이벤트에 대한 세부 정보에 액세스할 수 있습니다. 다음 예제에서는 PoolMonitor
를 인스턴스화하고 배포서버에 연결하여 PoolEvent
이벤트를 구독하는 방법을 보여 줍니다.
var eventArray []*event.PoolEvent cxnMonitor := &event.PoolMonitor{ Started: func(e *event.PoolEvent) { eventArray = append(eventArray, e) }, } clientOpts := options.Client().ApplyURI(uri).SetPoolMonitor(cxnMonitor) client, err := mongo.Connect(clientOpts)
이벤트 설명
다음 표에서는 드라이버가 발생하는 풀 이벤트의 유형에 대해 설명합니다.
풀 이벤트 유형 | 설명 |
---|---|
| 연결 풀이 생성될 때 생성됩니다. |
| 연결 풀이 준비되면 생성됩니다. |
| 풀의 모든 연결이 닫힐 때 생성됩니다. |
| 서버 인스턴스가 삭제되기 전에 연결 풀이 닫힐 때 생성됩니다. |
| 연결이 생성될 때 생성되지만 작업에 사용될 때 반드시 생성되지는 않습니다. |
| 연결이 핸드셰이크를 완료하고 작업에 사용할 준비가 된 후에 생성됩니다. |
| 연결이 닫힐 때 생성됩니다. |
| 작업이 실행을 위해 연결을 획득하려고 시도할 때 생성됩니다. |
| 작업이 실행할 연결을 획득할 수 없을 때 생성됩니다. |
| 작업이 실행을 위한 연결을 성공적으로 획득할 때 생성됩니다. |
| 작업이 실행된 후 연결이 풀에 다시 체크인될 때 생성됩니다. |
이벤트 문서 예시
다음 섹션에서는 각 유형의 연결 풀 모니터링 이벤트에 대한 샘플 출력을 보여줍니다.
connectionPoolCreated
*event.PoolEvent { "type": "ConnectionPoolCreated", "address": "...", "connectionId": 0, "options": { "maxPoolSize": 100, "minPoolSize": 0, "maxIdleTimeMS": 0 }, "reason": "", "serviceId": null, "error": null }
connectionPoolReady
*event.PoolEvent { "type": "ConnectionPoolReady", "address": "...", "connectionId": 0, "options": null, "reason": "", "serviceId": null, "error": null }
connectionPoolCleared
*event.PoolEvent { "type": "ConnectionPoolCleared", "address": "...", "connectionId": 0, "options": null, "reason": "", "serviceId": null, "error": null }
connectionPoolClosed
*event.PoolEvent { "type": "ConnectionPoolClosed", "address": "...", "connectionId": 0, "options": null, "reason": "", "serviceId": null, "error": null }
connectionCreated 이벤트
*event.PoolEvent { "type": "ConnectionCreated", "address": "...", "connectionId": 1, "options": null, "reason": "", "serviceId": null, "error": null }
connectionReady
*event.PoolEvent { "type": "ConnectionReady", "address": "...", "connectionId": 1, "options": null, "reason": "", "serviceId": null, "error": null }
연결종료
*event.PoolEvent { "type": "ConnectionClosed", "address": "...", "connectionId": 1, "options": null, "reason": "", "serviceId": null, "error": null }
connectionCheckOutStarted 이벤트
*event.PoolEvent { "type": "ConnectionCheckOutStarted", "address": "...", "connectionId": 0, "options": null, "reason": "", "serviceId": null, "error": null }
connectionCheckOutFailed
*event.PoolEvent { "type": "ConnectionCheckOutFailed", "address": "...", "connectionId": 0, "options": null, "reason": "", "serviceId": null, "error": null }
connectionCheckedOut
*event.PoolEvent { "type": "ConnectionCheckedOut", "address": "...", "connectionId": 1, "options": null, "reason": "", "serviceId": null, "error": null }
connectionCheckedIn
*event.PoolEvent { "type": "ConnectionCheckedIn", "address": "...", "connectionId": 1, "options": null, "reason": "", "serviceId": null, "error": null }
추가 정보
MongoDB 배포 모니터링에 대해 자세히 알아보려면 MongoDB를 모니터링하는 방법 문서를 참조하세요.
MongoDB 에 연결하는 방법에 대해 자세히 학습하려면 MongoClient 만들기 가이드를 참조하세요.
MongoDB 작업 수행에 대해 자세히 학습하려면 CRUD 작업 섹션을 참조하세요.
API 문서
이 가이드에서 사용되는 메서드 또는 유형에 대해 자세히 알아보려면 다음 API 문서를 참조하세요.
이벤트 패키지
명령 이벤트
SDAM 이벤트
연결 풀 이벤트
PoolMonitor 유형
PoolEvent 유형
SetPoolMonitor() 메서드