Skip to content
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

support scraping additional metrics #100

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ cmd/replicated/__debug_bin

# pact
pact_logs/
pact/log/
pacts/
1 change: 1 addition & 0 deletions chart/templates/replicated-secret.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ stringData:
{{- end }}
replicatedID: {{ .Values.replicatedID | default "" | quote }}
appID: {{ .Values.appID | default "" | quote }}
additionalMetricsEndpoint: {{ .Values.additionalMetricsEndpoint | default "" | quote }}
{{- if (.Values.integration).licenseID }}
integration-license-id: {{ .Values.integration.licenseID }}
{{- end }}
Expand Down
1 change: 1 addition & 0 deletions chart/values.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,4 @@ isAirgap: false
userAgent: ""
replicatedID: ""
appID: ""
additionalMetricsEndpoint: ""
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thoughts on supporting multiple endpoints here? asking because it's what copilot kept trying to guide me towards :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's actually a good idea.

35 changes: 18 additions & 17 deletions cmd/replicated/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,24 @@ func APICmd() *cobra.Command {
}

params := apiserver.APIServerParams{
Context: cmd.Context(),
LicenseBytes: []byte(replicatedConfig.License),
IntegrationLicenseID: integrationLicenseID,
LicenseFields: replicatedConfig.LicenseFields,
AppName: replicatedConfig.AppName,
ChannelID: replicatedConfig.ChannelID,
ChannelName: replicatedConfig.ChannelName,
ChannelSequence: replicatedConfig.ChannelSequence,
ReleaseSequence: replicatedConfig.ReleaseSequence,
ReleaseCreatedAt: replicatedConfig.ReleaseCreatedAt,
ReleaseNotes: replicatedConfig.ReleaseNotes,
VersionLabel: replicatedConfig.VersionLabel,
ReplicatedAppEndpoint: replicatedConfig.ReplicatedAppEndpoint,
StatusInformers: replicatedConfig.StatusInformers,
ReplicatedID: replicatedConfig.ReplicatedID,
AppID: replicatedConfig.AppID,
Namespace: namespace,
Context: cmd.Context(),
LicenseBytes: []byte(replicatedConfig.License),
IntegrationLicenseID: integrationLicenseID,
LicenseFields: replicatedConfig.LicenseFields,
AppName: replicatedConfig.AppName,
ChannelID: replicatedConfig.ChannelID,
ChannelName: replicatedConfig.ChannelName,
ChannelSequence: replicatedConfig.ChannelSequence,
ReleaseSequence: replicatedConfig.ReleaseSequence,
ReleaseCreatedAt: replicatedConfig.ReleaseCreatedAt,
ReleaseNotes: replicatedConfig.ReleaseNotes,
VersionLabel: replicatedConfig.VersionLabel,
ReplicatedAppEndpoint: replicatedConfig.ReplicatedAppEndpoint,
StatusInformers: replicatedConfig.StatusInformers,
ReplicatedID: replicatedConfig.ReplicatedID,
AppID: replicatedConfig.AppID,
AdditionalMetricsEndpoint: replicatedConfig.AdditionalMetricsEndpoint,
Namespace: namespace,
}
apiserver.Start(params)

