Skip to content

Commit

Permalink
return headers as a map
Browse files Browse the repository at this point in the history
  • Loading branch information
sgalsaleh committed Oct 6, 2023
1 parent 59d7233 commit b244690
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 24 deletions.
8 changes: 7 additions & 1 deletion pkg/handlers/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@ import (
"github.com/replicatedhq/kots/pkg/kotsutil"
"github.com/replicatedhq/kots/pkg/logger"
"github.com/replicatedhq/kots/pkg/replicatedapp"
"github.com/replicatedhq/kots/pkg/reporting"
"github.com/replicatedhq/kots/pkg/session"
"github.com/replicatedhq/kots/pkg/store"
)

func (h *Handler) GetAppMetrics(w http.ResponseWriter, r *http.Request) {
JSON(w, http.StatusOK, "")
app := session.ContextGetApp(r)
reportingInfo := reporting.GetReportingInfo(app.ID)
headers := reporting.GetReportingInfoHeaders(reportingInfo)

JSON(w, http.StatusOK, headers)
}

type SendCustomAppMetricsRequest struct {
Expand Down
58 changes: 35 additions & 23 deletions pkg/reporting/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,57 +10,69 @@ import (
)

func InjectReportingInfoHeaders(req *http.Request, reportingInfo *types.ReportingInfo) {
headers := GetReportingInfoHeaders(reportingInfo)

for key, value := range headers {
req.Header.Set(key, value)
}
}

func GetReportingInfoHeaders(reportingInfo *types.ReportingInfo) map[string]string {
headers := make(map[string]string)

if reportingInfo == nil {
return
return headers
}

req.Header.Set("X-Replicated-K8sVersion", reportingInfo.K8sVersion)
req.Header.Set("X-Replicated-IsKurl", strconv.FormatBool(reportingInfo.IsKurl))
req.Header.Set("X-Replicated-AppStatus", reportingInfo.AppStatus)
req.Header.Set("X-Replicated-ClusterID", reportingInfo.ClusterID)
req.Header.Set("X-Replicated-InstanceID", reportingInfo.InstanceID)
req.Header.Set("X-Replicated-ReplHelmInstalls", strconv.Itoa(reportingInfo.Downstream.ReplHelmInstalls))
req.Header.Set("X-Replicated-NativeHelmInstalls", strconv.Itoa(reportingInfo.Downstream.NativeHelmInstalls))
headers["X-Replicated-K8sVersion"] = reportingInfo.K8sVersion
headers["X-Replicated-IsKurl"] = strconv.FormatBool(reportingInfo.IsKurl)
headers["X-Replicated-AppStatus"] = reportingInfo.AppStatus
headers["X-Replicated-ClusterID"] = reportingInfo.ClusterID
headers["X-Replicated-InstanceID"] = reportingInfo.InstanceID
headers["X-Replicated-ReplHelmInstalls"] = strconv.Itoa(reportingInfo.Downstream.ReplHelmInstalls)
headers["X-Replicated-NativeHelmInstalls"] = strconv.Itoa(reportingInfo.Downstream.NativeHelmInstalls)

if reportingInfo.Downstream.Cursor != "" {
req.Header.Set("X-Replicated-DownstreamChannelSequence", reportingInfo.Downstream.Cursor)
headers["X-Replicated-DownstreamChannelSequence"] = reportingInfo.Downstream.Cursor
}
if reportingInfo.Downstream.ChannelID != "" {
req.Header.Set("X-Replicated-DownstreamChannelID", reportingInfo.Downstream.ChannelID)
headers["X-Replicated-DownstreamChannelID"] = reportingInfo.Downstream.ChannelID
} else if reportingInfo.Downstream.ChannelName != "" {
req.Header.Set("X-Replicated-DownstreamChannelName", reportingInfo.Downstream.ChannelName)
headers["X-Replicated-DownstreamChannelName"] = reportingInfo.Downstream.ChannelName
}

if reportingInfo.Downstream.Status != "" {
req.Header.Set("X-Replicated-InstallStatus", reportingInfo.Downstream.Status)
headers["X-Replicated-InstallStatus"] = reportingInfo.Downstream.Status
}
if reportingInfo.Downstream.PreflightState != "" {
req.Header.Set("X-Replicated-PreflightStatus", reportingInfo.Downstream.PreflightState)
headers["X-Replicated-PreflightStatus"] = reportingInfo.Downstream.PreflightState
}
if reportingInfo.Downstream.Sequence != nil {
req.Header.Set("X-Replicated-DownstreamSequence", strconv.FormatInt(*reportingInfo.Downstream.Sequence, 10))
headers["X-Replicated-DownstreamSequence"] = strconv.FormatInt(*reportingInfo.Downstream.Sequence, 10)
}
if reportingInfo.Downstream.Source != "" {
req.Header.Set("X-Replicated-DownstreamSource", reportingInfo.Downstream.Source)
headers["X-Replicated-DownstreamSource"] = reportingInfo.Downstream.Source
}
req.Header.Set("X-Replicated-SkipPreflights", strconv.FormatBool(reportingInfo.Downstream.SkipPreflights))
headers["X-Replicated-SkipPreflights"] = strconv.FormatBool(reportingInfo.Downstream.SkipPreflights)

if reportingInfo.KOTSInstallID != "" {
req.Header.Set("X-Replicated-KotsInstallID", reportingInfo.KOTSInstallID)
headers["X-Replicated-KotsInstallID"] = reportingInfo.KOTSInstallID
}
if reportingInfo.KURLInstallID != "" {
req.Header.Set("X-Replicated-KurlInstallID", reportingInfo.KURLInstallID)
headers["X-Replicated-KurlInstallID"] = reportingInfo.KURLInstallID
}

req.Header.Set("X-Replicated-KurlNodeCountTotal", strconv.Itoa(reportingInfo.KurlNodeCountTotal))
req.Header.Set("X-Replicated-KurlNodeCountReady", strconv.Itoa(reportingInfo.KurlNodeCountReady))
headers["X-Replicated-KurlNodeCountTotal"] = strconv.Itoa(reportingInfo.KurlNodeCountTotal)
headers["X-Replicated-KurlNodeCountReady"] = strconv.Itoa(reportingInfo.KurlNodeCountReady)

req.Header.Set("X-Replicated-IsGitOpsEnabled", strconv.FormatBool(reportingInfo.IsGitOpsEnabled))
req.Header.Set("X-Replicated-GitOpsProvider", reportingInfo.GitOpsProvider)
headers["X-Replicated-IsGitOpsEnabled"] = strconv.FormatBool(reportingInfo.IsGitOpsEnabled)
headers["X-Replicated-GitOpsProvider"] = reportingInfo.GitOpsProvider

if reportingInfo.K8sDistribution != "" {
req.Header.Set("X-Replicated-K8sDistribution", reportingInfo.K8sDistribution)
headers["X-Replicated-K8sDistribution"] = reportingInfo.K8sDistribution
}

return headers
}

func canReport(endpoint string) bool {
Expand Down

0 comments on commit b244690

Please sign in to comment.