-
Notifications
You must be signed in to change notification settings - Fork 490
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move alerta api token to header and add option to skip TLS verification. #1053
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall this is good. Just a few things have been commented below.
In addition the tests in integrations/streamer_test.go
will need to be updated to check for the token in the header instead of the query parameters.
Thanks!
@@ -11,6 +11,8 @@ type Config struct { | |||
Enabled bool `toml:"enabled" override:"enabled"` | |||
// The Alerta URL. | |||
URL string `toml:"url" override:"url"` | |||
// Whether to skip the tls verification of the alerta host | |||
TLSSkipVerify bool `toml:"tls_skip_verify" override:"tls_skip_verify"` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you rename this option to InsecureSkipVerify
and insecure-skip-verify
so that it is consistent with other similar config options?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup no problem.
if err != nil { | ||
return err | ||
} | ||
|
||
resp, err := http.Post(url, "application/json", post) | ||
client := &http.Client{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you create the client in the NewService
method so that we don't have to create a new client for each new request?
Since the Alerta config can change dynamically you will also need to create a new client in the Update
method if the config changed. And that also means that you will need to synchronize access to the client. For that I would recommend using atomic.Value, see how the config
field is access for an example.
Let me know if I can clarify any of that better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, good catch. I'm pretty sure I understand how the update method works. Let me fix that up.
Updated to address above issues. |
Here is the failing test from
|
Tests passes this time! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes look good. One last thing is the change to reuse the same client creates a race condition. I have commented in-line below on how to fix it. Should be a simple change.
Thanks again!
@@ -17,13 +17,19 @@ import ( | |||
type Service struct { | |||
configValue atomic.Value | |||
logger *log.Logger | |||
client *http.Client |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The client field here should be an atomic.Value
type and to be consistent we should call it clientValue
.
} | ||
|
||
func NewService(c Config, l *log.Logger) *Service { | ||
s := &Service{ | ||
logger: l, | ||
} | ||
s.configValue.Store(c) | ||
s.client = &http.Client{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here, use the Store
method to store the new client. Much like the configValue line above.
@@ -95,7 +101,13 @@ func (s *Service) Update(newConfig []interface{}) error { | |||
return fmt.Errorf("expected config object to be of type %T, got %T", c, newConfig[0]) | |||
} else { | |||
s.configValue.Store(c) | |||
s.client = &http.Client{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same goes here, we should use the Store
method.
if err != nil { | ||
return err | ||
} | ||
|
||
resp, err := http.Post(url, "application/json", post) | ||
resp, err := s.client.Do(req) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lastly this becomes:
client := s.clientValue.Get().(*http.Client)
resp, err := client.Do(req)
All of these changes avoid a race condition on the client
field, synchronizing access to the field.
Race condition resolved. |
Move alerta api token to header and add option to skip TLS verification.
Thanks for the help! |
Required for all non-trivial PRs
Resolves issue #1052
I have tested this change live with the custom kapacitor docker image allen13/kapacitor:latest