|
| 1 | +# Instana instrumentation of Couchbase SDK v2 (gocb) for Go |
| 2 | + |
| 3 | +This module contains the code for instrumenting the Couchbase SDK, based on the [`gocb`][gocb-github] library for Go. |
| 4 | + |
| 5 | +The following services are currently instrumented: |
| 6 | + |
| 7 | +1. Cluster |
| 8 | +2. Bucket |
| 9 | +3. Bucket Manager |
| 10 | +4. Collection |
| 11 | +5. Binary Collection |
| 12 | +6. Collection Manager |
| 13 | +7. Data Structures |
| 14 | + 1. List |
| 15 | + 2. Map |
| 16 | + 3. Queue |
| 17 | + 4. Set |
| 18 | + |
| 19 | + |
| 20 | +Compatibility |
| 21 | +------------- |
| 22 | + |
| 23 | + * Minimum gocb version - v2.7.0 |
| 24 | + * Minimum Go version - 1.13 |
| 25 | + |
| 26 | + |
| 27 | +Installation |
| 28 | +------------ |
| 29 | + |
| 30 | +To add the module to your `go.mod` file run the following command in your project directory: |
| 31 | + |
| 32 | +```bash |
| 33 | +$ go get github.com/instana/go-sensor/instrumentation/instagocb |
| 34 | +``` |
| 35 | + |
| 36 | +Usage |
| 37 | +------ |
| 38 | + |
| 39 | +- Instead of using `gocb.Connect`, use `instagocb.Connect` to connect to the Couchbase server. |
| 40 | + - The function definition seems identical, except you need to pass an extra argument of `instana.TraceLogger` to instagocb.Connect. |
| 41 | +- For each instrumented service, you will find an interface in `instagocb`. Use this interface instead of using the direct instances from `gocb`. |
| 42 | + - For example, instead of `*gocb.Cluster`, use `instagocb.Cluster` interface. |
| 43 | + - `*gocb.Collection` becomes `instagocb.Collection`. |
| 44 | + - This applies to all instrumented services. |
| 45 | +- If you use `instagocb.Connect`, the returned cluster will be able to provide all the instrumented functionalities. For example, if you use `cluster.Buckets()`, it will return an instrumented `instagocb.BucketManager` interface instead of `*gocb.BucketManager`. |
| 46 | +- Set the `ParentSpan` property of the `options` function argument using `instagocb.GetParentSpanFromContext(ctx)` if your Couchbase call is part of some HTTP request or something. Otherwise, the parent-child relationship of the spans won't be tracked (see the example for a full demo). |
| 47 | +- There is an `Unwrap()` method in all instagocb provided interfaces; it will return the underlying gocb instance. For example, `cluster.Unwrap()` will return an instance of `*gocb.Cluster`. |
| 48 | + |
| 49 | +> [!IMPORTANT] |
| 50 | +> Use `Unwrap()` if you need the original instance other than the instrumented one. It is not advisable to use this directly, as Instana tracing will not be enabled if you directly utilize this instance. |
| 51 | +
|
| 52 | + |
| 53 | +Sample Usage |
| 54 | +------------ |
| 55 | + ```go |
| 56 | + |
| 57 | + var collector instana.TracerLogger |
| 58 | + collector = instana.InitCollector(&instana.Options{ |
| 59 | + Service: "sample-app-couchbase", |
| 60 | + EnableAutoProfile: true, |
| 61 | + }) |
| 62 | + |
| 63 | + // connect to database |
| 64 | + // this will returns an instance of instagocb.Cluster, |
| 65 | + // which is capable of enabling instana tracing for Couchbase calls. |
| 66 | + cluster, err := instagocb.Connect(collector, connectionString, gocb.ClusterOptions{ |
| 67 | + Authenticator: gocb.PasswordAuthenticator{ |
| 68 | + Username: username, |
| 69 | + Password: password, |
| 70 | + }, |
| 71 | + }) |
| 72 | + if err != nil { |
| 73 | + // Handle error |
| 74 | + } |
| 75 | + |
| 76 | + bucket := cluster.Bucket(bucketName) |
| 77 | + err = bucket.WaitUntilReady(5*time.Second, nil) |
| 78 | + if err != nil { |
| 79 | + // Handle error |
| 80 | + } |
| 81 | + |
| 82 | + collection := bucket.Scope("tenant_agent_00").Collection("users") |
| 83 | + |
| 84 | + type User struct { |
| 85 | + Name string `json:"name"` |
| 86 | + Email string `json:"email"` |
| 87 | + Interests []string `json:"interests"` |
| 88 | + } |
| 89 | + |
| 90 | + // Create and store a Document |
| 91 | + _, err = col.Upsert("u:jade", |
| 92 | + User{ |
| 93 | + Name: "Jade", |
| 94 | + |
| 95 | + Interests: []string{"Swimming", "Rowing"}, |
| 96 | + }, &gocb.UpsertOptions{ |
| 97 | + // If you are using couchbase call as part of some http request or something, |
| 98 | + // you need to set this parentSpan property using `instagocb.GetParentSpanFromContext` method, |
| 99 | + // Else the parent-child span relationship wont be tracked. |
| 100 | + // You can keep this as nil, otherwise. |
| 101 | + ParentSpan: instagocb.GetParentSpanFromContext(ctx) |
| 102 | + }) |
| 103 | + if err != nil { |
| 104 | + // Handle error |
| 105 | + } |
| 106 | + |
| 107 | +``` |
| 108 | +[Full example][fullExample] |
| 109 | + |
| 110 | +[fullExample]: ../../example/couchbase/main.go |
0 commit comments