|
1 | 1 | # gomongowrapper |
2 | 2 | MongoDB Go wrapper source code |
| 3 | + |
| 4 | +## Table of contents |
| 5 | +- [End to end example](#end-to-end-example) |
| 6 | +- [Traces](#traces)y |
| 7 | +- [Metrics](#metrics) |
| 8 | + |
| 9 | +## End to end example |
| 10 | +With a MongoDB server running at "localhost:27017" and running this example with Go |
| 11 | + |
| 12 | +```go |
| 13 | +package main |
| 14 | + |
| 15 | +import ( |
| 16 | + "context" |
| 17 | + "log" |
| 18 | + "time" |
| 19 | + |
| 20 | + "github.com/mongodb/mongo-go-driver/bson" |
| 21 | + |
| 22 | + "github.com/opencensus-integrations/gomongowrapper" |
| 23 | + |
| 24 | + "contrib.go.opencensus.io/exporter/stackdriver" |
| 25 | + "go.opencensus.io/stats/view" |
| 26 | + "go.opencensus.io/trace" |
| 27 | +) |
| 28 | + |
| 29 | +func main() { |
| 30 | + // Enabling the OpenCensus exporter. |
| 31 | + // Just using Stackdriver since it has both Tracing and Metrics |
| 32 | + // and is easy to whip up. Add your desired one here. |
| 33 | + sde, err := stackdriver.NewExporter(stackdriver.Options{ |
| 34 | + ProjectID: "census-demos", |
| 35 | + MetricPrefix: "mongosample", |
| 36 | + }) |
| 37 | + if err != nil { |
| 38 | + log.Fatalf("Failed to create Stackdriver exporter: %v", err) |
| 39 | + } |
| 40 | + view.RegisterExporter(sde) |
| 41 | + trace.RegisterExporter(sde) |
| 42 | + if err := mongowrapper.RegisterAllViews(); err != nil { |
| 43 | + log.Fatalf("Failed to register all views: %v\n", err) |
| 44 | + } |
| 45 | + |
| 46 | + defer func() { |
| 47 | + <-time.After(2 * time.Minute) |
| 48 | + }() |
| 49 | + |
| 50 | + // Start a span like your application would start one. |
| 51 | + ctx, span := trace.StartSpan(context.Background(), "Fetch", trace.WithSampler(trace.AlwaysSample())) |
| 52 | + defer span.End() |
| 53 | + |
| 54 | + // Now for the mongo connections, using the context |
| 55 | + // with the span in it for continuity. |
| 56 | + client, err := mongowrapper.NewClient("mongodb://localhost:27017") |
| 57 | + if err != nil { |
| 58 | + log.Fatalf("Failed to create the new client: %v", err) |
| 59 | + } |
| 60 | + if err := client.Connect(ctx); err != nil { |
| 61 | + log.Fatalf("Failed to open client connection: %v", err) |
| 62 | + } |
| 63 | + defer client.Disconnect(ctx) |
| 64 | + coll := client.Database("the_db").Collection("music") |
| 65 | + |
| 66 | + q := bson.M{"name": "Examples"} |
| 67 | + cur, err := coll.Find(ctx, q) |
| 68 | + if err != nil { |
| 69 | + log.Fatalf("Find error: %v", err) |
| 70 | + } |
| 71 | + |
| 72 | + for cur.Next(ctx) { |
| 73 | + elem := make(map[string]int) |
| 74 | + if err := cur.Decode(elem); err != nil { |
| 75 | + log.Printf("Decode error: %v", err) |
| 76 | + continue |
| 77 | + } |
| 78 | + log.Printf("Got result: %v\n", elem) |
| 79 | + } |
| 80 | + log.Print("Done iterating") |
| 81 | + |
| 82 | + _, err = coll.DeleteMany(ctx, q) |
| 83 | + if err != nil { |
| 84 | + log.Fatalf("Failed to delete: %v", err) |
| 85 | + } |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +## Traces |
| 90 | + |
| 91 | + |
| 92 | +## Metrics |
| 93 | + |
0 commit comments