github.com/epsagon/epsagon-go@v1.39.0/example/mongo_example/main.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"github.com/epsagon/epsagon-go/epsagon"
     7  	epsagonmongo "github.com/epsagon/epsagon-go/wrappers/mongo"
     8  	"time"
     9  
    10  	"go.mongodb.org/mongo-driver/bson"
    11  	"go.mongodb.org/mongo-driver/mongo"
    12  	"go.mongodb.org/mongo-driver/mongo/options"
    13  	"go.mongodb.org/mongo-driver/mongo/readpref"
    14  )
    15  
    16  func dbAPI() {
    17  
    18  	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
    19  	defer cancel()
    20  
    21  	client, err := mongo.Connect(
    22  		ctx,
    23  		options.Client().ApplyURI("mongodb://localhost:27017"),
    24  	)
    25  
    26  	defer func() {
    27  		if err = client.Disconnect(ctx); err != nil {
    28  			panic(err)
    29  		}
    30  	}()
    31  
    32  	err = client.Ping(ctx, readpref.Primary())
    33  
    34  	db := client.Database("DB")
    35  
    36  	// WRAP THE MONGO COLLECTION with WrapMongoCollection()
    37  	coll := epsagonmongo.WrapMongoCollection(
    38  		db.Collection("COLL"),
    39  	)
    40  
    41  	type doc struct {
    42  		Name string
    43  	}
    44  	var res interface{}
    45  
    46  	fmt.Println("##InsertOne")
    47  	_, err = coll.InsertOne(
    48  		context.Background(),
    49  		doc{Name: "bon"},
    50  	)
    51  	if err != nil {
    52  		panic(err)
    53  	}
    54  
    55  	fmt.Println("##InsertMany")
    56  	_, err = coll.InsertMany(
    57  		context.Background(),
    58  		[]interface{}{
    59  			bson.D{
    60  				{Key: "name", Value: "hello"},
    61  				{Key: "age", Value: "33"},
    62  			},
    63  			bson.D{
    64  				{Key: "name", Value: "world"},
    65  				{Key: "age", Value: "44"},
    66  			},
    67  		},
    68  	)
    69  	if err != nil {
    70  		panic(err)
    71  	}
    72  
    73  	fmt.Println("##FindOne")
    74  	coll.FindOne(
    75  		context.Background(),
    76  		bson.D{{Key: "name", Value: "bon"}},
    77  	)
    78  
    79  	fmt.Println("##Find")
    80  	coll.Find(context.Background(), bson.M{})
    81  
    82  	fmt.Println("##Aggregate")
    83  	res, err = coll.Aggregate(
    84  		context.Background(),
    85  		mongo.Pipeline{
    86  			bson.D{{Key: "$match", Value: bson.D{{Key: "name", Value: "bon"}}}},
    87  		},
    88  	)
    89  	if err != nil || err == mongo.ErrNoDocuments {
    90  		panic(err)
    91  	}
    92  
    93  	fmt.Println("##CountDocuments")
    94  	res, err = coll.CountDocuments(
    95  		context.Background(),
    96  		bson.D{{Key: "name", Value: "bon"}},
    97  	)
    98  	fmt.Println(res)
    99  	if err != nil || err == mongo.ErrNoDocuments {
   100  		panic(err)
   101  	}
   102  
   103  	fmt.Println("##DeleteOne")
   104  	res, err = coll.DeleteOne(
   105  		context.Background(),
   106  		bson.D{{Key: "name", Value: "bon"}},
   107  	)
   108  	fmt.Println(res)
   109  	if err != nil || err == mongo.ErrNoDocuments {
   110  		panic(err)
   111  	}
   112  
   113  	fmt.Println("##UpdateOne")
   114  	res, err = coll.UpdateOne(
   115  		context.Background(),
   116  		bson.D{{Key: "name", Value: "bon"}},
   117  		bson.D{{Key: "$set", Value: bson.D{{Key: "name", Value: "son"}}}},
   118  	)
   119  	fmt.Println(res)
   120  	if err != nil || err == mongo.ErrNoDocuments {
   121  		panic(err)
   122  	}
   123  }
   124  
   125  func main() {
   126  
   127  	config := epsagon.NewTracerConfig(
   128  		"skate-shop",
   129  		"token",
   130  	)
   131  	config.MetadataOnly = false
   132  	config.Debug = true
   133  
   134  	epsagon.GoWrapper(
   135  		config,
   136  		dbAPI,
   137  	)()
   138  
   139  }