Skip to content

Commit

Permalink
feat: add validate basic and IsCollecting method
Browse files Browse the repository at this point in the history
  • Loading branch information
evan-forbes committed Mar 9, 2023
1 parent 014b9db commit e087e94
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
12 changes: 6 additions & 6 deletions pkg/remote/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ example, we're pushing a point in the consensus reactor to measure exactly when
each step of consensus is reached for each node.

```go
cs.eventCollector.WritePoint("consensus", map[string]interface{}{
"height": rs.Height,
"round": rs.Round,
"step": rs.Step,
})
if cs.eventCollector.IsCollecting() {
cs.eventCollector.WritePoint("consensus", map[string]interface{}{
"roundData": []interface{}{rs.Height, rs.Round, rs.Step},
})
}
```

Using this method enforces the typical schema, where we are tagging (aka
Expand All @@ -41,7 +41,7 @@ from(bucket: "e2e")
|> range(start: -1h)
|> filter(
fn: (r) => r["_measurement"] == "consensus"
and r.chain_id == "ci"
and r.chain_id == "ci-YREG8X"
and r.node_id == "0b529c309608172a29c49979394734260b42acfb"
)
```
Expand Down
31 changes: 29 additions & 2 deletions pkg/remote/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,33 @@ type EventCollectorConfig struct {
BatchSize int `mapstructure:"batch_size"`
}

// ValidateBasic performs basic validation on the config.
func (c *EventCollectorConfig) ValidateBasic() error {
// if there is not URL configured, then we do not need to validate the rest
// of the config because we are not connecting.
if c.URL == "" {
return nil
}
if c.Token == "" {
return fmt.Errorf("token is required")
}
if c.Org == "" {
return fmt.Errorf("org is required")
}
if c.Bucket == "" {
return fmt.Errorf("bucket is required")
}
if c.BatchSize <= 0 {
return fmt.Errorf("batch size must be greater than 0")
}
return nil
}

// DefaultEventCollectorConfig returns the default configuration.
func DefaultEventCollectorConfig() *EventCollectorConfig {
return &EventCollectorConfig{
URL: "",
Org: "core/app",
Org: "celestia",
Bucket: "e2e",
BatchSize: 10,
}
Expand Down Expand Up @@ -125,13 +147,18 @@ func (c *Client) logErrors(logger log.Logger) {
}
}

// IsCollecting returns true if the client is collecting events.
func (c *Client) IsCollecting() bool {
return c.Client != nil
}

// WritePoint async writes a point to influxdb. To enforce the schema, it
// automatically adds the chain_id and node_id tags, along with setting the
// timestamp to the current time. If the underlying client is nil, it does
// nothing. The "table" arg is used as the influxdb "measurement" for the point.
// If other tags are needed, use WriteCustomPoint.
func (c *Client) WritePoint(table string, fields map[string]interface{}) {
if c.Client == nil {
if !c.IsCollecting() {
return
}
writeAPI := c.Client.WriteAPI(c.Org, c.Bucket)
Expand Down

0 comments on commit e087e94

Please sign in to comment.