From e087e9458260581297e47dad5cbb52e23e24c036 Mon Sep 17 00:00:00 2001 From: evan-forbes Date: Thu, 9 Mar 2023 11:54:06 -0600 Subject: [PATCH] feat: add validate basic and IsCollecting method --- pkg/remote/README.md | 12 ++++++------ pkg/remote/client.go | 31 +++++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/pkg/remote/README.md b/pkg/remote/README.md index 218868d86b..b84461dbab 100644 --- a/pkg/remote/README.md +++ b/pkg/remote/README.md @@ -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 @@ -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" ) ``` diff --git a/pkg/remote/client.go b/pkg/remote/client.go index 237b744d02..b2ff4e3285 100644 --- a/pkg/remote/client.go +++ b/pkg/remote/client.go @@ -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, } @@ -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)