Skip to content

Commit

Permalink
Allow configuring airbrake via environment variable
Browse files Browse the repository at this point in the history
Allow setting the airbrake api key via the AIRBRAKE_API_KEY environment
variable. If this variable is set, it will override the
field in the airbrake section of config file.
  • Loading branch information
agbpatro committed Apr 5, 2024
1 parent 4582a49 commit 4a1806a
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 12 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ Data is encapsulated into protobuf messages of different types. Protos can be re
```sh
make generate-protos
```
## Airbrake
Fleet telemetry allows you to monitor errors using [airbrake](https://www.airbrake.io/error-monitoring). The integration test runs fleet telemetry with [errbit](https://github.com/errbit/errbit), which is an airbrake compliant self-hosted error catcher. You can set a project key for airbrake using either the config file or via an environment variable `AIRBRAKE_PROJECT_KEY`.

# Testing

Expand Down
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func startServer(config *config.Config, logger *logrus.Logger) (err error) {
logger.ActivityLog("starting_server", nil)
registry := streaming.NewSocketRegistry()

airbrakeNotifier, err := config.CreateAirbrakeNotifier()
airbrakeNotifier, _, err := config.CreateAirbrakeNotifier(logger)
if err != nil {
return err
}
Expand Down
29 changes: 22 additions & 7 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ import (
"github.com/teslamotors/fleet-telemetry/telemetry"
)

const (
airbrakeProjectKeyEnv = "AIRBRAKE_PROJECT_KEY"
)

// Config object for server
type Config struct {
// Host is the telemetry server hostname
Expand Down Expand Up @@ -148,7 +152,7 @@ type TLS struct {
ServerKey string `json:"server_key"`
}

// ExtractServiceTLSConfig return the TLS config needed for connecting with airbrake server
// AirbrakeTlsConfig return the TLS config needed for connecting with airbrake server
func (c *Config) AirbrakeTlsConfig() (*tls.Config, error) {
if c.Airbrake.TLS == nil {
return nil, nil
Expand Down Expand Up @@ -347,13 +351,13 @@ func (c *Config) CreateKinesisStreamMapping(recordNames []string) map[string]str
}

// CreateAirbrakeNotifier intializes an airbrake notifier with standard configs
func (c *Config) CreateAirbrakeNotifier() (*githubairbrake.Notifier, error) {
func (c *Config) CreateAirbrakeNotifier(logger *logrus.Logger) (*githubairbrake.Notifier, *githubairbrake.NotifierOptions, error) {
if c.Airbrake == nil {
return nil, nil
return nil, nil, nil
}
tlsConfig, err := c.AirbrakeTlsConfig()
if err != nil {
return nil, err
return nil, nil, err
}
transport := &http.Transport{
TLSClientConfig: tlsConfig,
Expand All @@ -363,8 +367,18 @@ func (c *Config) CreateAirbrakeNotifier() (*githubairbrake.Notifier, error) {
Timeout: 10 * time.Second,
}
errbitHost := c.Airbrake.Host
projectKey := c.Airbrake.ProjectKey
return githubairbrake.NewNotifierWithOptions(&githubairbrake.NotifierOptions{
projectKey, ok := os.LookupEnv(airbrakeProjectKeyEnv)
logInfo := logrus.LogInfo{}
if ok {
logInfo["source"] = "environment_variable"
logInfo["env_key"] = airbrakeProjectKeyEnv

} else {
projectKey = c.Airbrake.ProjectKey
logInfo["source"] = "config_file"
}
logger.ActivityLog("airbrake_configured", logInfo)
options := &githubairbrake.NotifierOptions{
Host: errbitHost,
RemoteConfigHost: errbitHost,
DisableRemoteConfig: true,
Expand All @@ -374,5 +388,6 @@ func (c *Config) CreateAirbrakeNotifier() (*githubairbrake.Notifier, error) {
ProjectKey: projectKey,
Environment: c.Airbrake.Environment,
HTTPClient: httpClient,
}), nil
}
return githubairbrake.NewNotifierWithOptions(options), options, nil
}
27 changes: 23 additions & 4 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,29 @@ var _ = Describe("Test full application config", func() {
})
})

Context("configure airbrake", func() {
It("gets config from file", func() {
config, err := loadTestApplicationConfig(TestAirbrakeConfig)
Expect(err).NotTo(HaveOccurred())

_, options, err := config.CreateAirbrakeNotifier(log)
Expect(err).NotTo(HaveOccurred())
Expect(options.ProjectKey).To(Equal("test1"))
})

It("gets config from env variable", func() {
projectKey := "environmentProjectKey"
err := os.Setenv("AIRBRAKE_PROJECT_KEY", projectKey)
Expect(err).NotTo(HaveOccurred())
config, err := loadTestApplicationConfig(TestAirbrakeConfig)
Expect(err).NotTo(HaveOccurred())

_, options, err := config.CreateAirbrakeNotifier(log)
Expect(err).NotTo(HaveOccurred())
Expect(options.ProjectKey).To(Equal(projectKey))
})
})

Context("configure kinesis", func() {
It("returns an error if kinesis isn't included", func() {
log, _ := logrus.NoOpLogger()
Expand Down Expand Up @@ -180,10 +203,6 @@ var _ = Describe("Test full application config", func() {
Expect(err).NotTo(HaveOccurred())
})

AfterEach(func() {
os.Clearenv()
})

It("pubsub does not work when both the environment variables are set", func() {
log, _ := logrus.NoOpLogger()
_ = os.Setenv("PUBSUB_EMULATOR_HOST", "some_url")
Expand Down
30 changes: 30 additions & 0 deletions config/test_configs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,33 @@ const TestTransmitDecodedRecords = `
}
}
`

const TestAirbrakeConfig = `
{
"host": "127.0.0.1",
"port": 443,
"status_port": 8080,
"namespace": "tesla_telemetry",
"kafka": {
"bootstrap.servers": "some.broker1:9093,some.broker1:9093",
"ssl.ca.location": "kafka.ca",
"ssl.certificate.location": "kafka.crt",
"ssl.key.location": "kafka.key",
"queue.buffering.max.messages": 1000000
},
"records": {
"FS": ["kafka"]
},
"tls": {
"ca_file": "tesla.ca",
"server_cert": "your_own_cert.crt",
"server_key": "your_own_key.key"
},
"airbrake": {
"project_id": 1,
"project_key": "test1",
"environment": "integration",
"host": "http://errbit-test.example.com"
}
}
`

0 comments on commit 4a1806a

Please sign in to comment.