Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

请问有类似Monitor的针对不同事件触发的监视器吗? #3

Closed
ycvk opened this issue Dec 11, 2023 · 2 comments · Fixed by #4
Closed

请问有类似Monitor的针对不同事件触发的监视器吗? #3

ycvk opened this issue Dec 11, 2023 · 2 comments · Fixed by #4
Labels
enhancement New feature or request

Comments

@ycvk
Copy link

ycvk commented Dec 11, 2023

类似qmgo的CommandMonitor,比如我想在qmgo里设置一个监听每次MongoDB成功和失败操作请求的监视器来打印日志,便于维护和定位问题,在qmgo里代码类似这样:

        // 创建cmdMonitor,用于打印日志
	startedCommands := sync.Map{} 
	cmdMonitor := &event.CommandMonitor{
		Started: func(_ context.Context, evt *event.CommandStartedEvent) {
			startedCommands.Store(evt.RequestID, evt.Command)
		},
		Succeeded: func(_ context.Context, evt *event.CommandSucceededEvent) {
			var commands bson.Raw
			v, ok := startedCommands.Load(evt.RequestID)
			if ok {
				commands = v.(bson.Raw)
			}
			defer mLog.Sync()
			mLog.Infof("\n[MongoDB] [%.3fms] [%v] %v \n", float64(evt.DurationNanos)/1e6, commands, evt.Reply)
		},
		Failed: func(_ context.Context, evt *event.CommandFailedEvent) {
			var commands bson.Raw
			v, ok := startedCommands.Load(evt.RequestID)
			if ok {
				commands = v.(bson.Raw)
			}
			defer mLog.Sync()
			mLog.Fatalf("\n[MongoDB] [%.3fms] [%v] \n %v \n", float64(evt.DurationNanos)/1e6, commands, evt.Failure)
		},
	}
	ops.SetMonitor(cmdMonitor)
@here-Leslie-Lau here-Leslie-Lau added the enhancement New feature or request label Dec 11, 2023
@here-Leslie-Lau
Copy link
Owner

目前还不支持,这段时间会优先支持事件监听器

@here-Leslie-Lau here-Leslie-Lau linked a pull request Dec 14, 2023 that will close this issue
@here-Leslie-Lau
Copy link
Owner

@ycvk tag: v1.0.4已支持,用例请参考README.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants