Skip to content

Commit

Permalink
telemetry: add no-op functionality
Browse files Browse the repository at this point in the history
Currently, when telemetry initialization fails, this is a hard failure
for the `crc` command and daemon, and they exit.
This commit handles this more gracefully, when there's a telemetry
initialization failure, an empty `Noop Client` instance is returned,
and all `Client` methods check for this `Noop` instance and return
early if needed.
  • Loading branch information
redbeam authored and praveenkumar committed Apr 30, 2024
1 parent ff8c976 commit ec97112
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
3 changes: 2 additions & 1 deletion cmd/crc/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ func init() {

// Initiate segment client
if segmentClient, err = segment.NewClient(config, httpproxy.HTTPTransport()); err != nil {
logging.Fatal(err.Error())
logging.Warn(err.Error())
logging.Warn("Error during segment client initialization, telemetry will be unavailable in this session")
}

// subcommands
Expand Down
30 changes: 26 additions & 4 deletions pkg/crc/segment/segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,24 @@ type Client struct {
identifyHashPath string
}

var NoopClient = &Client{}

func NewClient(config *crcConfig.Config, transport http.RoundTripper) (*Client, error) {
return newCustomClient(config, transport,
client, err := newCustomClient(config, transport,
filepath.Join(constants.GetHomeDir(), ".redhat", "anonymousId"),
filepath.Join(constants.CrcBaseDir, "segmentIdentifyHash"),
analytics.DefaultEndpoint)
if err != nil {
// Return empty client (noop client) -- telemetry will not be available
return NoopClient, err
}
return client, nil
}

func newCustomClient(config *crcConfig.Config, transport http.RoundTripper, telemetryFilePath, identifyHashFilePath, segmentEndpoint string) (*Client, error) {
userID, err := getUserIdentity(telemetryFilePath)
if err != nil {
return nil, err
return NoopClient, err
}
client, err := analytics.NewWithConfig(WriteKey, analytics.Config{
Endpoint: segmentEndpoint,
Expand All @@ -60,12 +67,12 @@ func newCustomClient(config *crcConfig.Config, transport http.RoundTripper, tele
Transport: transport,
})
if err != nil {
return nil, err
return NoopClient, err
}

identifyHash, err := readIdentifyHash(identifyHashFilePath)
if err != nil {
return nil, err
return NoopClient, err
}

return &Client{
Expand All @@ -79,15 +86,24 @@ func newCustomClient(config *crcConfig.Config, transport http.RoundTripper, tele
}

func (c *Client) Close() error {
if c == NoopClient {
return nil
}
return c.segmentClient.Close()
}

func (c *Client) UploadAction(action, source, status string) error {
if c == NoopClient {
return nil
}
return c.upload(action, baseProperties(source).
Set("status", status))
}

func (c *Client) UploadCmd(ctx context.Context, action string, duration time.Duration, err error) error {
if c == NoopClient {
return nil
}
return c.upload(action, properties(ctx, err, duration))
}

Expand Down Expand Up @@ -159,13 +175,19 @@ func writeIdentifyHash(client *Client) error {
}

func (c *Client) identifyNew() *analytics.Identify {
if c == NoopClient {
return &analytics.Identify{}
}
return &analytics.Identify{
UserId: c.userID,
Traits: addConfigTraits(c.config, traits()),
}
}

func (c *Client) upload(action string, a analytics.Properties) error {
if c == NoopClient {
return nil
}
if c.config.Get(crcConfig.ConsentTelemetry).AsString() != "yes" {
return nil
}
Expand Down

0 comments on commit ec97112

Please sign in to comment.