From 41cae06f2775ed0b70fc3362dba5cbaa3d10b971 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marcin=20=C5=81awniczak?= <marcin@lawniczak.me>
Date: Tue, 21 Jan 2025 12:15:16 +0100
Subject: [PATCH] feat: Add new Thousandeyes test type - API transaction time
 (#629)

## Motivation

New test type in Thousandeyes

## Summary

Added test type name

## Related changes

https://github.com/nobl9/nobl9-go/pull/624/files

## Testing

No testing needed.

## Release Notes

Added API Test type for Thousandeyes integration.
---
 internal/manifest/v1alpha/examples/slo.go     |   1 +
 .../manifest/v1alpha/examples/slo_variants.go |   6 +
 .../v1alpha/slo/examples/thousand-eyes.yaml   | 260 ++++++++++++++++++
 manifest/v1alpha/slo/metrics_thousand_eyes.go |   2 +
 4 files changed, 269 insertions(+)

diff --git a/internal/manifest/v1alpha/examples/slo.go b/internal/manifest/v1alpha/examples/slo.go
index 26c9e839..21d4da64 100644
--- a/internal/manifest/v1alpha/examples/slo.go
+++ b/internal/manifest/v1alpha/examples/slo.go
@@ -52,6 +52,7 @@ var customMetricExamples = map[v1alpha.DataSourceType]map[metricVariant][]metric
 			metricSubVariantThousandEyesNetLoss,
 			metricSubVariantThousandEyesDOMLoad,
 			metricSubVariantThousandEyesTransactionTime,
+			metricSubVariantThousandEyesAPITransactionTime,
 			metricSubVariantThousandEyesServerAvailability,
 			metricSubVariantThousandEyesServerThroughput,
 		},
diff --git a/internal/manifest/v1alpha/examples/slo_variants.go b/internal/manifest/v1alpha/examples/slo_variants.go
index 00841dbe..09e3080c 100644
--- a/internal/manifest/v1alpha/examples/slo_variants.go
+++ b/internal/manifest/v1alpha/examples/slo_variants.go
@@ -220,6 +220,7 @@ const (
 	metricSubVariantThousandEyesNetLoss            metricVariant = "net loss"
 	metricSubVariantThousandEyesDOMLoad            metricVariant = "DOM load"
 	metricSubVariantThousandEyesTransactionTime    metricVariant = "transaction time"
+	metricSubVariantThousandEyesAPITransactionTime metricVariant = "API transaction time"
 	metricSubVariantThousandEyesServerAvailability metricVariant = "server availability"
 	metricSubVariantThousandEyesServerThroughput   metricVariant = "server throughput"
 	// CloudWatch.
@@ -440,6 +441,11 @@ func (s sloExample) generateMetricVariant(slo v1alphaSLO.SLO) v1alphaSLO.SLO {
 				TestID:   ptr[int64](2280492),
 				TestType: ptr(v1alphaSLO.ThousandEyesWebTransactionTime),
 			}))
