/
scope.go
85 lines (61 loc) · 2.33 KB
/
scope.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// (c) Copyright IBM Corp. 2023
package instagocb
import "github.com/couchbase/gocb/v2"
type Scope interface {
Name() string
BucketName() string
Collection(collectionName string) Collection
Query(statement string, opts *gocb.QueryOptions) (*gocb.QueryResult, error)
AnalyticsQuery(statement string, opts *gocb.AnalyticsOptions) (*gocb.AnalyticsResult, error)
Unwrap() *gocb.Scope
}
type instaScope struct {
iTracer gocb.RequestTracer
*gocb.Scope
}
// Query executes the query statement on the server, constraining the query to the bucket and scope.
func (is *instaScope) Query(statement string, opts *gocb.QueryOptions) (*gocb.QueryResult, error) {
var tracectx gocb.RequestSpanContext
if opts.ParentSpan != nil {
tracectx = opts.ParentSpan.Context()
}
span := is.iTracer.RequestSpan(tracectx, "QUERY")
span.SetAttribute(operationSpanTag, statement)
span.SetAttribute(bucketNameSpanTag, is.BucketName())
res, err := is.Scope.Query(statement, opts)
span.(*Span).err = err
defer span.End()
return res, err
}
// AnalyticsQuery executes the analytics query statement on the server, constraining the query to the bucket and scope.
func (is *instaScope) AnalyticsQuery(statement string, opts *gocb.AnalyticsOptions) (*gocb.AnalyticsResult, error) {
var tracectx gocb.RequestSpanContext
if opts.ParentSpan != nil {
tracectx = opts.ParentSpan.Context()
}
span := is.iTracer.RequestSpan(tracectx, "ANALYTICS_QUERY")
span.SetAttribute(operationSpanTag, statement)
span.SetAttribute(bucketNameSpanTag, is.BucketName())
res, err := is.Scope.AnalyticsQuery(statement, opts)
span.(*Span).err = err
defer span.End()
return res, err
}
// Collection returns an instance of a collection.
func (is *instaScope) Collection(collectionName string) Collection {
collection := is.Scope.Collection(collectionName)
return createCollection(is.iTracer, collection)
}
// Unwrap returns the original *gocb.Scope instance.
// Note: It is not advisable to use this directly, as Instana tracing will not be enabled if you directly utilize this instance.
func (is *instaScope) Unwrap() *gocb.Scope {
return is.Scope
}
// helper functions
// createScope will wrap *gocb.Scope in to instanaScope and will return it as Scope interface
func createScope(tracer gocb.RequestTracer, scope *gocb.Scope) Scope {
return &instaScope{
iTracer: tracer,
Scope: scope,
}
}