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

docs: add guides for how to use this wrapper #2

Open
odeke-em opened this issue Oct 31, 2018 · 1 comment
Open

docs: add guides for how to use this wrapper #2

odeke-em opened this issue Oct 31, 2018 · 1 comment

Comments

@odeke-em
Copy link
Member

Important for usage

@odeke-em
Copy link
Member Author

I added some example_test.go code in

package mongowrapper_test
import (
"context"
"log"
"time"
"github.com/mongodb/mongo-go-driver/bson"
"github.com/opencensus-integrations/gomongowrapper"
"contrib.go.opencensus.io/exporter/stackdriver"
"go.opencensus.io/stats/view"
"go.opencensus.io/trace"
)
func Example() {
// Enabling the OpenCensus exporter.
// Just using Stackdriver since it has both Tracing and Metrics
// and is easy to whip up. Add your desired one here.
sde, err := stackdriver.NewExporter(stackdriver.Options{
ProjectID: "census-demos",
MetricPrefix: "mongosample",
})
if err != nil {
log.Fatalf("Failed to create Stackdriver exporter: %v", err)
}
view.RegisterExporter(sde)
trace.RegisterExporter(sde)
if err := mongowrapper.RegisterAllViews(); err != nil {
log.Fatalf("Failed to register all views: %v\n", err)
}
defer func() {
<-time.After(2 * time.Minute)
}()
// Start a span like your application would start one.
ctx, span := trace.StartSpan(context.Background(), "Fetch", trace.WithSampler(trace.AlwaysSample()))
defer span.End()
// Now for the mongo connections, using the context
// with the span in it for continuity.
client, err := mongowrapper.NewClient("mongodb://localhost:27017")
if err != nil {
log.Fatalf("Failed to create the new client: %v", err)
}
if err := client.Connect(ctx); err != nil {
log.Fatalf("Failed to open client connection: %v", err)
}
defer client.Disconnect(ctx)
coll := client.Database("the_db").Collection("music")
q := bson.M{"name": "Examples"}
cur, err := coll.Find(ctx, q)
if err != nil {
log.Fatalf("Find error: %v", err)
}
for cur.Next(ctx) {
elem := make(map[string]int)
if err := cur.Decode(elem); err != nil {
log.Printf("Decode error: %v", err)
continue
}
log.Printf("Got result: %v\n", elem)
}
log.Print("Done iterating")
_, err = coll.DeleteMany(ctx, q)
if err != nil {
log.Fatalf("Failed to delete: %v", err)
}
}

so perhaps that's sufficient? I paged @PikBot to help examine the example that I provided in https://opencensus.io/guides/integrations/mongodb/go_driver/

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

No branches or pull requests

1 participant