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

feat: adding Elastic as a metric provider #3890

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions .github/actions/spelling/expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ dupl
dynamicinformer
dynatrace
ecr
elastic
elasticsearch
elif
ENABLEGITINFO
endblock
Expand Down Expand Up @@ -392,6 +394,7 @@ loadtests
LOCALBIN
logf
logr
lte
makefiles
markdownfiles
markdownlint
Expand Down
4 changes: 2 additions & 2 deletions metrics-operator/api/v1/keptnmetricsprovider_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import (
// KeptnMetricsProviderSpec defines the desired state of KeptnMetricsProvider
type KeptnMetricsProviderSpec struct {
// +kubebuilder:validation:Optional
// +kubebuilder:validation:Pattern:=cortex|datadog|dql|dynatrace|prometheus|thanos
// Type represents the provider type. This can be one of cortex, datadog, dql, dynatrace, prometheus or thanos.
// +kubebuilder:validation:Pattern:=cortex|datadog|dql|dynatrace|prometheus|thanos|elastic
// Type represents the provider type. This can be one of cortex, datadog, dql, dynatrace, prometheus, elastic or thanos.
Type string `json:"type"`
// TargetServer defines URL (including port and protocol) at which the metrics provider is reachable.
TargetServer string `json:"targetServer"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ spec:
type:
description: Type represents the provider type. This can be one of
cortex, datadog, dql, dynatrace, prometheus or thanos.
pattern: cortex|datadog|dql|dynatrace|prometheus|thanos
pattern: cortex|datadog|dql|dynatrace|prometheus|thanos|elastic
type: string
required:
- targetServer
Expand Down Expand Up @@ -198,7 +198,7 @@ spec:
type:
description: Type represents the provider type. This can be one of
prometheus, dynatrace, datadog, dql.
pattern: prometheus|dynatrace|datadog|dql
pattern: prometheus|dynatrace|datadog|dql|elastic
type: string
required:
- targetServer
Expand Down Expand Up @@ -264,7 +264,7 @@ spec:
type:
description: Type represents the provider type. This can be one of
prometheus, dynatrace, datadog, dql.
pattern: prometheus|dynatrace|datadog|dql
pattern: prometheus|dynatrace|datadog|dql|elastic
type: string
required:
- targetServer
Expand Down
2 changes: 2 additions & 0 deletions metrics-operator/controllers/common/providers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const PrometheusProviderType = "prometheus"
const ThanosProviderType = "thanos"
const CortexProviderType = "cortex"
const DataDogProviderType = "datadog"
const ElasticProviderType = "elastic"

var SupportedProviders = []string{
DynatraceProviderType,
Expand All @@ -14,4 +15,5 @@ var SupportedProviders = []string{
DataDogProviderType,
CortexProviderType,
ThanosProviderType,
ElasticProviderType,
}
157 changes: 157 additions & 0 deletions metrics-operator/controllers/common/providers/elastic/elastic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package elastic

import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"net/http"
"strings"
"time"

elastic "github.com/elastic/go-elasticsearch/v8"
"github.com/go-logr/logr"
metricsapi "github.com/keptn/lifecycle-toolkit/metrics-operator/api/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

const (
warningLogStringElastic = "%s API returned warnings: %s"
defaultTimeRange = 30 * time.Minute
)

type KeptnElasticProvider struct {
Log logr.Logger
K8sClient client.Client
Elastic *elastic.Client
}

type ElasticsearchResponse struct {
Hits struct {
Total struct {
Value int `json:"value"`
} `json:"total"`
} `json:"hits"`
}

func GetElasticClient(provider metricsapi.KeptnMetricsProvider) (*elastic.Client, error) {
es, err := elastic.NewClient(elastic.Config{
Addresses: []string{provider.Spec.TargetServer},
APIKey: provider.Spec.SecretKeyRef.Key,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: provider.Spec.InsecureSkipTlsVerify,
},
},
})
if err != nil {
return nil, fmt.Errorf("failed to create Elasticsearch client: %w", err)
}
return es, nil
}

func (r *KeptnElasticProvider) FetchAnalysisValue(ctx context.Context, query string, analysis metricsapi.Analysis, provider *metricsapi.KeptnMetricsProvider) (string, error) {
ctx, cancel := context.WithTimeout(ctx, 20*time.Second)
defer cancel()

result, err := r.runElasticQuery(ctx, query, analysis.GetFrom(), analysis.GetTo())
if err != nil {
return "", err
}

r.Log.Info(fmt.Sprintf("Elasticsearch query result: %v", result))
return r.extractMetric(result)
}

func (r *KeptnElasticProvider) EvaluateQuery(ctx context.Context, metric metricsapi.KeptnMetric, provider metricsapi.KeptnMetricsProvider) (string, []byte, error) {
ctx, cancel := context.WithTimeout(ctx, 20*time.Second)
defer cancel()

timeRange := getTimeRangeFromSpec(metric.Spec.Range)

result, err := r.runElasticQuery(ctx, metric.Spec.Query, time.Now().Add(-timeRange), time.Now())
if err != nil {
return "", nil, err
}

metricValue, err := r.extractMetric(result)
if err != nil {
return "", nil, err
}

return metricValue, []byte{}, nil
}

func (r *KeptnElasticProvider) EvaluateQueryForStep(ctx context.Context, metric metricsapi.KeptnMetric, provider metricsapi.KeptnMetricsProvider) ([]string, []byte, error) {
r.Log.Info("EvaluateQueryForStep called but not implemented")
return nil, nil, nil
}

func getTimeRangeFromSpec(rangeSpec *metricsapi.RangeSpec) time.Duration {
if rangeSpec == nil || rangeSpec.Interval == "" {
return defaultTimeRange
}

duration, err := time.ParseDuration(rangeSpec.Interval)
if err != nil {
return defaultTimeRange
}

return duration
}

func (r *KeptnElasticProvider) runElasticQuery(ctx context.Context, query string, from, to time.Time) (map[string]interface{}, error) {
queryBody := fmt.Sprintf(`
{
"query": {
"bool": {
"must": [
%s,
{
"range": {
"@timestamp": {
"gte": "%s",
"lte": "%s"
}
}
}
]
}
}
}`, query, from.Format(time.RFC3339), to.Format(time.RFC3339))

res, err := r.Elastic.Search(
r.Elastic.Search.WithContext(ctx),
r.Elastic.Search.WithBody(strings.NewReader(queryBody)),
)
if err != nil {
return nil, fmt.Errorf("failed to execute Elasticsearch query: %w", err)
}
defer res.Body.Close()

if warnings, ok := res.Header["Warning"]; ok {
r.Log.Info(fmt.Sprintf(warningLogStringElastic, "Elasticsearch", warnings))
}

var result map[string]interface{}
if err := json.NewDecoder(res.Body).Decode(&result); err != nil {
return nil, fmt.Errorf("failed to parse Elasticsearch response: %w", err)
}

return result, nil
}

func (r *KeptnElasticProvider) extractMetric(result map[string]interface{}) (string, error) {
var response ElasticsearchResponse
jsonData, err := json.Marshal(result)
if err != nil {
return "", fmt.Errorf("failed to marshal result: %w", err)
}

if err := json.Unmarshal(jsonData, &response); err != nil {
return "", fmt.Errorf("failed to unmarshal result into struct: %w", err)
}

value := fmt.Sprintf("%d", response.Hits.Total.Value)
return value, nil
}
11 changes: 11 additions & 0 deletions metrics-operator/controllers/common/providers/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
metricsapi "github.com/keptn/lifecycle-toolkit/metrics-operator/api/v1"
"github.com/keptn/lifecycle-toolkit/metrics-operator/controllers/common/providers/datadog"
"github.com/keptn/lifecycle-toolkit/metrics-operator/controllers/common/providers/dynatrace"
"github.com/keptn/lifecycle-toolkit/metrics-operator/controllers/common/providers/elastic"
"github.com/keptn/lifecycle-toolkit/metrics-operator/controllers/common/providers/prometheus"
"sigs.k8s.io/controller-runtime/pkg/client"
)
Expand Down Expand Up @@ -62,6 +63,16 @@ func NewProvider(provider *metricsapi.KeptnMetricsProvider, log logr.Logger, k8s
},
K8sClient: k8sClient,
}, nil
case ElasticProviderType:
es, err := elastic.GetElasticClient(*provider)
if err != nil {
return nil, err
}
return &elastic.KeptnElasticProvider{
Log: log,
K8sClient: k8sClient,
Elastic: es,
}, nil
default:
return nil, fmt.Errorf("provider %s not supported", provider.Spec.Type)
}
Expand Down
2 changes: 2 additions & 0 deletions metrics-operator/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.23
require (
github.com/DataDog/datadog-api-client-go/v2 v2.32.0
github.com/benbjohnson/clock v1.3.5
github.com/elastic/go-elasticsearch/v8 v8.17.1
github.com/go-logr/logr v1.4.2
github.com/gorilla/mux v1.8.1
github.com/kelseyhightower/envconfig v1.4.0
Expand Down Expand Up @@ -43,6 +44,7 @@ require (
github.com/coreos/go-semver v0.3.1 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/elastic/elastic-transport-go/v8 v8.6.1 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch v5.7.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions metrics-operator/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/elastic/elastic-transport-go/v8 v8.6.1 h1:h2jQRqH6eLGiBSN4eZbQnJLtL4bC5b4lfVFRjw2R4e4=
github.com/elastic/elastic-transport-go/v8 v8.6.1/go.mod h1:YLHer5cj0csTzNFXoNQ8qhtGY1GTvSqPnKWKaqQE3Hk=
github.com/elastic/go-elasticsearch/v8 v8.17.1 h1:bOXChDoCMB4TIwwGqKd031U8OXssmWLT3UrAr9EGs3Q=
github.com/elastic/go-elasticsearch/v8 v8.17.1/go.mod h1:MVJCtL+gJJ7x5jFeUmA20O7rvipX8GcQmo5iBcmaJn4=
github.com/emicklei/go-restful/v3 v3.11.0 h1:rAQeMHw1c7zTmncogyy8VvRZwtkmkZ4FxERmMY4rD+g=
github.com/emicklei/go-restful/v3 v3.11.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
github.com/evanphx/json-patch v5.7.0+incompatible h1:vgGkfT/9f8zE6tvSCe74nfpAVDQ2tG6yudJd8LBksgI=
Expand Down
Loading