Expand Down
4 changes: 4 additions & 0 deletions pact/heartbeat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func TestSendAppHeartbeat(t *testing.T) {
State: appstatetypes.StateMissing,
ResourceStates: []appstatetypes.ResourceState{},
})
mockStore.EXPECT().GetAdditionalMetricsEndpoint().Return("")
},
pactInteraction: func() {
pact.
Expand Down Expand Up @@ -108,6 +109,7 @@ func TestSendAppHeartbeat(t *testing.T) {
State: appstatetypes.StateMissing,
ResourceStates: []appstatetypes.ResourceState{},
})
mockStore.EXPECT().GetAdditionalMetricsEndpoint().Return("")
},
pactInteraction: func() {
pact.
Expand Down Expand Up @@ -159,6 +161,7 @@ func TestSendAppHeartbeat(t *testing.T) {
State: appstatetypes.StateMissing,
ResourceStates: []appstatetypes.ResourceState{},
})
mockStore.EXPECT().GetAdditionalMetricsEndpoint().Return("")
},
pactInteraction: func() {
pact.
Expand Down Expand Up @@ -210,6 +213,7 @@ func TestSendAppHeartbeat(t *testing.T) {
State: appstatetypes.StateMissing,
ResourceStates: []appstatetypes.ResourceState{},
})
mockStore.EXPECT().GetAdditionalMetricsEndpoint().Return("")
},
pactInteraction: func() {
pact.
Expand Down
31 changes: 16 additions & 15 deletions pkg/apiserver/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,21 @@ func bootstrap(params APIServerParams) error {
}

storeOptions := store.InitInMemoryStoreOptions{
License: verifiedLicense,
LicenseFields: params.LicenseFields,
AppName: params.AppName,
ChannelID: channelID,
ChannelName: channelName,
ChannelSequence: params.ChannelSequence,
ReleaseSequence: params.ReleaseSequence,
ReleaseCreatedAt: params.ReleaseCreatedAt,
ReleaseNotes: params.ReleaseNotes,
VersionLabel: params.VersionLabel,
ReplicatedAppEndpoint: params.ReplicatedAppEndpoint,
Namespace: params.Namespace,
ReplicatedID: replicatedID,
AppID: appID,
License: verifiedLicense,
LicenseFields: params.LicenseFields,
AppName: params.AppName,
ChannelID: channelID,
ChannelName: channelName,
ChannelSequence: params.ChannelSequence,
ReleaseSequence: params.ReleaseSequence,
ReleaseCreatedAt: params.ReleaseCreatedAt,
ReleaseNotes: params.ReleaseNotes,
VersionLabel: params.VersionLabel,
ReplicatedAppEndpoint: params.ReplicatedAppEndpoint,
Namespace: params.Namespace,
ReplicatedID: replicatedID,
AppID: appID,
AdditionalMetricsEndpoint: params.AdditionalMetricsEndpoint,
}
store.InitInMemory(storeOptions)

Expand All @@ -120,7 +121,7 @@ func bootstrap(params APIServerParams) error {
ChannelName: store.GetStore().GetChannelName(),
ChannelSequence: store.GetStore().GetChannelSequence(),
}
updates, err := upstream.GetUpdates(store.GetStore(), store.GetStore().GetLicense(), currentCursor)
updates, err := upstream.GetUpdates(store.GetStore(), clientset, store.GetStore().GetLicense(), currentCursor)
if err != nil {
return errors.Wrap(err, "failed to get updates")
}
Expand Down
35 changes: 18 additions & 17 deletions pkg/apiserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,24 @@ import (
)

type APIServerParams struct {
Context context.Context
LicenseBytes []byte
IntegrationLicenseID string
LicenseFields sdklicensetypes.LicenseFields
AppName string
ChannelID string
ChannelName string
ChannelSequence int64
ReleaseSequence int64
ReleaseCreatedAt string
ReleaseNotes string
VersionLabel string
ReplicatedAppEndpoint string
StatusInformers []appstatetypes.StatusInformerString
ReplicatedID string
AppID string
Namespace string
Context context.Context
LicenseBytes []byte
IntegrationLicenseID string
LicenseFields sdklicensetypes.LicenseFields
AppName string
ChannelID string
ChannelName string
ChannelSequence int64
ReleaseSequence int64
ReleaseCreatedAt string
ReleaseNotes string
VersionLabel string
ReplicatedAppEndpoint string
StatusInformers []appstatetypes.StatusInformerString
ReplicatedID string
AppID string
AdditionalMetricsEndpoint string
Namespace string
}

func Start(params APIServerParams) {
Expand Down
29 changes: 15 additions & 14 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@ import (
)

type ReplicatedConfig struct {
License string `yaml:"license"`
LicenseFields sdklicensetypes.LicenseFields `yaml:"licenseFields"`
AppName string `yaml:"appName"`
ChannelID string `yaml:"channelID"`
ChannelName string `yaml:"channelName"`
ChannelSequence int64 `yaml:"channelSequence"`
ReleaseSequence int64 `yaml:"releaseSequence"`
ReleaseCreatedAt string `yaml:"releaseCreatedAt"`
ReleaseNotes string `yaml:"releaseNotes"`
VersionLabel string `yaml:"versionLabel"`
ReplicatedAppEndpoint string `yaml:"replicatedAppEndpoint"`
StatusInformers []appstatetypes.StatusInformerString `yaml:"statusInformers"`
ReplicatedID string `yaml:"replicatedID"`
AppID string `yaml:"appID"`
License string `yaml:"license"`
LicenseFields sdklicensetypes.LicenseFields `yaml:"licenseFields"`
AppName string `yaml:"appName"`
ChannelID string `yaml:"channelID"`
ChannelName string `yaml:"channelName"`
ChannelSequence int64 `yaml:"channelSequence"`
ReleaseSequence int64 `yaml:"releaseSequence"`
ReleaseCreatedAt string `yaml:"releaseCreatedAt"`
ReleaseNotes string `yaml:"releaseNotes"`
VersionLabel string `yaml:"versionLabel"`
ReplicatedAppEndpoint string `yaml:"replicatedAppEndpoint"`
StatusInformers []appstatetypes.StatusInformerString `yaml:"statusInformers"`
ReplicatedID string `yaml:"replicatedID"`
AppID string `yaml:"appID"`
AdditionalMetricsEndpoint string `yaml:"additionalMetricsEndpoint"`
}

func ParseReplicatedConfig(config []byte) (*ReplicatedConfig, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/handlers/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func GetAppUpdates(w http.ResponseWriter, r *http.Request) {
ChannelName: store.GetStore().GetChannelName(),
ChannelSequence: store.GetStore().GetChannelSequence(),
}
us, err := upstream.GetUpdates(store.GetStore(), license, currentCursor)
us, err := upstream.GetUpdates(store.GetStore(), clientset, license, currentCursor)
if err != nil {
logger.Error(errors.Wrap(err, "failed to get updates"))
JSONCached(w, http.StatusOK, updates)
Expand Down
10 changes: 9 additions & 1 deletion pkg/handlers/custom_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"reflect"

"github.com/pkg/errors"
"github.com/replicatedhq/replicated-sdk/pkg/k8sutil"
"github.com/replicatedhq/replicated-sdk/pkg/logger"
"github.com/replicatedhq/replicated-sdk/pkg/metrics"
"github.com/replicatedhq/replicated-sdk/pkg/store"
Expand Down Expand Up @@ -39,7 +40,14 @@ func SendCustomApplicationMetrics(w http.ResponseWriter, r *http.Request) {
return
}

err := metrics.SendApplicationMetricsData(store.GetStore(), license, request.Data)
clientset, err := k8sutil.GetClientset()
if err != nil {
logger.Error(errors.Wrap(err, "failed to get clientset"))
w.WriteHeader(http.StatusInternalServerError)
return
}

err = metrics.SendApplicationMetricsData(store.GetStore(), clientset, license, request.Data)
if err != nil {
logger.Error(errors.Wrap(err, "set application data"))
w.WriteHeader(http.StatusBadRequest)
Expand Down
44 changes: 38 additions & 6 deletions pkg/heartbeat/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func SendAppHeartbeat(clientset kubernetes.Interface, sdkStore store.Store) erro
return nil
}

heartbeatInfo := GetHeartbeatInfo(sdkStore)
heartbeatInfo := GetHeartbeatInfo(sdkStore, clientset)

marshalledRS, err := json.Marshal(heartbeatInfo.ResourceStates)
if err != nil {
Expand Down Expand Up @@ -63,7 +63,7 @@ func SendAppHeartbeat(clientset kubernetes.Interface, sdkStore store.Store) erro
return nil
}

func GetHeartbeatInfo(sdkStore store.Store) *types.HeartbeatInfo {
func GetHeartbeatInfo(sdkStore store.Store, clientset kubernetes.Interface) *types.HeartbeatInfo {
r := types.HeartbeatInfo{
ClusterID: sdkStore.GetReplicatedID(),
InstanceID: sdkStore.GetAppID(),
Expand All @@ -74,10 +74,7 @@ func GetHeartbeatInfo(sdkStore store.Store) *types.HeartbeatInfo {
ResourceStates: sdkStore.GetAppStatus().ResourceStates,
}

clientset, err := k8sutil.GetClientset()
if err != nil {
logger.Debugf("failed to get clientset: %v", err.Error())
} else {
if clientset != nil {
k8sVersion, err := k8sutil.GetK8sVersion(clientset)
if err != nil {
logger.Debugf("failed to get k8s version: %v", err.Error())
Expand All @@ -90,5 +87,40 @@ func GetHeartbeatInfo(sdkStore store.Store) *types.HeartbeatInfo {
}
}

if sdkStore.GetAdditionalMetricsEndpoint() != "" {
additionalMetrics, err := getAdditionalMetrics(sdkStore.GetAdditionalMetricsEndpoint(), sdkStore.GetLicense().Spec.LicenseID)
if err != nil {
logger.Errorf("failed to get additional metrics: %v", err.Error())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, only logging an error and continuing to report without the additional metrics can be problematic. let's discuss.

} else {
r.AdditionalMetrics = additionalMetrics
}
}

return &r
}

func getAdditionalMetrics(endpoint string, licenseID string) (types.AdditionalMetrics, error) {
req, err := util.NewRequest("GET", endpoint, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to create http request")
}

req.Header.Set("Authorization", licenseID)

resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, errors.Wrap(err, "failed to get request")
}
defer resp.Body.Close()

if resp.StatusCode != 200 {
return nil, errors.Errorf("Unexpected status code %d", resp.StatusCode)
}

var additionalMetrics types.AdditionalMetrics
if err := json.NewDecoder(resp.Body).Decode(&additionalMetrics); err != nil {
return nil, errors.Wrap(err, "failed to decode response body")
}

return additionalMetrics, nil
}
Loading
Loading