-
Notifications
You must be signed in to change notification settings - Fork 93
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 #4013 from replicatedhq/divolgin/sc-85778/endpoint…
…-in-kots-to-receive-dispatch-custom API to accept metrics from application
- Loading branch information
Showing
14 changed files
with
383 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package handlers | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"reflect" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/replicatedhq/kots/pkg/kotsadm" | ||
"github.com/replicatedhq/kots/pkg/logger" | ||
"github.com/replicatedhq/kots/pkg/replicatedapp" | ||
"github.com/replicatedhq/kots/pkg/session" | ||
) | ||
|
||
type SendCustomApplicationMetricsRequest struct { | ||
Data ApplicationMetricsData `json:"data"` | ||
} | ||
|
||
type ApplicationMetricsData map[string]interface{} | ||
|
||
func (h *Handler) SendCustomApplicationMetrics(w http.ResponseWriter, r *http.Request) { | ||
if kotsadm.IsAirgap() { | ||
w.WriteHeader(http.StatusForbidden) | ||
w.Write([]byte("This request cannot be satisfied in airgap mode")) | ||
return | ||
} | ||
|
||
license := session.ContextGetLicense(r) | ||
app := session.ContextGetApp(r) | ||
|
||
request := SendCustomApplicationMetricsRequest{} | ||
if err := json.NewDecoder(r.Body).Decode(&request); err != nil { | ||
logger.Error(errors.Wrap(err, "decode request")) | ||
w.WriteHeader(http.StatusBadRequest) | ||
return | ||
} | ||
|
||
if err := validateCustomMetricsData(request.Data); err != nil { | ||
w.WriteHeader(http.StatusBadRequest) | ||
w.Write([]byte(err.Error())) | ||
return | ||
} | ||
|
||
err := replicatedapp.SendApplicationMetricsData(license, app, request.Data) | ||
if err != nil { | ||
logger.Error(errors.Wrap(err, "set application data")) | ||
w.WriteHeader(http.StatusBadRequest) | ||
return | ||
} | ||
|
||
JSON(w, http.StatusOK, "") | ||
} | ||
|
||
func validateCustomMetricsData(data ApplicationMetricsData) error { | ||
if len(data) == 0 { | ||
return errors.New("no data provided") | ||
} | ||
|
||
for key, val := range data { | ||
valType := reflect.TypeOf(val) | ||
switch valType.Kind() { | ||
case reflect.Slice: | ||
return errors.Errorf("%s value is an array, only scalar values are allowed", key) | ||
case reflect.Array: | ||
return errors.Errorf("%s value is an array, only scalar values are allowed", key) | ||
case reflect.Map: | ||
return errors.Errorf("%s value is a map, only scalar values are allowed", key) | ||
} | ||
} | ||
|
||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package handlers | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"testing" | ||
|
||
gomock "github.com/golang/mock/gomock" | ||
"github.com/gorilla/mux" | ||
apptypes "github.com/replicatedhq/kots/pkg/app/types" | ||
"github.com/replicatedhq/kots/pkg/session" | ||
"github.com/replicatedhq/kotskinds/apis/kots/v1beta1" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_validateCustomMetricsData(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
data ApplicationMetricsData | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "all values are valid", | ||
data: ApplicationMetricsData{ | ||
"key1": "val1", | ||
"key2": 6, | ||
"key3": 6.6, | ||
"key4": true, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "no data", | ||
data: ApplicationMetricsData{}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "array value", | ||
data: ApplicationMetricsData{ | ||
"key1": 10, | ||
"key2": []string{"val1", "val2"}, | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "map value", | ||
data: ApplicationMetricsData{ | ||
"key1": 10, | ||
"key2": map[string]string{"key1": "val1"}, | ||
}, | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
err := validateCustomMetricsData(test.data) | ||
if test.wantErr { | ||
require.Error(t, err) | ||
} else { | ||
require.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func Test_SendCustomApplicationMetrics(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
req := require.New(t) | ||
customMetricsData := []byte(`{"data":{"key1_string":"val1","key2_int":5,"key3_float":1.5,"key4_numeric_string":"1.6"}}`) | ||
appID := "app-id-123" | ||
|
||
// Mock server side | ||
|
||
serverRouter := mux.NewRouter() | ||
server := httptest.NewServer(serverRouter) | ||
defer server.Close() | ||
|
||
serverRouter.Methods("POST").Path("/application/custom-metrics").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
body, err := io.ReadAll(r.Body) | ||
req.NoError(err) | ||
req.Equal(string(customMetricsData), string(body)) | ||
req.Equal(appID, r.Header.Get("X-Replicated-InstanceID")) | ||
w.WriteHeader(http.StatusOK) | ||
}) | ||
|
||
// Mock kotsadm side | ||
|
||
os.Setenv("USE_MOCK_REPORTING", "1") | ||
defer os.Unsetenv("USE_MOCK_REPORTING") | ||
|
||
handler := Handler{} | ||
clientWriter := httptest.NewRecorder() | ||
clientRequest := &http.Request{ | ||
Body: io.NopCloser(bytes.NewBuffer(customMetricsData)), | ||
} | ||
|
||
clientRequest = session.ContextSetLicense(clientRequest, &v1beta1.License{ | ||
Spec: v1beta1.LicenseSpec{ | ||
Endpoint: server.URL, | ||
}, | ||
}) | ||
clientRequest = session.ContextSetApp(clientRequest, &apptypes.App{ | ||
ID: appID, | ||
}) | ||
|
||
// Validate | ||
|
||
handler.SendCustomApplicationMetrics(clientWriter, clientRequest) | ||
|
||
req.Equal(http.StatusOK, clientWriter.Code) | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.