oplog

package module
v1.0.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 13, 2021 License: MIT Imports: 5 Imported by: 0

README

Oplog

GitHub GitHub top language GitHub release (latest by date) GitHub last commit

MongoDB replicaset oplog tailing by Golang

Tailing of MongoDB Oplog

It can be send oplog to everywhere with minimal system resource usage.

  • MongoDB replicaset -> Oplog -> MongoDB | Logstash | Fluentd | ...

Install

go get -u github.com/gnokoheat/oplog

Usage example

  • main.go
package main

import (
	"log"
	"github.com/gnokoheat/oplog"
)

func main() {
	var o = &oplog.Options{
		// (e.g. mongodb://username:password@127.0.0.1:27017,127.0.0.1:27018/local?replicaSet=rs01&authSource=admin)
		Addrs:      []string{"127.0.0.1:27017", "127.0.0.1:27018"}, // replicaset host and port
		Username:   "username", // admin db username
		Password:   "password", // admin db user password
		ReplicaSet: "rs01", // replicaset name
		DB:         "myDB", // tailing target db
		Collection: "myCollection", // tailing target collection
		Events:     []string{"insert", "update", "delete"}, // tailing target method
	}

	l := make(chan *[]oplog.Log) // Oplog Channel
	e := make(chan error) // Error Channel
	
	// Oplog tailing start ! 
	go o.Tail(l, e)

	for {
		select {
		case err := <-e:
			log.Println("[Error] ", err)
			return
		case op := <-l:
			// input oplog handling code
			log.Println("[Result] ", op)
			break
		}
	}
}
  • Run
go run main.go
  • Result
2019/11/08 16:13:57 [Oplog Tail Start]  2019-11-08 07:13:57.485633 +0000 UTC
2019/11/08 16:14:04 [Result]  &[{2019-11-08 16:14:02.744 +0900 ... ]
2019/11/08 16:14:08 [Result]  &[{2019-11-08 16:14:08.554 +0900 ... ]
2019/11/08 16:16:57 [Result]  &[{2019-11-08 16:16:57.364 +0900 ... ]
...

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Log

type Log struct {
	Timestamp    time.Time              `json:"wall" bson:"wall"`
	HistoryID    int64                  `json:"h" bson:"h"`
	MongoVersion int                    `json:"v" bson:"v"`
	Operation    string                 `json:"op" bson:"op"`
	Namespace    string                 `json:"ns" bson:"ns"`
	Doc          map[string]interface{} `json:"o" bson:"o"`
	Update       map[string]interface{} `json:"o2" bson:"o2"`
}

Log : Oplog document

type Options

type Options struct {
	Addrs      []string
	Username   string
	Password   string
	ReplicaSet string
	DB         string
	Collection string
	Events     []string
}

Options : MongoDB connection information for oplog tailing

func (*Options) MgoConn

func (o *Options) MgoConn(e chan error) (*mgo.Session, *mgo.Collection)

MgoConn : MongoDB connect

func (*Options) Tail

func (o *Options) Tail(l chan *[]Log, e chan error)

Tail : MongoDB oplog tailing start

Example
package main

import (
	"log"

	"github.com/gnokoheat/oplog"
)

func main() {
	var o = &oplog.Options{
		// (e.g. mongodb://username:password@127.0.0.1:27017,127.0.0.1:27018/local?replicaSet=rs01&authSource=admin)
		Addrs:      []string{"127.0.0.1:27017", "127.0.0.1:27018"}, // replicaset host and port
		Username:   "username",                                     // admin db username
		Password:   "password",                                     // admin db user password
		ReplicaSet: "rs01",                                         // replicaset name
		DB:         "myDB",                                         // tailing target db
		Collection: "myCollection",                                 // tailing target collection
		Events:     []string{"insert", "update", "delete"},         // tailing target method
	}

	l := make(chan *[]oplog.Log) // Oplog Channel
	e := make(chan error)        // Error Channel

	// Oplog tailing start !
	go o.Tail(l, e)

	for {
		select {
		case err := <-e:
			log.Println("[Error] ", err)
			return
		case op := <-l:
			// input oplog handling code
			log.Println("[Result] ", op)
			break
		}
	}
}
Output:

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL