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

[CLOUDGA-20774] Add support for integration type PROMETHEUS #274

Merged
merged 1 commit into from
Nov 21, 2024
Merged
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
17 changes: 15 additions & 2 deletions cmd/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ func init() {
createIntegrationCmd.Flags().StringToString("sumologic-spec", nil, `Configuration for sumologic.
Please provide key value pairs as follows:
access-key=<your-sumologic-access-key>,access-id=<your-sumologic-access-id>,installation-token=<your-sumologic-installation-token>`)
createIntegrationCmd.Flags().StringToString("prometheus-spec", nil, `Configuration for prometheus.
Please provide key value pairs as follows:
endpoint=<prometheus-otlp-endpoint-url>`)

if util.IsFeatureFlagEnabled(util.GOOGLECLOUD_INTEGRATION) {
createIntegrationCmd.Flags().String("googlecloud-cred-filepath", "", `Filepath for Google Cloud service account credentials.
Expand All @@ -186,7 +189,7 @@ func init() {
}

func setIntegrationConfiguration(cmd *cobra.Command, IntegrationName string, sinkTypeEnum ybmclient.TelemetryProviderTypeEnum) (*ybmclient.TelemetryProviderSpec, error) {
// We initialise this one here, even if we error out later
// We initialize this one here, even if we error out later
IntegrationSpec := ybmclient.NewTelemetryProviderSpec(IntegrationName, sinkTypeEnum)

switch sinkTypeEnum {
Expand All @@ -205,6 +208,17 @@ func setIntegrationConfiguration(cmd *cobra.Command, IntegrationName string, sin
}
datadogSpec := ybmclient.NewDatadogTelemetryProviderSpec(apiKey, site)
IntegrationSpec.SetDatadogSpec(*datadogSpec)
case ybmclient.TELEMETRYPROVIDERTYPEENUM_PROMETHEUS:
if !cmd.Flags().Changed("prometheus-spec") {
return nil, fmt.Errorf("prometheus-spec is required for prometheus sink")
}
prometheusSpecs, _ := cmd.Flags().GetStringToString("prometheus-spec")
endpoint := prometheusSpecs["endpoint"]
if len(endpoint) < 1 {
return nil, fmt.Errorf("endpoint is a required field for prometheus-spec")
}
prometheusSpec := ybmclient.NewPrometheusTelemetryProviderSpec(endpoint)
IntegrationSpec.SetPrometheusSpec(*prometheusSpec)
case ybmclient.TELEMETRYPROVIDERTYPEENUM_GRAFANA:
if !cmd.Flags().Changed("grafana-spec") {
return nil, fmt.Errorf("grafana-spec is required for grafana sink")
Expand Down Expand Up @@ -232,7 +246,6 @@ func setIntegrationConfiguration(cmd *cobra.Command, IntegrationName string, sin
case ybmclient.TELEMETRYPROVIDERTYPEENUM_SUMOLOGIC:
if !cmd.Flags().Changed("sumologic-spec") {
return nil, fmt.Errorf("sumologic-spec is required for sumologic sink")

}
sumoLogicSpecString, _ := cmd.Flags().GetStringToString("sumologic-spec")
accessKey := sumoLogicSpecString["access-key"]
Expand Down
39 changes: 39 additions & 0 deletions cmd/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,45 @@ ID Name Type Site ApiKey
})

})
Context("When type is Prometheus", func() {
It("should create the config", func() {
statusCode = 200
err := loadJson("./test/fixtures/metrics-exporter-prom.json", &responseIntegration)
Expect(err).ToNot(HaveOccurred())
server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest(http.MethodPost, "/api/public/v1/accounts/340af43a-8a7c-4659-9258-4876fd6a207b/projects/78d4459c-0f45-47a5-899a-45ddf43eba6e/telemetry-providers"),
ghttp.RespondWithJSONEncodedPtr(&statusCode, responseIntegration),
),
)
cmd := exec.Command(compiledCLIPath, "integration", "create", "--config-name", "test", "--type", "prometheus", "--prometheus-spec", "endpoint=http://prometheus.yourcompany.com")
session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
session.Wait(2)
Expect(session.Out).Should(gbytes.Say(`The Integration test has been created
ID Name Type Endpoint
9e3fabbc-849c-4a77-bdb2-9422e712e7dc test PROMETHEUS http://prometheus.yourcompany.com/api/v1/otlp`))
session.Kill()
})
It("should return error when arg prometheus-spec not set", func() {
cmd := exec.Command(compiledCLIPath, "integration", "create", "--config-name", "test", "--type", "prometheus")
session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
session.Wait(2)
Expect(session.Err).Should(gbytes.Say("prometheus-spec is required for prometheus sink"))
session.Kill()
})
It("should return error when field endpoint not set", func() {
cmd := exec.Command(compiledCLIPath, "integration", "create", "--config-name", "test", "--type", "prometheus", "--prometheus-spec", "invalid-key=val")
session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
session.Wait(2)
Expect(session.Err).Should(gbytes.Say("(?m:endpoint is a required field for prometheus-spec$)"))
session.Kill()
})

})

Context("When type is Grafana", func() {
It("should create the config", func() {
statusCode = 200
Expand Down
19 changes: 19 additions & 0 deletions cmd/test/fixtures/metrics-exporter-prom.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"data": {
"spec": {
"name": "test",
"type": "PROMETHEUS",
"prometheus_spec": {
"endpoint": "http://prometheus.yourcompany.com/api/v1/otlp"
}
},
"info": {
"id": "9e3fabbc-849c-4a77-bdb2-9422e712e7dc",
"cluster_ids": [],
"metadata": {
"created_on": "2023-08-24T06:41:16.145Z",
"updated_on": "2023-08-24T06:41:16.145Z"
}
}
}
}
9 changes: 9 additions & 0 deletions internal/formatter/telemetry_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

const defaultIntegrationListing = "table {{.ID}}\t{{.Name}}\t{{.Type}}"
const defaultIntegrationDataDog = "table {{.ID}}\t{{.Name}}\t{{.Type}}\t{{.Site}}\t{{.ApiKey}}"
const defaultIntegrationPrometheus = "table {{.ID}}\t{{.Name}}\t{{.Type}}\t{{.Endpoint}}"
const defaultIntegrationGrafana = "table {{.ID}}\t{{.Name}}\t{{.Type}}\t{{.Zone}}\t{{.AccessTokenPolicy}}\t{{.InstanceId}}\t{{.OrgSlug}}"
const defaultIntegrationSumologic = "table {{.ID}}\t{{.Name}}\t{{.Type}}\t{{.AccessKey}}\t{{.AccessID}}\t{{.InstallationToken}}"

Expand All @@ -41,6 +42,8 @@ func NewIntegrationFormat(source string, providerType string) Format {
switch providerType {
case string(ybmclient.TELEMETRYPROVIDERTYPEENUM_DATADOG):
format = defaultIntegrationDataDog
case string(ybmclient.TELEMETRYPROVIDERTYPEENUM_PROMETHEUS):
format = defaultIntegrationPrometheus
case string(ybmclient.TELEMETRYPROVIDERTYPEENUM_GRAFANA):
format = defaultIntegrationGrafana
case string(ybmclient.TELEMETRYPROVIDERTYPEENUM_SUMOLOGIC):
Expand Down Expand Up @@ -70,6 +73,7 @@ func NewIntegrationContext() *IntegrationContext {
"InstallationToken": "InstallationToken",
"AccessID": "Access ID",
"AccessKey": "Access Key",
"Endpoint": "Endpoint",
}
return &IntegrationCtx
}
Expand Down Expand Up @@ -149,3 +153,8 @@ func IntegrationShortenKey(key string, stringLen int) string {
}
return key
}

// Prometheus
func (tp *IntegrationContext) Endpoint() string {
return tp.tp.Spec.GetPrometheusSpec().Endpoint
}