-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathquery.go
68 lines (65 loc) · 1.89 KB
/
query.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
// Copyright 2022 The incite Authors. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package incite
import "context"
// Query is sweet, sweet sugar to perform a synchronous CloudWatch Logs
// Insights query and get back all the results without needing to
// construct a QueryManager. Query runs the query indicated by q, using
// the CloudWatch Logs actions provided by a, and returns all the
// query results (or the error if the query failed).
//
// The context ctx controls the lifetime of the query. If ctx expires or
// is cancelled, the query is cancelled and an error is returned. If
// you don't need the ability to set a timeout or cancel the request,
// use context.Background().
//
// Unlike NewQueryManager, supports a configurable level of parallel
// query execution, Query uses a parallelism factor of 1. This means
// that if q represents a chunked query, then the chunks will be run
// serially, in ascending order of time.
//
// This function is intended for quick prototyping and simple scripting
// and command-line interface use cases. More complex applications,
// especially applications running concurrent queries against the same
// region from multiple goroutines, should construct and configure a
// QueryManager explicitly.
func Query(ctx context.Context, a CloudWatchLogsActions, q QuerySpec) ([]Result, error) {
if ctx == nil {
panic(nilContextMsg)
}
m := NewQueryManager(Config{
Actions: a,
Parallel: 1,
})
defer func() {
_ = m.Close()
}()
c := make(chan struct {
r []Result
err error
})
go func() {
var s Stream
var p struct {
r []Result
err error
}
s, p.err = m.Query(q)
if p.err == nil {
p.r, p.err = ReadAll(s)
}
c <- p
}()
select {
case p := <-c:
close(c)
return p.r, p.err
case <-ctx.Done():
go func() {
<-c
close(c)
}()
return nil, ctx.Err()
}
}