-
Notifications
You must be signed in to change notification settings - Fork 182
feat(newrelic): APM application id can be looked up by name #361
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -6,6 +6,7 @@ import ( | |||||||||||||||||||||||||||||||||||||||||||||||
"errors" | ||||||||||||||||||||||||||||||||||||||||||||||||
"fmt" | ||||||||||||||||||||||||||||||||||||||||||||||||
"net/http" | ||||||||||||||||||||||||||||||||||||||||||||||||
"strconv" | ||||||||||||||||||||||||||||||||||||||||||||||||
"strings" | ||||||||||||||||||||||||||||||||||||||||||||||||
texttemplate "text/template" | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -27,8 +28,10 @@ type NewrelicNotification struct { | |||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
var ( | ||||||||||||||||||||||||||||||||||||||||||||||||
ErrMissingConfig = errors.New("config is missing") | ||||||||||||||||||||||||||||||||||||||||||||||||
ErrMissingApiKey = errors.New("apiKey is missing") | ||||||||||||||||||||||||||||||||||||||||||||||||
ErrMissingConfig = errors.New("config is missing") | ||||||||||||||||||||||||||||||||||||||||||||||||
ErrMissingApiKey = errors.New("apiKey is missing") | ||||||||||||||||||||||||||||||||||||||||||||||||
ErrAppIdMultipleMatches = errors.New("multiple matches found for application name") | ||||||||||||||||||||||||||||||||||||||||||||||||
ErrAppIdNoMatches = errors.New("no matches found for application name") | ||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
func (n *NewrelicNotification) GetTemplater(name string, f texttemplate.FuncMap) (Templater, error) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -109,6 +112,43 @@ type newrelicService struct { | |||||||||||||||||||||||||||||||||||||||||||||||
type newrelicDeploymentMarkerRequest struct { | ||||||||||||||||||||||||||||||||||||||||||||||||
Deployment NewrelicNotification `json:"deployment"` | ||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
type newrelicApplicationsResponse struct { | ||||||||||||||||||||||||||||||||||||||||||||||||
Applications []struct { | ||||||||||||||||||||||||||||||||||||||||||||||||
ID json.Number `json:"id"` | ||||||||||||||||||||||||||||||||||||||||||||||||
} `json:"applications"` | ||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
func (s newrelicService) getApplicationId(client *http.Client, appName string) (string, error) { | ||||||||||||||||||||||||||||||||||||||||||||||||
applicationsApi := fmt.Sprintf("%s/v2/applications.json?filter[name]=%s", s.opts.ApiURL, appName) | ||||||||||||||||||||||||||||||||||||||||||||||||
req, err := http.NewRequest(http.MethodGet, applicationsApi, nil) | ||||||||||||||||||||||||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||
return "", fmt.Errorf("Failed to create filtered application request: %s", err) | ||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
req.Header.Set("Content-Type", "application/json") | ||||||||||||||||||||||||||||||||||||||||||||||||
req.Header.Set("X-Api-Key", s.opts.ApiKey) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
resp, err := client.Do(req) | ||||||||||||||||||||||||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||
return "", err | ||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
defer resp.Body.Close() | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
var data newrelicApplicationsResponse | ||||||||||||||||||||||||||||||||||||||||||||||||
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||
return "", fmt.Errorf("Failed to decode applications response: %s", err) | ||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
if len(data.Applications) == 0 { | ||||||||||||||||||||||||||||||||||||||||||||||||
return "", ErrAppIdNoMatches | ||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
if len(data.Applications) > 1 { | ||||||||||||||||||||||||||||||||||||||||||||||||
return "", ErrAppIdMultipleMatches | ||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
return data.Applications[0].ID.String(), nil | ||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
func (s newrelicService) Send(notification Notification, dest Destination) error { | ||||||||||||||||||||||||||||||||||||||||||||||||
if s.opts.ApiKey == "" { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -142,7 +182,22 @@ func (s newrelicService) Send(notification Notification, dest Destination) error | |||||||||||||||||||||||||||||||||||||||||||||||
return err | ||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
markerApi := fmt.Sprintf(s.opts.ApiURL+"/v2/applications/%s/deployments.json", dest.Recipient) | ||||||||||||||||||||||||||||||||||||||||||||||||
var appId = dest.Recipient | ||||||||||||||||||||||||||||||||||||||||||||||||
if dest.Recipient != "" { | ||||||||||||||||||||||||||||||||||||||||||||||||
_, err := strconv.Atoi(dest.Recipient) | ||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+185
to
+187
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of reusing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for taking a look at this. I first interpreted your comment as changing the I had a look at the other services to see how they had handled service-specific changes in recipient fields: notifications-engine/pkg/services/rocketchat.go Lines 75 to 79 in f189f7e
notifications-engine/pkg/services/telegram.go Lines 28 to 45 in f189f7e
Could we do something similar with |
||||||||||||||||||||||||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||
log.Debugf( | ||||||||||||||||||||||||||||||||||||||||||||||||
"Recipient was provided by application name. Looking up the application id for %s", | ||||||||||||||||||||||||||||||||||||||||||||||||
dest.Recipient, | ||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||
appId, err = s.getApplicationId(client, dest.Recipient) | ||||||||||||||||||||||||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||
log.Errorf("Failed to lookup application %s by name: %s", dest.Recipient, err) | ||||||||||||||||||||||||||||||||||||||||||||||||
return err | ||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||
markerApi := fmt.Sprintf(s.opts.ApiURL+"/v2/applications/%s/deployments.json", appId) | ||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1, use |
||||||||||||||||||||||||||||||||||||||||||||||||
req, err := http.NewRequest(http.MethodPost, markerApi, bytes.NewBuffer(jsonValue)) | ||||||||||||||||||||||||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||
log.Errorf("Failed to create deployment marker request: %s", err) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -141,6 +141,59 @@ func TestSend_Newrelic(t *testing.T) { | |
} | ||
}) | ||
|
||
t.Run("recipient is application name", func(t *testing.T) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if r.URL.Path == "/v2/applications.json" { | ||
_, err := w.Write([]byte(`{ | ||
"applications": [ | ||
{"id": "123456789"} | ||
] | ||
}`)) | ||
if !assert.NoError(t, err) { | ||
t.FailNow() | ||
} | ||
return | ||
} | ||
|
||
b, err := io.ReadAll(r.Body) | ||
if !assert.NoError(t, err) { | ||
t.FailNow() | ||
} | ||
|
||
assert.Equal(t, "/v2/applications/123456789/deployments.json", r.URL.Path) | ||
assert.Equal(t, []string{"application/json"}, r.Header["Content-Type"]) | ||
assert.Equal(t, []string{"NRAK-5F2FIVA5UTA4FFDD11XCXVA7WPJ"}, r.Header["X-Api-Key"]) | ||
|
||
assert.JSONEq(t, `{ | ||
"deployment": { | ||
"revision": "2027ed5", | ||
"description": "message", | ||
"user": "[email protected]" | ||
} | ||
}`, string(b)) | ||
})) | ||
defer ts.Close() | ||
|
||
service := NewNewrelicService(NewrelicOptions{ | ||
ApiKey: "NRAK-5F2FIVA5UTA4FFDD11XCXVA7WPJ", | ||
ApiURL: ts.URL, | ||
}) | ||
err := service.Send(Notification{ | ||
Message: "message", | ||
Newrelic: &NewrelicNotification{ | ||
Revision: "2027ed5", | ||
User: "[email protected]", | ||
}, | ||
}, Destination{ | ||
Service: "newrelic", | ||
Recipient: "myapp", | ||
}) | ||
|
||
if !assert.NoError(t, err) { | ||
t.FailNow() | ||
} | ||
}) | ||
|
||
t.Run("missing config", func(t *testing.T) { | ||
service := NewNewrelicService(NewrelicOptions{ | ||
ApiKey: "NRAK-5F2FIVA5UTA4FFDD11XCXVA7WPJ", | ||
|
@@ -171,3 +224,69 @@ func TestSend_Newrelic(t *testing.T) { | |
} | ||
}) | ||
} | ||
|
||
func TestGetApplicationId(t *testing.T) { | ||
t.Run("successful lookup by application name", func(t *testing.T) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
assert.Equal(t, "/v2/applications.json", r.URL.Path) | ||
assert.Equal(t, "GET", r.Method) | ||
assert.Equal(t, "myapp", r.URL.Query().Get("filter[name]")) | ||
assert.Equal(t, []string{"application/json"}, r.Header["Content-Type"]) | ||
assert.Equal(t, []string{"NRAK-5F2FIVA5UTA4FFDD11XCXVA7WPJ"}, r.Header["X-Api-Key"]) | ||
|
||
_, err := w.Write([]byte(`{ | ||
"applications": [ | ||
{"id": "123456789"} | ||
] | ||
}`)) | ||
if !assert.NoError(t, err) { | ||
t.FailNow() | ||
} | ||
})) | ||
defer ts.Close() | ||
service := NewNewrelicService(NewrelicOptions{ | ||
ApiKey: "NRAK-5F2FIVA5UTA4FFDD11XCXVA7WPJ", | ||
ApiURL: ts.URL, | ||
}).(*newrelicService) | ||
appId, err := service.getApplicationId(http.DefaultClient, "myapp") | ||
assert.NoError(t, err) | ||
assert.Equal(t, "123456789", appId) | ||
}) | ||
|
||
t.Run("application not found", func(t *testing.T) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
_, err := w.Write([]byte(`{"applications": []}`)) | ||
if !assert.NoError(t, err) { | ||
t.FailNow() | ||
} | ||
})) | ||
defer ts.Close() | ||
service := NewNewrelicService(NewrelicOptions{ | ||
ApiKey: "NRAK-5F2FIVA5UTA4FFDD11XCXVA7WPJ", | ||
ApiURL: ts.URL, | ||
}).(*newrelicService) | ||
_, err := service.getApplicationId(http.DefaultClient, "myapp") | ||
assert.Equal(t, ErrAppIdNoMatches, err) | ||
}) | ||
|
||
t.Run("multiple matches for application name", func(t *testing.T) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
_, err := w.Write([]byte(`{ | ||
"applications": [ | ||
{"id": "123456789"}, | ||
{"id": "987654321"} | ||
] | ||
}`)) | ||
if !assert.NoError(t, err) { | ||
t.FailNow() | ||
} | ||
})) | ||
defer ts.Close() | ||
service := NewNewrelicService(NewrelicOptions{ | ||
ApiKey: "NRAK-5F2FIVA5UTA4FFDD11XCXVA7WPJ", | ||
ApiURL: ts.URL, | ||
}).(*newrelicService) | ||
_, err := service.getApplicationId(http.DefaultClient, "myapp") | ||
assert.Equal(t, ErrAppIdMultipleMatches, err) | ||
}) | ||
} |
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.
I'd use the
url
library to construct this, to avoidappName
potentially injecting arbitrary stuff