Join us Sept 17 at .local NYC! Use code WEB50 to save 50% on tickets. Learn more >
MongoDB Event
Docs Menu
Docs Home
/ / /
Go 드라이버
/

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

이 가이드 에서는 MongoDB 고 (Go) 드라이버 에서 모니터링 설정하다 하고 구성하는 방법을 학습 수 있습니다. 고 (Go) 운전자를 사용하여 애플리케이션에서 이벤트 를 구독하여 이벤트를 모니터할 수 있습니다.

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

이벤트 작동 중에 운전자 내에서 발생하는 모든 조치 입니다. 고 (Go) 운전자 이러한 이벤트의 하위 집합을 수신하는 기능이 포함되어 있습니다.

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

이 가이드에서는 이와 같은 작업을 수행하는 방법애 대해 학습합니다.

  • 명령 이벤트 모니터링

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

  • 연결 풀 이벤트 모니터링

이 가이드 코드에서 운전자 의 활동에 대한 정보를 사용하는 방법을 보여줍니다. 운전자 에서 이벤트를 기록 방법을 학습 고 (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

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

CommandSucceededEvent

명령이 성공하면 생성됩니다.

CommandFailedEvent

명령이 성공하지 못했을 때 생성됩니다.

다음 섹션에서는 각 유형의 명령 모니터링 이벤트에 대한 샘플 출력을 보여줍니다.

*event.CommandStartedEvent
{
"Command": "...",
"DatabaseName": "...",
"CommandName": "...",
"RequestID": ...,
"ConnectionID": "...",
"ServerConnectionID": ...,
"ServiceID": "..."
}
*event.CommandSucceededEvent
{
"DurationNanos": 38717583,
"Duration": 38717583,
"CommandName": "insert",
"RequestID": 13,
"ConnectionID": "...",
"ServerConnectionID": ...,
"ServiceID": null,
"Reply": "..."
}
*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 이벤트를 구독할 수 있습니다.

이벤트 이름
설명

ServerDescriptionChangedEvent

인스턴스 상태가 변경될 때 생성됩니다(예: 세컨더리에서 프라이머리로).

ServerOpeningEvent

서버가 초기화될 때 생성됩니다.

ServerClosedEvent

서버가 닫힐 때 생성됩니다.

TopologyDescriptionChangedEvent

새 기본 프록시가 투표되거나 mongos 프록시의 연결이 끊어지는 등 토폴로지가 변경될 때 생성됩니다.

TopologyOpeningEvent

토폴로지가 초기화될 때 생성됩니다.

TopologyClosedEvent

토폴로지가 닫힐 때 생성됩니다.

ServerHeartbeatStartedEvent

하트비트가 시작될 때 생성됩니다.

ServerHeartbeatSucceededEvent

하트비트가 성공할 때 생성됩니다.

ServerHeartbeatFailedEvent

하트비트가 실패할 때 생성됩니다.

다음 섹션에서는 각 유형의 SDAM 이벤트에 대한 샘플 출력을 보여줍니다.

*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 필드는 토폴로지의 단일 서버 유형을 나타내며 다음 값을 가질 수 있습니다.

설명

0

알 수 없는 인스턴스

1

독립형 인스턴스

2

복제본 세트 멤버

6

프라이머리 인스턴스

10

세컨더리 인스턴스

18

중재자 인스턴스

34

복제본 세트 고스트(쿼리할 수 없는 멤버)

256

mongos 프록시 인스턴스

512

로드 밸런서 인스턴스

*event.ServerOpeningEvent
{
"Address": "...",
"TopologyID": "..."
}
*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 필드 값 섹션을 참조하세요.

*event.TopologyOpeningEvent
{
"TopologyID": "..."
}
*event.TopologyClosedEvent
{
"TopologyID": "..."
}
*event.ServerHeartbeatStartedEvent
{
"ConnectionID": "...",
"Awaited": true
}
*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 필드 값 섹션을 참조하세요.

*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

연결 풀이 생성될 때 생성됩니다.

ConnectionPoolReady

연결 풀이 준비되면 생성됩니다.

ConnectionPoolCleared

풀의 모든 연결이 닫힐 때 생성됩니다.

ConnectionPoolClosed

서버 인스턴스가 삭제되기 전에 연결 풀이 닫힐 때 생성됩니다.

ConnectionCreated

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

ConnectionReady

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

ConnectionClosed

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

ConnectionCheckOutStarted

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

ConnectionCheckOutFailed

작업이 실행할 연결을 획득할 수 없을 때 생성됩니다.

ConnectionCheckedOut

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

ConnectionCheckedIn

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

다음 섹션에서는 각 유형의 연결 풀 모니터링 이벤트에 대한 샘플 출력을 보여줍니다.

*event.PoolEvent
{
"type": "ConnectionPoolCreated",
"address": "...",
"connectionId": 0,
"options": {
"maxPoolSize": 100,
"minPoolSize": 0,
"maxIdleTimeMS": 0
},
"reason": "",
"serviceId": null,
"error": null
}
*event.PoolEvent
{
"type": "ConnectionPoolReady",
"address": "...",
"connectionId": 0,
"options": null,
"reason": "",
"serviceId": null,
"error": null
}
*event.PoolEvent
{
"type": "ConnectionPoolCleared",
"address": "...",
"connectionId": 0,
"options": null,
"reason": "",
"serviceId": null,
"error": null
}
*event.PoolEvent
{
"type": "ConnectionPoolClosed",
"address": "...",
"connectionId": 0,
"options": null,
"reason": "",
"serviceId": null,
"error": null
}
*event.PoolEvent
{
"type": "ConnectionCreated",
"address": "...",
"connectionId": 1,
"options": null,
"reason": "",
"serviceId": null,
"error": null
}
*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
}
*event.PoolEvent
{
"type": "ConnectionCheckOutStarted",
"address": "...",
"connectionId": 0,
"options": null,
"reason": "",
"serviceId": null,
"error": null
}
*event.PoolEvent
{
"type": "ConnectionCheckOutFailed",
"address": "...",
"connectionId": 0,
"options": null,
"reason": "",
"serviceId": null,
"error": null
}
*event.PoolEvent
{
"type": "ConnectionCheckedOut",
"address": "...",
"connectionId": 1,
"options": null,
"reason": "",
"serviceId": null,
"error": null
}
*event.PoolEvent
{
"type": "ConnectionCheckedIn",
"address": "...",
"connectionId": 1,
"options": null,
"reason": "",
"serviceId": null,
"error": null
}

MongoDB 배포 모니터링에 대해 자세히 알아보려면 MongoDB를 모니터링하는 방법 문서를 참조하세요.

MongoDB에 연결하는 방법에 대한 자세한 내용은 연결 가이드를 참조하세요.

MongoDB 작업 수행에 대해 자세히 알아보려면 CRUD 작업 가이드를 참조하세요.

이 가이드에서 사용되는 메서드 또는 유형에 대해 자세히 알아보려면 다음 API 문서를 참조하세요.

돌아가기

모니터링 & 로깅