-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement lifecycle events, results, diagnostics with InfluxDB #3
Merged
Merged
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1dca242
wip metrics.
raulk d653ac0
batch write results to InfluxDB on close.
raulk 4202902
test utils for sdk-go.
raulk fa3c2b4
influxdb v2
raulk 1ac1cec
switch to influxdb v1; implement batching; tests.
raulk b040b9f
go mod tidy.
raulk 12bb027
simplify and clean up solution.
raulk 8291112
adjust points translation.
raulk 6fc3a07
test: remove setting INFLUXDB_URL env var.
raulk 429e879
rename INFLUXDB_ADDR env var to INFLUXDB_URL for correctness.
raulk dc53852
add TestFrequencyChange test.
raulk f69054c
remove New prefix from aggregated metrics accessors.
raulk e00149e
rename GaugeFunctional to GaugeF.
raulk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package runtime | ||
|
||
const ( | ||
EnvTestBranch = "TEST_BRANCH" | ||
EnvTestCase = "TEST_CASE" | ||
EnvTestGroupID = "TEST_GROUP_ID" | ||
EnvTestGroupInstanceCount = "TEST_GROUP_INSTANCE_COUNT" | ||
EnvTestInstanceCount = "TEST_INSTANCE_COUNT" | ||
EnvTestInstanceParams = "TEST_INSTANCE_PARAMS" | ||
EnvTestInstanceRole = "TEST_INSTANCE_ROLE" | ||
EnvTestOutputsPath = "TEST_OUTPUTS_PATH" | ||
EnvTestPlan = "TEST_PLAN" | ||
EnvTestRepo = "TEST_REPO" | ||
EnvTestRun = "TEST_RUN" | ||
EnvTestSidecar = "TEST_SIDECAR" | ||
EnvTestStartTime = "TEST_START_TIME" | ||
EnvTestSubnet = "TEST_SUBNET" | ||
EnvTestTag = "TEST_TAG" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
package runtime | ||
|
||
import ( | ||
"io" | ||
"time" | ||
|
||
"github.com/avast/retry-go" | ||
_ "github.com/influxdata/influxdb1-client" | ||
client "github.com/influxdata/influxdb1-client/v2" | ||
) | ||
|
||
type Batcher interface { | ||
io.Closer | ||
|
||
WritePoint(p *client.Point) | ||
} | ||
|
||
type batcher struct { | ||
re *RunEnv | ||
client client.Client | ||
length int | ||
interval time.Duration | ||
retryOpts []retry.Option | ||
|
||
writeCh chan *client.Point | ||
flushCh chan struct{} | ||
doneCh chan struct{} | ||
|
||
pending []*client.Point | ||
sending []*client.Point | ||
sendRes chan error | ||
doneErr chan error | ||
} | ||
|
||
func newBatcher(re *RunEnv, cli client.Client, length int, interval time.Duration, retry ...retry.Option) *batcher { | ||
b := &batcher{ | ||
re: re, | ||
client: cli, | ||
length: length, | ||
interval: interval, | ||
retryOpts: retry, | ||
|
||
writeCh: make(chan *client.Point), | ||
flushCh: make(chan struct{}, 1), | ||
sendRes: make(chan error, 1), | ||
doneCh: make(chan struct{}), | ||
doneErr: make(chan error), | ||
|
||
pending: nil, | ||
sending: nil, | ||
} | ||
|
||
go b.background() | ||
|
||
return b | ||
} | ||
|
||
func (b *batcher) background() { | ||
tick := time.NewTicker(b.interval) | ||
defer tick.Stop() | ||
|
||
attemptFlush := func() { | ||
if b.sending != nil { | ||
// there's already a flush taking place. | ||
return | ||
} | ||
select { | ||
case b.flushCh <- struct{}{}: | ||
default: | ||
// there's a flush queued to be accepted. | ||
} | ||
} | ||
|
||
for { | ||
select { | ||
case p := <-b.writeCh: | ||
b.pending = append(b.pending, p) | ||
if len(b.pending) >= b.length { | ||
attemptFlush() | ||
} | ||
|
||
case err := <-b.sendRes: | ||
if err == nil { | ||
b.pending = b.pending[len(b.sending):] | ||
b.re.RecordMessage("influxdb: uploaded %d points", len(b.sending)) | ||
} else { | ||
b.re.RecordMessage("influxdb: failed to upload %d points; err: %s", len(b.sending), err) | ||
} | ||
b.sending = nil | ||
if len(b.pending) >= b.length { | ||
attemptFlush() | ||
} | ||
|
||
case <-tick.C: | ||
attemptFlush() | ||
|
||
case <-b.flushCh: | ||
if b.sending != nil { | ||
continue | ||
} | ||
l := len(b.pending) | ||
if l == 0 { | ||
continue | ||
} | ||
if l > b.length { | ||
l = b.length | ||
} | ||
b.sending = b.pending[:l] | ||
go b.send() | ||
|
||
case <-b.doneCh: | ||
if b.sending != nil { | ||
// we are currently sending, wait for the send to finish first. | ||
if err := <-b.sendRes; err == nil { | ||
b.pending = b.pending[len(b.sending):] | ||
b.re.RecordMessage("influxdb: uploaded %d points", len(b.sending)) | ||
} else { | ||
b.re.RecordMessage("influxdb: failed to upload %d points; err: %s", len(b.sending), err) | ||
} | ||
} | ||
|
||
var err error | ||
if len(b.pending) > 0 { | ||
// send all remaining data at once. | ||
b.sending = b.pending | ||
go b.send() | ||
err = <-b.sendRes | ||
if err == nil { | ||
b.re.RecordMessage("influxdb: uploaded %d points", len(b.sending)) | ||
} else { | ||
b.re.RecordMessage("influxdb: failed to upload %d points; err: %s", len(b.sending), err) | ||
} | ||
b.sending = nil | ||
} | ||
b.doneErr <- err | ||
return | ||
} | ||
} | ||
} | ||
|
||
func (b *batcher) WritePoint(p *client.Point) { | ||
b.writeCh <- p | ||
} | ||
|
||
// Close flushes any remaining points and returns any errors from the final flush. | ||
func (b *batcher) Close() error { | ||
select { | ||
case _, ok := <-b.doneCh: | ||
if !ok { | ||
return nil | ||
} | ||
default: | ||
} | ||
close(b.doneCh) | ||
return <-b.doneErr | ||
} | ||
|
||
func (b *batcher) send() { | ||
points, err := client.NewBatchPoints(client.BatchPointsConfig{Database: "testground"}) | ||
if err != nil { | ||
b.sendRes <- err | ||
return | ||
} | ||
|
||
for _, p := range b.sending { | ||
points.AddPoint(p) | ||
} | ||
|
||
err = retry.Do(func() error { return b.client.Write(points) }, b.retryOpts...) | ||
b.sendRes <- err | ||
} | ||
|
||
type nilBatcher struct { | ||
client.Client | ||
} | ||
|
||
func (n *nilBatcher) WritePoint(p *client.Point) { | ||
bp, _ := client.NewBatchPoints(client.BatchPointsConfig{Database: "testground"}) | ||
bp.AddPoint(p) | ||
_ = n.Write(bp) | ||
} | ||
|
||
func (n *nilBatcher) Close() error { | ||
return nil | ||
} | ||
|
||
var _ Batcher = (*nilBatcher)(nil) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to specify the precision here? I actually don't know.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default precision is
ns
, that's why I didn't specify it ;-)