-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #845 from newrelic/feat/validate-config
(feat): validate CLI config
- Loading branch information
Showing
17 changed files
with
364 additions
and
149 deletions.
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
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,43 @@ | ||
package diagnose | ||
|
||
import ( | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/newrelic/newrelic-cli/internal/client" | ||
"github.com/newrelic/newrelic-cli/internal/utils" | ||
"github.com/newrelic/newrelic-client-go/newrelic" | ||
) | ||
|
||
var cmdValidate = &cobra.Command{ | ||
Use: "validate", | ||
Short: "Validate your CLI configuration and connectivity", | ||
Long: `Validate your CLI configuration and connectivity. | ||
Checks the configuration in the default or specified configuation profile by sending | ||
data to the New Relic platform and verifying that it has been received.`, | ||
Example: "\tnewrelic diagnose validate", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
client.WithClient(func(nrClient *newrelic.NewRelic) { | ||
v := NewConfigValidator(nrClient) | ||
err := v.ValidateConfig(utils.SignalCtx) | ||
if err != nil { | ||
if err == ErrDiscovery { | ||
log.Fatal("Failed to detect your system's hostname. Please contact New Relic support.") | ||
} | ||
if err == ErrPostEvent { | ||
log.Fatal("There was a failure posting data to New Relic.") | ||
} | ||
if err == ErrValidation { | ||
log.Fatal("There was a failure locating the data that was posted to New Relic.") | ||
} | ||
|
||
log.Fatal(err) | ||
} | ||
}) | ||
}, | ||
} | ||
|
||
func init() { | ||
Command.AddCommand(cmdValidate) | ||
} |
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,87 @@ | ||
package diagnose | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"reflect" | ||
|
||
"github.com/google/uuid" | ||
log "github.com/sirupsen/logrus" | ||
|
||
"github.com/shirou/gopsutil/host" | ||
|
||
"github.com/newrelic/newrelic-cli/internal/credentials" | ||
"github.com/newrelic/newrelic-cli/internal/utils/validation" | ||
"github.com/newrelic/newrelic-client-go/newrelic" | ||
) | ||
|
||
const ( | ||
validationEventType = "NrIntegrationError" | ||
) | ||
|
||
type ConfigValidator struct { | ||
client *newrelic.NewRelic | ||
*validation.PollingNRQLValidator | ||
} | ||
|
||
type ValidationTracerEvent struct { | ||
EventType string `json:"eventType"` | ||
Hostname string `json:"hostname"` | ||
Purpose string `json:"purpose"` | ||
GUID string `json:"guid"` | ||
} | ||
|
||
func NewConfigValidator(client *newrelic.NewRelic) *ConfigValidator { | ||
v := validation.NewPollingNRQLValidator(&client.Nrdb) | ||
v.MaxAttempts = 20 | ||
|
||
return &ConfigValidator{ | ||
client: client, | ||
PollingNRQLValidator: v, | ||
} | ||
} | ||
|
||
func (c *ConfigValidator) ValidateConfig(ctx context.Context) error { | ||
defaultProfile := credentials.DefaultProfile() | ||
|
||
i, err := host.InfoWithContext(ctx) | ||
if err != nil { | ||
log.Error(err) | ||
return ErrDiscovery | ||
} | ||
|
||
evt := ValidationTracerEvent{ | ||
EventType: validationEventType, | ||
Hostname: i.Hostname, | ||
Purpose: "New Relic CLI configuration validation", | ||
GUID: uuid.NewString(), | ||
} | ||
|
||
log.Printf("Sending tracer event to New Relic.") | ||
|
||
if err = c.client.Events.CreateEvent(defaultProfile.AccountID, evt); err != nil { | ||
log.Error(reflect.TypeOf(err)) | ||
log.Error(err) | ||
return ErrPostEvent | ||
} | ||
|
||
query := fmt.Sprintf(` | ||
FROM %s | ||
SELECT count(*) | ||
WHERE hostname LIKE '%s%%' | ||
AND guid = '%s' | ||
SINCE 10 MINUTES AGO | ||
`, evt.EventType, evt.Hostname, evt.GUID) | ||
|
||
if _, err = c.Validate(ctx, query); err != nil { | ||
log.Error(err) | ||
err = ErrValidation | ||
} | ||
|
||
return err | ||
} | ||
|
||
var ErrDiscovery = errors.New("discovery failed") | ||
var ErrPostEvent = errors.New("posting an event failed") | ||
var ErrValidation = errors.New("validation failed") |
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,17 @@ | ||
package discovery | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/newrelic/newrelic-cli/internal/install/types" | ||
) | ||
|
||
type NoOpProcessFilterer struct{} | ||
|
||
func NewNoOpProcessFilterer() *NoOpProcessFilterer { | ||
return &NoOpProcessFilterer{} | ||
} | ||
|
||
func (f *NoOpProcessFilterer) filter(ctx context.Context, processes []types.GenericProcess, manifest types.DiscoveryManifest) ([]types.MatchedProcess, error) { | ||
return []types.MatchedProcess{}, nil | ||
} |
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
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
Oops, something went wrong.