Skip to content

Commit

Permalink
Synthetic Monitoring: Configure with access token (#465)
Browse files Browse the repository at this point in the history
Rather than finding stack information and installing SM, a user can get the token directly from the SM config page

The Terraform provider is configured the same way (as well as the config export plugin)
  • Loading branch information
julienduchesne authored Jul 17, 2024
1 parent ca90aaa commit ea67ac7
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 43 deletions.
11 changes: 4 additions & 7 deletions docs/content/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,11 @@ grr config set mimir.api-key abcdef12345 # Authentication token (if you are usin
To interact with Grafana Synthetic Monitoring, you must configure the below settings:

```sh
grr config set synthetic-monitoring.token abcdef123456 # API key (must have MetricsPublisher permissions)
grr config set synthetic-monitoring.stack-id 123 # Grafana stack ID
grr config set synthetic-monitoring.metrics-id 123 # Metrics instance ID
grr config set synthetic-monitoring.logs-id 123 # Logs instance ID
grr config set synthetic-monitoring.url https://synthetic-monitoring-api.grafana.net # Synthetic Monitoring instance URL
grr config set synthetic-monitoring.access-token abcdef123456
grr config set synthetic-monitoring.url https://synthetic-monitoring-api.grafana.net
```
Your stack ID is the number at the end of the url when you view your Grafana instance details, ie. `grafana.com/orgs/myorg/stacks/123456` would be `123456`. Your metrics and logs ID's are the `User` when you view your Prometheus or Loki instance details in Grafana Cloud.
You can find your instance URL under your Synthetic Monitoring configuration.

You can find the URL and access token in the Synthetic Monitoring plugin's config page in Grafana.

## Configuring Targets
Grizzly supports a number of resource types (`grr providers` will list those supported). Often, however, we do not
Expand Down
44 changes: 23 additions & 21 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ func override(v *viper.Viper) {
"grafana.user": "GRAFANA_USER",
"grafana.token": "GRAFANA_TOKEN",

"synthetic-monitoring.token": "GRAFANA_SM_TOKEN",
"synthetic-monitoring.stack-id": "GRAFANA_SM_STACK_ID",
"synthetic-monitoring.logs-id": "GRAFANA_SM_LOGS_ID",
"synthetic-monitoring.metrics-id": "GRAFANA_SM_METRICS_ID",
"synthetic-monitoring.url": "GRAFANA_SM_URL",
"synthetic-monitoring.access-token": "GRAFANA_SM_ACCESS_TOKEN",
"synthetic-monitoring.token": "GRAFANA_SM_TOKEN",
"synthetic-monitoring.stack-id": "GRAFANA_SM_STACK_ID",
"synthetic-monitoring.logs-id": "GRAFANA_SM_LOGS_ID",
"synthetic-monitoring.metrics-id": "GRAFANA_SM_METRICS_ID",
"synthetic-monitoring.url": "GRAFANA_SM_URL",

"mimir.address": "MIMIR_ADDRESS",
"mimir.tenant-id": "MIMIR_TENANT_ID",
Expand Down Expand Up @@ -161,22 +162,23 @@ func CurrentContext() (*Context, error) {
}

var acceptableKeys = map[string]string{
"grafana.url": "string",
"grafana.token": "string",
"grafana.user": "string",
"grafana.insecure-skip-verify": "bool",
"grafana.tls-host": "string",
"mimir.address": "string",
"mimir.tenant-id": "string",
"mimir.api-key": "string",
"synthetic-monitoring.token": "string",
"synthetic-monitoring.stack-id": "int",
"synthetic-monitoring.metrics-id": "int",
"synthetic-monitoring.logs-id": "int",
"synthetic-monitoring.url": "string",
"targets": "[]string",
"output-format": "string",
"only-spec": "bool",
"grafana.url": "string",
"grafana.token": "string",
"grafana.user": "string",
"grafana.insecure-skip-verify": "bool",
"grafana.tls-host": "string",
"mimir.address": "string",
"mimir.tenant-id": "string",
"mimir.api-key": "string",
"synthetic-monitoring.access-token": "string",
"synthetic-monitoring.token": "string",
"synthetic-monitoring.stack-id": "int",
"synthetic-monitoring.metrics-id": "int",
"synthetic-monitoring.logs-id": "int",
"synthetic-monitoring.url": "string",
"targets": "[]string",
"output-format": "string",
"only-spec": "bool",
}

func Get(path, outputFormat string) (string, error) {
Expand Down
12 changes: 7 additions & 5 deletions pkg/config/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ type MimirTLSConfig struct {
}

type SyntheticMonitoringConfig struct {
Token string `yaml:"token" mapstructure:"token"`
StackID int64 `yaml:"stack-id" mapstructure:"stack-id"`
LogsID int64 `yaml:"logs-id" mapstructure:"logs-id"`
MetricsID int64 `yaml:"metrics-id" mapstructure:"metrics-id"`
URL string `yaml:"url" mapstructure:"url"`
URL string `yaml:"url" mapstructure:"url"`
// SM can be configured with a metrics publisher token (and various stack information) or an access token gotten from the UI
Token string `yaml:"token" mapstructure:"token"`
StackID int64 `yaml:"stack-id" mapstructure:"stack-id"`
LogsID int64 `yaml:"logs-id" mapstructure:"logs-id"`
MetricsID int64 `yaml:"metrics-id" mapstructure:"metrics-id"`
AccessToken string `yaml:"access-token" mapsructure:"access-token"`
}

type Context struct {
Expand Down
24 changes: 14 additions & 10 deletions pkg/syntheticmonitoring/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,17 @@ func (p *Provider) Validate() error {
if p.config.URL == "" {
p.config.URL = "https://synthetic-monitoring-api.grafana.net"
}
if p.config.StackID == 0 {
return fmt.Errorf("stack id is not set")
}
if p.config.MetricsID == 0 {
return fmt.Errorf("metrics id is not set")
}
if p.config.LogsID == 0 {
return fmt.Errorf("logs id is not set")

smInstallationConfigured := p.config.StackID != 0 && p.config.MetricsID != 0 && p.config.LogsID != 0 && p.config.Token != ""

if p.config.AccessToken != "" && smInstallationConfigured {
return fmt.Errorf("both access token and stack configuration (stack id, metrics id, logs id, token) are set. Only one can be used")
}
if p.config.Token == "" {
return fmt.Errorf("token is not set")

if p.config.AccessToken == "" && !smInstallationConfigured {
return fmt.Errorf("neither access token nor stack configuration (stack id, metrics id, logs id, token) are set. One must be set")
}

return nil
}

Expand Down Expand Up @@ -79,6 +78,11 @@ func (p *Provider) Client() (*smapi.Client, error) {
return nil, err
}

if p.config.AccessToken != "" {
smClient := smapi.NewClient(p.config.URL, p.config.AccessToken, client)
return smClient, nil
}

smClient := smapi.NewClient(p.config.URL, "", client)

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
Expand Down

0 comments on commit ea67ac7

Please sign in to comment.