forked from oplehto/platform
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogging.go
More file actions
59 lines (53 loc) · 1.36 KB
/
logging.go
File metadata and controls
59 lines (53 loc) · 1.36 KB
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
package query
import (
"context"
"fmt"
"io"
"time"
"github.com/influxdata/flux"
)
// LoggingServiceBridge implements ProxyQueryService and logs the queries while consuming a QueryService interface.
type LoggingServiceBridge struct {
QueryService QueryService
QueryLogger Logger
}
// Query executes and logs the query.
func (s *LoggingServiceBridge) Query(ctx context.Context, w io.Writer, req *ProxyRequest) (n int64, err error) {
var stats flux.Statistics
defer func() {
r := recover()
if r != nil {
err = fmt.Errorf("panic: %v", r)
}
log := Log{
OrganizationID: req.Request.OrganizationID,
ProxyRequest: req,
ResponseSize: n,
Time: time.Now(),
Statistics: stats,
}
if err != nil {
log.Error = err
}
s.QueryLogger.Log(log)
}()
results, err := s.QueryService.Query(ctx, &req.Request)
if err != nil {
return 0, err
}
// Check if this result iterator reports stats. We call this defer before cancel because
// the query needs to be finished before it will have valid statistics.
if s, ok := results.(flux.Statisticser); ok {
defer func() {
stats = s.Statistics()
}()
}
defer results.Release()
encoder := req.Dialect.Encoder()
n, err = encoder.Encode(w, results)
if err != nil {
return n, err
}
// The results iterator may have had an error independent of encoding errors.
return n, results.Err()
}