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

docs: document how to write create new keptnmetricsprovider #2939

Merged
Show file tree
Hide file tree
Changes from 47 commits
Commits
Show all changes
199 commits
Select commit Hold shift + click to select a range
c9a57fb
keep
geoffrey1330 Aug 7, 2023
007130c
keep
geoffrey1330 Sep 22, 2023
a38b3c5
Added dummy provider in keptn metric provider
geoffrey1330 Feb 1, 2024
decf9ce
Added dummy provider in keptn metric provider
geoffrey1330 Feb 1, 2024
73ed522
Added dummy provider in keptn metric provider
geoffrey1330 Feb 1, 2024
1288846
Added dummy provider in keptn metric provider
geoffrey1330 Feb 1, 2024
59d59eb
Added dummy provider in keptn metric provider
geoffrey1330 Feb 1, 2024
e34c356
Added dummy provider in keptn metric provider
geoffrey1330 Feb 1, 2024
b941123
Added dummy provider in keptn metric provider
geoffrey1330 Feb 1, 2024
f694a51
Added dummy provider in keptn metric provider
geoffrey1330 Feb 1, 2024
f0c713a
Added dummy provider in keptn metric provider
geoffrey1330 Feb 1, 2024
89610f4
Added dummy provider in keptn metric provider
geoffrey1330 Feb 1, 2024
3da39bd
Added dummy provider in keptn metric provider
geoffrey1330 Feb 1, 2024
82ff85c
Added dummy provider in keptn metric provider
geoffrey1330 Feb 1, 2024
20dcfe6
Added dummy provider in keptn metric provider
geoffrey1330 Feb 1, 2024
2458e88
Added dummy provider in keptn metric provider
geoffrey1330 Feb 1, 2024
b6cbf99
Added dummy provider in keptn metric provider
geoffrey1330 Feb 1, 2024
d0670bb
Added dummy provider in keptn metric provider
geoffrey1330 Feb 1, 2024
fd23cbc
Added dummy provider in keptn metric provider
geoffrey1330 Feb 1, 2024
491b361
Update docs/docs/contribute/software/add-new-metric-provider.md
geoffrey1330 Feb 2, 2024
90660a8
Update docs/docs/contribute/software/add-new-metric-provider.md
geoffrey1330 Feb 2, 2024
0d643bb
Added dummy provider in keptn metric provider
geoffrey1330 Feb 2, 2024
c3fb722
Added dummy provider in keptn metric provider
geoffrey1330 Feb 2, 2024
65e3a52
Added dummy provider in keptn metric provider
geoffrey1330 Feb 2, 2024
7cfe368
Added dummy provider in keptn metric provider
geoffrey1330 Feb 2, 2024
5a4f822
Added dummy provider in keptn metric provider
geoffrey1330 Feb 2, 2024
f62c25f
Added dummy provider in keptn metric provider
geoffrey1330 Feb 2, 2024
95f9f4e
Added dummy provider in keptn metric provider
geoffrey1330 Feb 2, 2024
a77be7a
Added dummy provider in keptn metric provider
geoffrey1330 Feb 2, 2024
0f4947d
Added dummy provider in keptn metric provider
geoffrey1330 Feb 2, 2024
577ca9a
Added dummy provider in keptn metric provider
geoffrey1330 Feb 2, 2024
1592466
Added dummy provider in keptn metric provider
geoffrey1330 Feb 2, 2024
5f95c95
Added dummy provider in keptn metric provider
geoffrey1330 Feb 2, 2024
447eee1
Added dummy provider in keptn metric provider
geoffrey1330 Feb 2, 2024
8199672
Added dummy provider in keptn metric provider
geoffrey1330 Feb 2, 2024
ae81ae7
Added dummy provider in keptn metric provider
geoffrey1330 Feb 2, 2024
6774213
Added dummy provider in keptn metric provider
geoffrey1330 Feb 2, 2024
ae6006b
Added dummy provider in keptn metric provider
geoffrey1330 Feb 2, 2024
ac51465
Added dummy provider in keptn metric provider
geoffrey1330 Feb 2, 2024
0cf5be0
Added dummy provider in keptn metric provider
geoffrey1330 Feb 2, 2024
76bac32
Added dummy provider in keptn metric provider
geoffrey1330 Feb 2, 2024
f9dd6f4
Added dummy provider in keptn metric provider
geoffrey1330 Feb 2, 2024
d98ec97
Added dummy provider in keptn metric provider
geoffrey1330 Feb 2, 2024
462af45
Added dummy provider in keptn metric provider
geoffrey1330 Feb 2, 2024
0575eb3
Added dummy provider in keptn metric provider
geoffrey1330 Feb 5, 2024
da33022
Added dummy provider in keptn metric provider
geoffrey1330 Feb 5, 2024
0dfa7ad
Added dummy provider in keptn metric provider
geoffrey1330 Feb 5, 2024
d807824
Added dummy provider in keptn metric provider
geoffrey1330 Feb 5, 2024
90a0361
Added dummy provider in keptn metric provider
geoffrey1330 Feb 5, 2024
bff4667
Added dummy provider in keptn metric provider
geoffrey1330 Feb 5, 2024
1cc2401
Added dummy provider in keptn metric provider
geoffrey1330 Feb 5, 2024
887e90e
Added dummy provider in keptn metric provider
geoffrey1330 Feb 5, 2024
312a403
Added dummy provider in keptn metric provider
geoffrey1330 Feb 5, 2024
a19d3a9
Added dummy provider in keptn metric provider
geoffrey1330 Feb 5, 2024
9366b44
Added dummy provider in keptn metric provider
geoffrey1330 Feb 5, 2024
830fac1
Added dummy provider in keptn metric provider
geoffrey1330 Feb 5, 2024
325f4a7
Added dummy provider in keptn metric provider
geoffrey1330 Feb 5, 2024
9f59459
Added dummy provider in keptn metric provider
geoffrey1330 Feb 5, 2024
d951427
Added dummy provider in keptn metric provider
geoffrey1330 Feb 5, 2024
972581c
Added dummy provider in keptn metric provider
geoffrey1330 Feb 5, 2024
af27322
Added dummy provider in keptn metric provider
geoffrey1330 Feb 5, 2024
5579f8d
Added dummy provider in keptn metric provider
geoffrey1330 Feb 5, 2024
26d43eb
Added dummy provider in keptn metric provider
geoffrey1330 Feb 5, 2024
fb72358
Added dummy provider in keptn metric provider
geoffrey1330 Feb 5, 2024
206c166
Added dummy provider in keptn metric provider
geoffrey1330 Feb 5, 2024
4816a0f
Added dummy provider in keptn metric provider
geoffrey1330 Feb 5, 2024
9444a0e
Added dummy provider in keptn metric provider
geoffrey1330 Feb 5, 2024
3ac838b
Added dummy provider in keptn metric provider
geoffrey1330 Feb 5, 2024
14b4bb1
Added dummy provider in keptn metric provider
geoffrey1330 Feb 5, 2024
425e43f
Added dummy provider in keptn metric provider
geoffrey1330 Feb 5, 2024
8b31763
Added dummy provider in keptn metric provider
geoffrey1330 Feb 5, 2024
c872041
Added dummy provider in keptn metric provider
geoffrey1330 Feb 5, 2024
8a53e1a
Added dummy provider in keptn metric provider
geoffrey1330 Feb 5, 2024
dcd8f51
Added dummy provider in keptn metric provider
geoffrey1330 Feb 6, 2024
1b5f991
Added dummy provider in keptn metric provider
geoffrey1330 Feb 6, 2024
05856bf
Added dummy provider in keptn metric provider
geoffrey1330 Feb 6, 2024
63ed5ce
Added dummy provider in keptn metric provider
geoffrey1330 Feb 6, 2024
7ef117e
Added dummy provider in keptn metric provider
geoffrey1330 Feb 6, 2024
410865c
Added dummy provider in keptn metric provider
geoffrey1330 Feb 6, 2024
ba60139
Added dummy provider in keptn metric provider
geoffrey1330 Feb 6, 2024
e03e231
Added dummy provider in keptn metric provider
geoffrey1330 Feb 6, 2024
8499c72
Update docs/docs/contribute/software/add-new-metric-provider.md
geoffrey1330 Feb 6, 2024
b569b5b
Update docs/docs/contribute/software/add-new-metric-provider.md
geoffrey1330 Feb 6, 2024
eb0027a
Added dummy provider in keptn metric provider
geoffrey1330 Feb 6, 2024
ee23f5d
Added dummy provider in keptn metric provider
geoffrey1330 Feb 6, 2024
0710eff
Added dummy provider in keptn metric provider
geoffrey1330 Feb 6, 2024
9492512
reverted back the change in the helm test
geoffrey1330 Feb 6, 2024
6554d1e
Merge branch 'main' into doc/1248/document_how_to_write_create_new_ke…
geoffrey1330 Feb 9, 2024
eecdb10
Update docs/docs/contribute/software/add-new-metric-provider.md
geoffrey1330 Feb 9, 2024
c0fb934
reverted back the change in the helm test
geoffrey1330 Feb 9, 2024
660b54a
reverted back the change in the helm test
geoffrey1330 Feb 9, 2024
9d90c12
reverted back the change in the helm test
geoffrey1330 Feb 9, 2024
0ec1782
reverted back the change in the helm test
geoffrey1330 Feb 9, 2024
04f2237
reverted back the change in the helm test
geoffrey1330 Feb 9, 2024
0b6e851
reverted back the change in the helm test
geoffrey1330 Feb 9, 2024
a91fa0c
reverted back the change in the helm test
geoffrey1330 Feb 9, 2024
e0e4391
reverted back the change in the helm test
geoffrey1330 Feb 9, 2024
7869737
reverted back the change in the helm test
geoffrey1330 Feb 9, 2024
31da095
reverted back the change in the helm test
geoffrey1330 Feb 9, 2024
6f6d623
reverted back the change in the helm test
geoffrey1330 Feb 9, 2024
7a2b149
reverted back the change in the helm test
geoffrey1330 Feb 9, 2024
9d45b27
reverted back the change in the helm test
geoffrey1330 Feb 9, 2024
7ae0ce1
reverted back the change in the helm test
geoffrey1330 Feb 12, 2024
ccbdefb
Merge branch 'main' into doc/1248/document_how_to_write_create_new_ke…
geoffrey1330 Feb 12, 2024
ea06952
reverted back the change in the helm test
geoffrey1330 Feb 12, 2024
84ef1a7
reverted back the change in the helm test
geoffrey1330 Feb 12, 2024
a4b6ab4
Merge branch 'main' into doc/1248/document_how_to_write_create_new_ke…
geoffrey1330 Feb 12, 2024
3c27cd0
reverted back the change in the helm test
geoffrey1330 Feb 12, 2024
ed29941
Update docs/docs/contribute/software/add-new-metric-provider.md
geoffrey1330 Feb 12, 2024
0542852
Update docs/docs/contribute/software/add-new-metric-provider.md
geoffrey1330 Feb 12, 2024
1d56d51
Update docs/docs/contribute/software/add-new-metric-provider.md
geoffrey1330 Feb 12, 2024
28870b4
reverted back the change in the helm test
geoffrey1330 Feb 12, 2024
e314794
reverted back the change in the helm test
geoffrey1330 Feb 12, 2024
5243404
Update docs/docs/contribute/software/add-new-metric-provider.md
geoffrey1330 Feb 12, 2024
e48f3b6
Update docs/docs/contribute/software/add-new-metric-provider.md
geoffrey1330 Feb 12, 2024
ce320c0
Update docs/docs/contribute/software/add-new-metric-provider.md
geoffrey1330 Feb 12, 2024
45a0580
documenting the steps for creating a new metric provider
geoffrey1330 Feb 12, 2024
2a76a7c
Update docs/docs/contribute/software/add-new-metric-provider.md
geoffrey1330 Feb 12, 2024
abe4559
Merge branch 'main' into doc/1248/document_how_to_write_create_new_ke…
geoffrey1330 Feb 12, 2024
cf309c9
Update docs/docs/contribute/software/add-new-metric-provider.md
geoffrey1330 Feb 13, 2024
619aeff
Update docs/docs/contribute/software/add-new-metric-provider.md
geoffrey1330 Feb 13, 2024
1c918d9
documenting the steps for creating a new metric provider
geoffrey1330 Feb 13, 2024
664ff73
documenting the steps for creating a new metric provider
geoffrey1330 Feb 13, 2024
bfd540b
documenting the steps for creating a new metric provider
geoffrey1330 Feb 13, 2024
ca834af
Merge branch 'main' into doc/1248/document_how_to_write_create_new_ke…
geoffrey1330 Feb 13, 2024
e117d9d
documenting the steps for creating a new metric provider
geoffrey1330 Feb 13, 2024
da8cfd6
documenting the steps for creating a new metric provider
geoffrey1330 Feb 13, 2024
08212ed
Update docs/docs/contribute/software/add-new-metric-provider.md
geoffrey1330 Feb 13, 2024
721f67b
Update docs/docs/contribute/software/add-new-metric-provider.md
geoffrey1330 Feb 13, 2024
de9c130
Update docs/docs/contribute/software/add-new-metric-provider.md
geoffrey1330 Feb 13, 2024
4d4451c
Update docs/docs/contribute/software/add-new-metric-provider.md
geoffrey1330 Feb 13, 2024
161e8f7
Update docs/docs/contribute/software/add-new-metric-provider.md
geoffrey1330 Feb 13, 2024
d68840a
Merge branch 'main' into doc/1248/document_how_to_write_create_new_ke…
geoffrey1330 Feb 13, 2024
2779358
documenting the steps for creating a new metric provider
geoffrey1330 Feb 13, 2024
79c9ed3
documenting the steps for creating a new metric provider
geoffrey1330 Feb 13, 2024
c08cac4
documenting the steps for creating a new metric provider
geoffrey1330 Feb 13, 2024
75eea9a
documenting the steps for creating a new metric provider
geoffrey1330 Feb 13, 2024
c7f0aaf
documenting the steps for creating a new metric provider
geoffrey1330 Feb 13, 2024
8dd5b7e
documenting the steps for creating a new metric provider
geoffrey1330 Feb 13, 2024
2beec41
documenting the steps for creating a new metric provider
geoffrey1330 Feb 13, 2024
922734b
documenting the steps for creating a new metric provider
geoffrey1330 Feb 13, 2024
a6c468c
documenting the steps for creating a new metric provider
geoffrey1330 Feb 13, 2024
376a2d5
documenting the steps for creating a new metric provider
geoffrey1330 Feb 13, 2024
806ada8
documenting the steps for creating a new metric provider
geoffrey1330 Feb 14, 2024
3b43f5b
documenting the steps for creating a new metric provider
geoffrey1330 Feb 14, 2024
c686f1c
documenting the steps for creating a new metric provider
geoffrey1330 Feb 14, 2024
95e545c
documenting the steps for creating a new metric provider
geoffrey1330 Feb 14, 2024
425475b
documenting the steps for creating a new metric provider
geoffrey1330 Feb 14, 2024
6c6f20f
documenting the steps for creating a new metric provider
geoffrey1330 Feb 14, 2024
1098842
documenting the steps for creating a new metric provider
geoffrey1330 Feb 14, 2024
2a57c2e
documenting the steps for creating a new metric provider
geoffrey1330 Feb 14, 2024
598d123
documenting the steps for creating a new metric provider
geoffrey1330 Feb 14, 2024
6066e6a
documenting the steps for creating a new metric provider
geoffrey1330 Feb 14, 2024
758da0b
documenting the steps for creating a new metric provider
geoffrey1330 Feb 14, 2024
6aae2ea
documenting the steps for creating a new metric provider
geoffrey1330 Feb 14, 2024
1293a40
documenting the steps for creating a new metric provider
geoffrey1330 Feb 14, 2024
7c8469b
documenting the steps for creating a new metric provider
geoffrey1330 Feb 14, 2024
a346994
documenting the steps for creating a new metric provider
geoffrey1330 Feb 14, 2024
25bb139
documenting the steps for creating a new metric provider
geoffrey1330 Feb 14, 2024
96df7ae
documenting the steps for creating a new metric provider
geoffrey1330 Feb 14, 2024
9d78556
documenting the steps for creating a new metric provider
geoffrey1330 Feb 14, 2024
35fae0a
documenting the steps for creating a new metric provider
geoffrey1330 Feb 14, 2024
d6c6654
Update docs/docs/contribute/software/add-new-metric-provider.md
geoffrey1330 Feb 15, 2024
2532d36
Merge branch 'main' into doc/1248/document_how_to_write_create_new_ke…
geoffrey1330 Feb 15, 2024
50991f4
Update mkdocs.yml
geoffrey1330 Feb 15, 2024
2b7f6dc
Update docs/docs/contribute/software/add-new-metric-provider.md
geoffrey1330 Feb 15, 2024
c7dede0
Update docs/docs/contribute/software/add-new-metric-provider.md
geoffrey1330 Feb 15, 2024
17138d8
Update docs/docs/contribute/software/add-new-metric-provider.md
geoffrey1330 Feb 15, 2024
ad337e9
Update docs/docs/contribute/software/add-new-metric-provider.md
geoffrey1330 Feb 15, 2024
b1f19ad
Update docs/docs/contribute/software/add-new-metric-provider.md
geoffrey1330 Feb 15, 2024
17bf9ef
Update docs/docs/contribute/software/add-new-metric-provider.md
geoffrey1330 Feb 15, 2024
ca5a2a0
documenting the steps for creating a new metric provider
geoffrey1330 Feb 15, 2024
6514233
documenting the steps for creating a new metric provider
geoffrey1330 Feb 15, 2024
7e139e1
Update docs/docs/contribute/software/add-new-metric-provider.md
geoffrey1330 Feb 15, 2024
478dd67
documenting the steps for creating a new metric provider
geoffrey1330 Feb 15, 2024
414ff89
documenting the steps for creating a new metric provider
geoffrey1330 Feb 15, 2024
8b1abe8
documenting the steps for creating a new metric provider
geoffrey1330 Feb 15, 2024
552160d
Merge branch 'main' into doc/1248/document_how_to_write_create_new_ke…
geoffrey1330 Feb 15, 2024
f864eda
documenting the steps for creating a new metric provider
geoffrey1330 Feb 15, 2024
3343dc2
documenting the steps for creating a new metric provider
geoffrey1330 Feb 16, 2024
e82c53c
documenting the steps for creating a new metric provider
geoffrey1330 Feb 16, 2024
a79bc1d
documenting the steps for creating a new metric provider
geoffrey1330 Feb 16, 2024
f8bd3eb
documenting the steps for creating a new metric provider
geoffrey1330 Feb 16, 2024
22fe49a
documenting the steps for creating a new metric provider
geoffrey1330 Feb 16, 2024
23a4bd1
documenting the steps for creating a new metric provider
geoffrey1330 Feb 16, 2024
0149b1b
documenting the steps for creating a new metric provider
geoffrey1330 Feb 16, 2024
eabfb92
documenting the steps for creating a new metric provider
geoffrey1330 Feb 16, 2024
f93eec4
documenting the steps for creating a new metric provider
geoffrey1330 Feb 16, 2024
2ae6973
documenting the steps for creating a new metric provider
geoffrey1330 Feb 16, 2024
5502edb
documenting the steps for creating a new metric provider
geoffrey1330 Feb 16, 2024
28c77ed
documenting the steps for creating a new metric provider
geoffrey1330 Feb 20, 2024
f2226cf
documenting the steps for creating a new metric provider
geoffrey1330 Feb 20, 2024
eb28608
documenting the steps for creating a new metric provider
geoffrey1330 Feb 20, 2024
42731af
documenting the steps for creating a new metric provider
geoffrey1330 Feb 20, 2024
79ded21
documenting the steps for creating a new metric provider
geoffrey1330 Feb 20, 2024
37ebefd
documenting the steps for creating a new metric provider
geoffrey1330 Feb 20, 2024
9876c77
Merge branch 'main' into doc/1248/document_how_to_write_create_new_ke…
geoffrey1330 Feb 20, 2024
4ab7758
Update docs/docs/contribute/software/add-new-metric-provider.md
geoffrey1330 Feb 20, 2024
5843060
Update docs/docs/contribute/software/add-new-metric-provider.md
odubajDT Feb 20, 2024
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
146 changes: 146 additions & 0 deletions docs/docs/contribute/software/add-new-metric-provider.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
# Adding a New Metrics Provider for Dummy Endpoint
geoffrey1330 marked this conversation as resolved.
Show resolved Hide resolved