+		case metricVariantThreshold + metricSubVariantThousandEyesAPITransactionTime:
+			return setThresholdMetric(slo, newMetricSpec(v1alphaSLO.ThousandEyesMetric{
+				TestID:   ptr[int64](2280492),
+				TestType: ptr(v1alphaSLO.ThousandEyesAPITransactionTime),
+			}))
 		case metricVariantThreshold + metricSubVariantThousandEyesServerAvailability:
 			return setThresholdMetric(slo, newMetricSpec(v1alphaSLO.ThousandEyesMetric{
 				TestID:   ptr[int64](2280492),
diff --git a/manifest/v1alpha/slo/examples/thousand-eyes.yaml b/manifest/v1alpha/slo/examples/thousand-eyes.yaml
index bbf0c5fe..0d4ca493 100644
--- a/manifest/v1alpha/slo/examples/thousand-eyes.yaml
+++ b/manifest/v1alpha/slo/examples/thousand-eyes.yaml
@@ -1,4 +1,264 @@
 # Metric type: threshold
+# Metric variant: API transaction time
+# Budgeting method: Occurrences
+# Time window type: Calendar
+- apiVersion: n9/v1alpha
+  kind: SLO
+  metadata:
+    name: api-server-slo
+    displayName: API Server SLO
+    project: default
+    labels:
+      area:
+      - latency
+      - slow-check
+      env:
+      - prod
+      - dev
+      region:
+      - us
+      - eu
+      team:
+      - green
+      - sales
+    annotations:
+      area: latency
+      env: prod
+      region: us
+      team: sales
+  spec:
+    description: Example ThousandEyes SLO
+    indicator:
+      metricSource:
+        name: thousand-eyes
+        project: default
+        kind: Agent
+    budgetingMethod: Occurrences
+    objectives:
+    - displayName: Good response (200)
+      value: 200.0
+      name: ok
+      target: 0.95
+      rawMetric:
+        query:
+          thousandEyes:
+            testID: 2280492
+            testType: api-transaction-time
+      op: lte
+      primary: true
+    service: api-server
+    timeWindows:
+    - unit: Month
+      count: 1
+      isRolling: false
+      calendar:
+        startTime: "2022-12-01 00:00:00"
+        timeZone: UTC
+    alertPolicies:
+    - fast-burn-5x-for-last-10m
+    attachments:
+    - url: https://docs.nobl9.com
+      displayName: Nobl9 Documentation
+    anomalyConfig:
+      noData:
+        alertMethods:
+        - name: slack-notification
+          project: default
+# Metric type: threshold
+# Metric variant: API transaction time
+# Budgeting method: Occurrences
+# Time window type: Rolling
+- apiVersion: n9/v1alpha
+  kind: SLO
+  metadata:
+    name: api-server-slo
+    displayName: API Server SLO
+    project: default
+    labels:
+      area:
+      - latency
+      - slow-check
+      env:
+      - prod
+      - dev
+      region:
+      - us
+      - eu
+      team:
+      - green
+      - sales
+    annotations:
+      area: latency
+      env: prod
+      region: us
+      team: sales
+  spec:
+    description: Example ThousandEyes SLO
+    indicator:
+      metricSource:
+        name: thousand-eyes
+        project: default
+        kind: Agent
+    budgetingMethod: Occurrences
+    objectives:
+    - displayName: Good response (200)
+      value: 200.0
+      name: ok
+      target: 0.95
+      rawMetric:
+        query:
+          thousandEyes:
+            testID: 2280492
+            testType: api-transaction-time
+      op: lte
+      primary: true
+    service: api-server
+    timeWindows:
+    - unit: Hour
+      count: 1
+      isRolling: true
+    alertPolicies:
+    - fast-burn-5x-for-last-10m
+    attachments:
+    - url: https://docs.nobl9.com
+      displayName: Nobl9 Documentation
+    anomalyConfig:
+      noData:
+        alertMethods:
+        - name: slack-notification
+          project: default
+# Metric type: threshold
+# Metric variant: API transaction time
+# Budgeting method: Timeslices
+# Time window type: Calendar
+- apiVersion: n9/v1alpha
+  kind: SLO
+  metadata:
+    name: api-server-slo
+    displayName: API Server SLO
+    project: default
+    labels:
+      area:
+      - latency
+      - slow-check
+      env:
+      - prod
+      - dev
+      region:
+      - us
+      - eu
+      team:
+      - green
+      - sales
+    annotations:
+      area: latency
+      env: prod
+      region: us
+      team: sales
+  spec:
+    description: Example ThousandEyes SLO
+    indicator:
+      metricSource:
+        name: thousand-eyes
+        project: default
+        kind: Agent
+    budgetingMethod: Timeslices
+    objectives:
+    - displayName: Good response (200)
+      value: 200.0
+      name: ok
+      target: 0.95
+      timeSliceTarget: 0.9
+      rawMetric:
+        query:
+          thousandEyes:
+            testID: 2280492
+            testType: api-transaction-time
+      op: lte
+      primary: true
+    service: api-server
+    timeWindows:
+    - unit: Month
+      count: 1
+      isRolling: false
+      calendar:
+        startTime: "2022-12-01 00:00:00"
+        timeZone: UTC
+    alertPolicies:
+    - fast-burn-5x-for-last-10m
+    attachments:
+    - url: https://docs.nobl9.com
+      displayName: Nobl9 Documentation
+    anomalyConfig:
+      noData:
+        alertMethods:
+        - name: slack-notification
+          project: default
+# Metric type: threshold
+# Metric variant: API transaction time
+# Budgeting method: Timeslices
+# Time window type: Rolling
+- apiVersion: n9/v1alpha
+  kind: SLO
+  metadata:
+    name: api-server-slo
+    displayName: API Server SLO
+    project: default
+    labels:
+      area:
+      - latency
+      - slow-check
+      env:
+      - prod
+      - dev
+      region:
+      - us
+      - eu
+      team:
+      - green
+      - sales
+    annotations:
+      area: latency
+      env: prod
+      region: us
+      team: sales
+  spec:
+    description: Example ThousandEyes SLO
+    indicator:
+      metricSource:
+        name: thousand-eyes
+        project: default
+        kind: Agent
+    budgetingMethod: Timeslices
+    objectives:
+    - displayName: Good response (200)
+      value: 200.0
+      name: ok
+      target: 0.95
+      timeSliceTarget: 0.9
+      rawMetric:
+        query:
+          thousandEyes:
+            testID: 2280492
+            testType: api-transaction-time
+      op: lte
+      primary: true
+    service: api-server
+    timeWindows:
+    - unit: Hour
+      count: 1
+      isRolling: true
+    alertPolicies:
+    - fast-burn-5x-for-last-10m
+    attachments:
+    - url: https://docs.nobl9.com
+      displayName: Nobl9 Documentation
+    anomalyConfig:
+      noData:
+        alertMethods:
+        - name: slack-notification
+          project: default
+# Metric type: threshold
 # Metric variant: DOM load
 # Budgeting method: Occurrences
 # Time window type: Calendar
diff --git a/manifest/v1alpha/slo/metrics_thousand_eyes.go b/manifest/v1alpha/slo/metrics_thousand_eyes.go
index ee433191..356bbe51 100644
--- a/manifest/v1alpha/slo/metrics_thousand_eyes.go
+++ b/manifest/v1alpha/slo/metrics_thousand_eyes.go
@@ -17,6 +17,7 @@ const (
 	ThousandEyesWebPageLoad             = "web-page-load"
 	ThousandEyesWebDOMLoad              = "web-dom-load"
 	ThousandEyesWebTransactionTime      = "web-transaction-time"
+	ThousandEyesAPITransactionTime      = "api-transaction-time"
 	ThousandEyesHTTPResponseTime        = "http-response-time"
 	ThousandEyesServerAvailability      = "http-server-availability"
 	ThousandEyesServerThroughput        = "http-server-throughput"
@@ -43,6 +44,7 @@ var supportedThousandEyesTestTypes = []string{
 	ThousandEyesWebPageLoad,
 	ThousandEyesWebDOMLoad,
 	ThousandEyesWebTransactionTime,
+	ThousandEyesAPITransactionTime,
 	ThousandEyesHTTPResponseTime,
 	ThousandEyesServerAvailability,
 	ThousandEyesServerThroughput,