-
Notifications
You must be signed in to change notification settings - Fork 104
/
analyticsprovider_core.go
119 lines (101 loc) · 3.39 KB
/
analyticsprovider_core.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package gocb
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/couchbase/gocbcore/v10"
)
type analyticsProviderCoreProvider interface {
AnalyticsQuery(ctx context.Context, opts gocbcore.AnalyticsQueryOptions) (analyticsRowReader, error)
}
type analyticsProviderCore struct {
provider analyticsProviderCoreProvider
mgmtProvider mgmtProvider
retryStrategyWrapper *coreRetryStrategyWrapper
transcoder Transcoder
analyticsTimeout time.Duration
tracer *tracerWrapper
}
type jsonAnalyticsMetrics struct {
ElapsedTime string `json:"elapsedTime"`
ExecutionTime string `json:"executionTime"`
ResultCount uint64 `json:"resultCount"`
ResultSize uint64 `json:"resultSize"`
MutationCount uint64 `json:"mutationCount,omitempty"`
SortCount uint64 `json:"sortCount,omitempty"`
ErrorCount uint64 `json:"errorCount,omitempty"`
WarningCount uint64 `json:"warningCount,omitempty"`
ProcessedObjects uint64 `json:"processedObjects,omitempty"`
}
type jsonAnalyticsWarning struct {
Code uint32 `json:"code"`
Message string `json:"msg"`
}
type jsonAnalyticsResponse struct {
RequestID string `json:"requestID"`
ClientContextID string `json:"clientContextID"`
Status string `json:"status"`
Warnings []jsonAnalyticsWarning `json:"warnings"`
Metrics jsonAnalyticsMetrics `json:"metrics"`
Signature interface{} `json:"signature"`
}
func (ap *analyticsProviderCore) AnalyticsQuery(statement string, scope *Scope, opts *AnalyticsOptions) (*AnalyticsResult, error) {
if opts == nil {
opts = &AnalyticsOptions{}
}
span := ap.tracer.createSpan(opts.ParentSpan, "analytics", "analytics")
span.SetAttribute("db.statement", statement)
if scope != nil {
span.SetAttribute("db.name", scope.BucketName())
span.SetAttribute("db.couchbase.scope", scope.Name())
}
defer span.End()
timeout := opts.Timeout
if opts.Timeout == 0 {
timeout = ap.analyticsTimeout
}
deadline := time.Now().Add(timeout)
retryStrategy := ap.retryStrategyWrapper
if opts.RetryStrategy != nil {
retryStrategy = newCoreRetryStrategyWrapper(opts.RetryStrategy)
}
queryOpts, err := opts.toMap()
if err != nil {
return nil, &AnalyticsError{
InnerError: wrapError(err, "failed to generate query options"),
Statement: statement,
ClientContextID: opts.ClientContextID,
}
}
var priorityInt int32
if opts.Priority {
priorityInt = -1
}
queryOpts["statement"] = statement
if scope != nil {
queryOpts["query_context"] = fmt.Sprintf("default:`%s`.`%s`", scope.BucketName(), scope.Name())
}
eSpan := ap.tracer.createSpan(span, "request_encoding", "")
reqBytes, err := json.Marshal(queryOpts)
eSpan.End()
if err != nil {
return nil, &AnalyticsError{
InnerError: wrapError(err, "failed to marshall query body"),
Statement: maybeGetAnalyticsOption(queryOpts, "statement"),
ClientContextID: maybeGetAnalyticsOption(queryOpts, "client_context_id"),
}
}
res, err := ap.provider.AnalyticsQuery(opts.Context, gocbcore.AnalyticsQueryOptions{
Payload: reqBytes,
Priority: int(priorityInt),
RetryStrategy: retryStrategy,
Deadline: deadline,
TraceContext: span.Context(),
User: opts.Internal.User,
})
if err != nil {
return nil, maybeEnhanceAnalyticsError(err)
}
return newAnalyticsResult(res), nil
}