To create a provider for the dummy endpoint, follow these steps:

geoffrey1330 marked this conversation as resolved.
Show resolved Hide resolved
1. Fork the [Keptn repo](https://github.com/keptn/lifecycle-toolkit)
geoffrey1330 marked this conversation as resolved.
Show resolved Hide resolved

2. **Define the Provider Type:** In the `metrics-operator/controllers/common/providers/common.go` file,
define the constant `KeptnDummyProviderType` with the value `"dummy"`.
geoffrey1330 marked this conversation as resolved.
Show resolved Hide resolved

```go
const KeptnDummyProviderType = "dummy"
```

3. **Implement the Provider:** Create your own new folder inside the
geoffrey1330 marked this conversation as resolved.
Show resolved Hide resolved
[metrics-operator/controllers/common/providers](https://github.com/keptn/lifecycle-toolkit/tree/main/metrics-operator/controllers/common/providers)
geoffrey1330 marked this conversation as resolved.
Show resolved Hide resolved
matching the new service name: dummy and a new Go package for the dummy provider.
This package should contain
a struct that implements the `KeptnSLIProvider` interface.
geoffrey1330 marked this conversation as resolved.
Show resolved Hide resolved
You can follow other existing implementations,
such as [prometheus.go](https://github.com/keptn/lifecycle-toolkit/blob/main/metrics-operator/controllers/common/providers/prometheus/prometheus.go),
as an example.
Ensure that you implement the `EvaluateQuery` function to fetch the metrics accurately.
geoffrey1330 marked this conversation as resolved.
Show resolved Hide resolved
In the implementation, make a request to the dummy endpoint and return the response.

```go
geoffrey1330 marked this conversation as resolved.
Show resolved Hide resolved
// Inside the dummy package

type KeptnDummyProvider struct {
Log logr.Logger
HttpClient http.Client
}

func (d *KeptnDummyProvider) FetchAnalysisValue(ctx context.Context, query string, analysis metricsapi.Analysis, provider *metricsapi.KeptnMetricsProvider) (string, error) {
ctx, cancel := context.WithTimeout(ctx, 20*time.Second)
defer cancel()
res, _, err := d.query(ctx, query, *provider, analysis.GetFrom().Unix(), analysis.GetTo().Unix())
return res, err
}

// EvaluateQuery evaluates the query against the random number API endpoint.
func (d *KeptnDummyProvider) EvaluateQuery(ctx context.Context, metric metricsapi.KeptnMetric, provider metricsapi.KeptnMetricsProvider) (string, []byte, error) {
// create a context for cancelling the request if it takes too long.
ctx, cancel := context.WithTimeout(ctx, 20*time.Second)
defer cancel()

fromTime, toTime, err := getTimeRange(metric)
if err != nil {
return "", nil, err
}
return d.query(ctx, metric.Spec.Query, provider, fromTime, toTime)
}

func (d *KeptnDummyProvider) query(ctx context.Context, query string, provider metricsapi.KeptnMetricsProvider, fromTime int64, toTime int64) (string, []byte, error) {
// create a new request with context
//baseURL := "http://www.randomnumberapi.com/api/v1.0/"
qURL := provider.Spec.TargetServer
request, err := http.NewRequestWithContext(ctx, http.MethodGet, qURL+query+"?min=42&max=43", nil)
if err != nil {
d.Log.Error(err, "Error in creating the request")
return "", nil, err
}

// make an http call using the provided client.
response, err := d.HttpClient.Do(request)
if err != nil {
d.Log.Error(err, "Error in making the request")
return "", nil, err
}
defer response.Body.Close()

// parse the response data
responseData, err := io.ReadAll(response.Body)
if err != nil {
d.Log.Error(err, "Error in reading the response")
}
responseStr := d.bytesToString(responseData)

// Return the metric
return responseStr, responseData, nil

}

func (d *KeptnDummyProvider) EvaluateQueryForStep(ctx context.Context, metric metricsapi.KeptnMetric, provider metricsapi.KeptnMetricsProvider) ([]string, []byte, error) {
// create a context for cancelling the request if it takes too long.
ctx, cancel := context.WithTimeout(ctx, 20*time.Second)
defer cancel()
var result []string
fromTime, toTime, err := getTimeRange(metric)
if err != nil {
return result, nil, err
}
resStr, resData, err := d.query(ctx, metric.Spec.Query, provider, fromTime, toTime)

// Append strings to the slice
result = append(result, resStr)
return result, resData, err
}

func (d *KeptnDummyProvider) bytesToString(data []byte) string {
if len(data) == 0 {
return ""
}
return strconv.Itoa(int(data[0]))
}

func getTimeRange(metric metricsapi.KeptnMetric) (int64, int64, error) {
var intervalInMin string
if metric.Spec.Range != nil {
intervalInMin = metric.Spec.Range.Interval
} else {
intervalInMin = "5m"
}
intervalDuration, err := time.ParseDuration(intervalInMin)
if err != nil {
return 0, 0, err
}
return time.Now().Add(-intervalDuration).Unix(), time.Now().Unix(), nil
}
```

4. **Instantiate the Provider:** In the `providers.NewProvider` function
in the `metrics-operator/controllers/common/providers/provider.go` file,
geoffrey1330 marked this conversation as resolved.
Show resolved Hide resolved
add a case for the `KeptnDummyProviderType`.
Instantiate the dummy provider struct and return it.

```go
// Inside the providers package

// NewProvider function
func NewProvider(providerType string, log logr.Logger, k8sClient client.Client) (KeptnSLIProvider, error) {
switch strings.ToLower(providerType) {
case KeptnDummyProviderType:
return &dummy.KeptnDummyProvider{
Log: log,
HttpClient: http.Client{},
}, nil
// Other cases...
}
}
```

5. **Add Test Cases:** Write test cases to validate your implementation and ensure it works correctly.
geoffrey1330 marked this conversation as resolved.
Show resolved Hide resolved
This step is crucial for maintaining code quality and reliability.

6. **Test:** Thoroughly test your implementation to verify that it functions as expected.
Make sure to cover various scenarios and edge cases to ensure robustness.
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ func TestProvidersPool_StartProviders(t *testing.T) {
time.Sleep(time.Millisecond * 100)

// Assert the expected number of workers (goroutines) were started
require.Equal(t, 4, len(pool.providers))
require.Equal(t, 5, len(pool.providers))
require.Equal(t, numJobs, cap(pool.providers["prometheus"]))
// Stop the providers after testing
pool.StopProviders()
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 @@ -4,10 +4,12 @@ const DynatraceProviderType = "dynatrace"
const DynatraceDQLProviderType = "dql"
const PrometheusProviderType = "prometheus"
const DataDogProviderType = "datadog"
const KeptnDummyProviderType = "dummy"

var SupportedProviders = []string{
DynatraceProviderType,
DynatraceDQLProviderType,
PrometheusProviderType,
DataDogProviderType,
KeptnDummyProviderType,
}
104 changes: 104 additions & 0 deletions metrics-operator/controllers/common/providers/dummy/dummy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package dummy

import (
"context"
"io"
"net/http"
"strconv"
"time"

"github.com/go-logr/logr"
metricsapi "github.com/keptn/lifecycle-toolkit/metrics-operator/api/v1beta1"
)

type KeptnDummyProvider struct {
Log logr.Logger
HttpClient http.Client
}

func (d *KeptnDummyProvider) FetchAnalysisValue(ctx context.Context, query string, analysis metricsapi.Analysis, provider *metricsapi.KeptnMetricsProvider) (string, error) {
ctx, cancel := context.WithTimeout(ctx, 20*time.Second)
geoffrey1330 marked this conversation as resolved.
Show resolved Hide resolved
defer cancel()
res, _, err := d.query(ctx, query, *provider, analysis.GetFrom().Unix(), analysis.GetTo().Unix())
return res, err
}

// EvaluateQuery evaluates the query against the random number API endpoint.
func (d *KeptnDummyProvider) EvaluateQuery(ctx context.Context, metric metricsapi.KeptnMetric, provider metricsapi.KeptnMetricsProvider) (string, []byte, error) {
// create a context for cancelling the request if it takes too long.
ctx, cancel := context.WithTimeout(ctx, 20*time.Second)
defer cancel()

fromTime, toTime, err := getTimeRange(metric)
if err != nil {
return "", nil, err
}
return d.query(ctx, metric.Spec.Query, provider, fromTime, toTime)
}

func (d *KeptnDummyProvider) query(ctx context.Context, query string, provider metricsapi.KeptnMetricsProvider, fromTime int64, toTime int64) (string, []byte, error) {
// create a new request with context
//baseURL := "http://www.randomnumberapi.com/api/v1.0/"
qURL := provider.Spec.TargetServer
request, err := http.NewRequestWithContext(ctx, http.MethodGet, qURL+query+"?min=42&max=43", nil)
if err != nil {
d.Log.Error(err, "Error in creating the request")
return "", nil, err
}

// make an http call using the provided client.
response, err := d.HttpClient.Do(request)
if err != nil {
d.Log.Error(err, "Error in making the request")
return "", nil, err
}
defer response.Body.Close()

// parse the response data
responseData, err := io.ReadAll(response.Body)
if err != nil {
d.Log.Error(err, "Error in reading the response")
}
responseStr := d.bytesToString(responseData)

// Return the metric
return responseStr, responseData, nil

}

func (d *KeptnDummyProvider) EvaluateQueryForStep(ctx context.Context, metric metricsapi.KeptnMetric, provider metricsapi.KeptnMetricsProvider) ([]string, []byte, error) {
geoffrey1330 marked this conversation as resolved.
Show resolved Hide resolved
// create a context for cancelling the request if it takes too long.
ctx, cancel := context.WithTimeout(ctx, 20*time.Second)
defer cancel()
var result []string
fromTime, toTime, err := getTimeRange(metric)
if err != nil {
return result, nil, err
}
resStr, resData, err := d.query(ctx, metric.Spec.Query, provider, fromTime, toTime)

// Append strings to the slice
result = append(result, resStr)
return result, resData, err
}

func (d *KeptnDummyProvider) bytesToString(data []byte) string {
if len(data) == 0 {
return ""
}
return strconv.Itoa(int(data[0]))
}

func getTimeRange(metric metricsapi.KeptnMetric) (int64, int64, error) {
var intervalInMin string
if metric.Spec.Range != nil {
intervalInMin = metric.Spec.Range.Interval
} else {
intervalInMin = "5m"
}
intervalDuration, err := time.ParseDuration(intervalInMin)
if err != nil {
return 0, 0, err
}
return time.Now().Add(-intervalDuration).Unix(), time.Now().Unix(), nil
}
Loading
Loading