|
| 1 | +# Instana instrumentation of Couchbase SDK v2 (gocb) for Go |
| 2 | + |
| 3 | +This module contains the code for instrumenting Couchbase SDK which are 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 the `instagocb.Connect` for connecting to the couchbase server. |
| 40 | + - The function defenition seems identical, excpet you need to pass an extra argument of `instana.TraceLogger` to the instagocb.Connect. |
| 41 | +- For each instrumented service, you will find an interface in instagocb. Use this interface instead of using th direct instances from gocb. |
| 42 | + - For example, instead of `*gocb.Cluster` you need to use `instagocb.Cluster` interface. |
| 43 | + - `*gocb.Collection` - `instagocb.Collection`. |
| 44 | + - This applies to the all instrumented services. |
| 45 | + - Don't worry too much about this, if you use instagocb.Connect, the returned cluster will be able to give you all the instrumented functionalities. Eg : If you use this `cluster.Buckets()`, it will return instrumented `instagocb.BucketManager` interface instead of `*gocb.BucketManager`. |
| 46 | + - You need to set `ParentSpan` property of the `options` function argument using `instagocb.GetParentSpanFromContext(ctx)`, if your couchbase call is part of some http request or something. Else the parent-child relationship of the spans won't be tracked. (Please see the example for a full demo.) |
| 47 | + - ## sample usage |
| 48 | + ```go |
| 49 | + |
| 50 | + var collector instana.TracerLogger |
| 51 | + collector = instana.InitCollector(&instana.Options{ |
| 52 | + Service: "sample-app-couchbase", |
| 53 | + EnableAutoProfile: true, |
| 54 | + }) |
| 55 | + |
| 56 | + // connect to database |
| 57 | + // this will returns an instance of instagocb.Cluster, |
| 58 | + // which is capable of enabling instana tracing for Couchbase calls. |
| 59 | + cluster, err := instagocb.Connect(collector, connectionString, gocb.ClusterOptions{ |
| 60 | + Authenticator: gocb.PasswordAuthenticator{ |
| 61 | + Username: username, |
| 62 | + Password: password, |
| 63 | + }, |
| 64 | + }) |
| 65 | + if err != nil { |
| 66 | + // Handle error |
| 67 | + } |
| 68 | + |
| 69 | + bucket := cluster.Bucket(bucketName) |
| 70 | + err = bucket.WaitUntilReady(5*time.Second, nil) |
| 71 | + if err != nil { |
| 72 | + // Handle error |
| 73 | + } |
| 74 | + |
| 75 | + collection := bucket.Scope("tenant_agent_00").Collection("users") |
| 76 | + |
| 77 | + type User struct { |
| 78 | + Name string `json:"name"` |
| 79 | + Email string `json:"email"` |
| 80 | + Interests []string `json:"interests"` |
| 81 | + } |
| 82 | + |
| 83 | + // Create and store a Document |
| 84 | + _, err = col.Upsert("u:jade", |
| 85 | + User{ |
| 86 | + Name: "Jade", |
| 87 | + |
| 88 | + Interests: []string{"Swimming", "Rowing"}, |
| 89 | + }, &gocb.UpsertOptions{ |
| 90 | + // If you are using couchbase call as part of some http request or something, |
| 91 | + // you need to set this parentSpan property using `instagocb.GetParentSpanFromContext` method, |
| 92 | + // Else the parent-child span relationship wont be tracked. |
| 93 | + // You can keep this as nil, otherwise. |
| 94 | + ParentSpan: instagocb.GetParentSpanFromContext(ctx) |
| 95 | + }) |
| 96 | + if err != nil { |
| 97 | + // Handle error |
| 98 | + } |
| 99 | + |
| 100 | +``` |
| 101 | +[Full example][fullExample] |
| 102 | + |
| 103 | +[fullExample]: ../../example/couchbase/main.go |
| 104 | + |
| 105 | +> [!NOTE] |
| 106 | +> You will find a `Unwrap()` method in all instagocb provided interfaces, it will returned the underlying gocb instance. Eg : `cluster.Unwrap()` will return an instance of `*gocb.Cluster`. Use this 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. |
0 commit comments