You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: watch.go
    path of file: ./repos/bk-cmdb/src/storage/stream/event
    
    the code of the file until where you have to start completion: /*
 * Tencent is pleased to support the open source community by making 蓝鲸 available.
 * Copyright (C) 2017-2018 THL A29 Limited, a Tencent company. All rights reserved.
 * Licensed under the MIT License (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * http://opensource.org/licenses/MIT
 * Unless required by applicable law or agreed to in writing, software distributed under
 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions and
 * limitations under the License.
 */

package event

import (
	"context"
	"errors"
	"reflect"
	"strings"
	"time"

	"configcenter/src/common/blog"
	"configcenter/src/common/json"
	"configcenter/src/storage/stream/types"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

// Watch TODO
func (e *Event) Watch(ctx context.Context, opts *types.WatchOptions) (*types.Watcher, error) {
	if err := opts.CheckSetDefault(); err != nil {
		return nil, err
	}

	eventChan := make(chan *types.Event, types.DefaultEventChanSize)
	go func() {
		pipeline, streamOptions := generateOptions(&opts.Options)

		blog.InfoJSON("start watch with pipeline: %s, options: %s, stream options: %s", pipeline, opts, streamOptions)

		var stream *mongo.ChangeStream
		var err error

		if opts.Collection != "" {
			stream, err = e.client.
				Database(e.database).
				Collection(opts.Collection).
				Watch(ctx, pipeline, streamOptions)
		} else {
			stream, err = e.client.
				Database(e.database).
				Watch(ctx, pipeline, streamOptions)
		}

		if err != nil && isFatalError(err) {
			// TODO: send alarm immediately.
			blog.Errorf("mongodb watch collection:
    
func (e *Event) Watch(ctx context.Context, opts *types.WatchOptions) (*types.Watcher, error) {
	if err := opts.CheckSetDefault(); err != nil {
		return nil, err
	}

	eventChan := make(chan *types.Event, types.DefaultEventChanSize)
	go func() {
		pipeline, streamOptions := generateOptions(&opts.Options)

		blog.InfoJSON("start watch with pipeline: %s, options: %s, stream options: %s", pipeline, opts, streamOptions)

		var stream *mongo.ChangeStream
		var err error

		if opts.Collection != "" {
			stream, err = e.client.
				Database(e.database).
				Collection(opts.Collection).
				Watch(ctx, pipeline, streamOptions)
		} else {
			stream, err = e.client.
				Database(e.database).
				Watch(ctx, pipeline, streamOptions)
		}

		if err != nil && isFatalError(err) {
			// TODO: send alarm immediately.
			blog.Errorf("mongodb watch collection: %s got a fatal error, skip resume token and retry, err: %v",
				opts.Collection, err)
			// reset the resume token, because we can not use the former resume token to watch success for now.
			streamOptions.StartAfter = nil
			// cause we have already got a fatal error, we can not try to watch from where we lost.
			// so re-watch from 1 minutes ago to avoid lost events.
			// Note: apparently, we may got duplicate events with this re-watch
			startAtTime := uint32(time.Now().Unix()) - 60
			streamOptions.StartAtOperationTime = &primitive.Timestamp{
				T: startAtTime,
				I: 0,
			}

			if opts.WatchFatalErrorCallback != nil {
				err := opts.WatchFatalErrorCallback(types.TimeStamp{Sec: startAtTime})
				if err != nil {
					blog.Errorf("do watch fatal error callback for coll %s failed, err: %v", opts.Collection, err)
				}
			}

			blog.InfoJSON("start watch with pipeline: %s, options: %s, stream options: %s", pipeline, opts, streamOptions)
			if opts.Collection != "" {
				stream, err = e.client.
					Database(e.database).
					Collection(opts.Collection).
					Watch(ctx, pipeline, streamOptions)
			} else {
				stream, err = e.client.
					Database(e.database).
					Watch(ctx, pipeline, streamOptions)
			}
		}

		if err != nil {
			if errors.Is(err, context.Canceled) {
				// if error is context cancelled, then loop watch will exit at the same time
				return
			}
			blog.Fatalf("mongodb watch failed with conf: %+v, err: %v", *opts, err)
		}

		go e.loopWatch(ctx, &opts.Options, streamOptions, stream, pipeline, eventChan)

	}()

	watcher := &types.Watcher{
		EventChan: eventChan,
	}
	return watcher, nil
}