Skip to content

Commit 822a5cf

Browse files
agrawrohhuikang
authored andcommitted
feat(analysis): Added additional metadata to the status of AnalysisRun
Signed-off-by: Rohit Agrawal <[email protected]>
1 parent 53643c5 commit 822a5cf

34 files changed

+223
-19
lines changed

analysis/analysis.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,16 +325,17 @@ func (c *Controller) runMeasurements(run *v1alpha1.AnalysisRun, tasks []metricTa
325325
metricResult := analysisutil.GetResult(run, t.metric.Name)
326326
resultsLock.Unlock()
327327

328+
provider, err := c.newProvider(*logger, t.metric)
328329
if metricResult == nil {
329330
metricResult = &v1alpha1.MetricResult{
330-
Name: t.metric.Name,
331-
Phase: v1alpha1.AnalysisPhaseRunning,
332-
DryRun: dryRunMetricsMap[t.metric.Name],
331+
Name: t.metric.Name,
332+
Phase: v1alpha1.AnalysisPhaseRunning,
333+
DryRun: dryRunMetricsMap[t.metric.Name],
334+
Metadata: provider.GetMetadata(t.metric),
333335
}
334336
}
335337

336338
var newMeasurement v1alpha1.Measurement
337-
provider, err := c.newProvider(*logger, t.metric)
338339
if err != nil {
339340
if t.incompleteMeasurement != nil {
340341
newMeasurement = *t.incompleteMeasurement

analysis/analysis_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,7 @@ func TestReconcileAnalysisRunInitial(t *testing.T) {
892892
},
893893
}
894894
f.provider.On("Run", mock.Anything, mock.Anything, mock.Anything).Return(newMeasurement(v1alpha1.AnalysisPhaseSuccessful), nil)
895+
f.provider.On("GetMetadata", mock.Anything, mock.Anything).Return(map[string]string{}, nil)
895896
{
896897
newRun := c.reconcileAnalysisRun(run)
897898
assert.Equal(t, v1alpha1.AnalysisPhaseRunning, newRun.Status.MetricResults[0].Phase)
@@ -1128,6 +1129,38 @@ func TestResolveMetricArgsUnableToSubstitute(t *testing.T) {
11281129
}
11291130
}
11301131

1132+
func TestGetMetadataIsCalled(t *testing.T) {
1133+
f := newFixture(t)
1134+
defer f.Close()
1135+
c, _, _ := f.newController(noResyncPeriodFunc)
1136+
arg := "success-rate"
1137+
run := &v1alpha1.AnalysisRun{
1138+
Spec: v1alpha1.AnalysisRunSpec{
1139+
Args: []v1alpha1.Argument{
1140+
{
1141+
Name: "metric-name",
1142+
Value: &arg,
1143+
},
1144+
},
1145+
Metrics: []v1alpha1.Metric{{
1146+
Name: "rate",
1147+
SuccessCondition: "result[0] > 0",
1148+
Provider: v1alpha1.MetricProvider{
1149+
Prometheus: &v1alpha1.PrometheusMetric{
1150+
Query: "{{args.metric-name}}",
1151+
},
1152+
},
1153+
}},
1154+
},
1155+
}
1156+
metricMetadata := map[string]string{"foo": "bar"}
1157+
f.provider.On("Run", mock.Anything, mock.Anything, mock.Anything).Return(newMeasurement(v1alpha1.AnalysisPhaseSuccessful), nil)
1158+
f.provider.On("GetMetadata", mock.Anything, mock.Anything).Return(metricMetadata, nil)
1159+
newRun := c.reconcileAnalysisRun(run)
1160+
assert.Equal(t, v1alpha1.AnalysisPhaseSuccessful, newRun.Status.Phase)
1161+
assert.Equal(t, metricMetadata, newRun.Status.MetricResults[0].Metadata)
1162+
}
1163+
11311164
// TestSecretContentReferenceSuccess verifies that secret arguments are properly resolved
11321165
func TestSecretContentReferenceSuccess(t *testing.T) {
11331166
f := newFixture(t)
@@ -1172,6 +1205,7 @@ func TestSecretContentReferenceSuccess(t *testing.T) {
11721205
},
11731206
}
11741207
f.provider.On("Run", mock.Anything, mock.Anything, mock.Anything).Return(newMeasurement(v1alpha1.AnalysisPhaseSuccessful), nil)
1208+
f.provider.On("GetMetadata", mock.Anything, mock.Anything).Return(map[string]string{}, nil)
11751209
newRun := c.reconcileAnalysisRun(run)
11761210
assert.Equal(t, v1alpha1.AnalysisPhaseSuccessful, newRun.Status.Phase)
11771211
}
@@ -1236,6 +1270,7 @@ func TestSecretContentReferenceProviderError(t *testing.T) {
12361270
measurement.Message = error.Error()
12371271

12381272
f.provider.On("Run", mock.Anything, mock.Anything, mock.Anything).Return(measurement)
1273+
f.provider.On("GetMetadata", mock.Anything, mock.Anything).Return(map[string]string{}, nil)
12391274
newRun := c.reconcileAnalysisRun(run)
12401275
logMessage := buf.String()
12411276

@@ -1297,6 +1332,7 @@ func TestSecretContentReferenceAndMultipleArgResolutionSuccess(t *testing.T) {
12971332
}
12981333

12991334
f.provider.On("Run", mock.Anything, mock.Anything, mock.Anything).Return(newMeasurement(v1alpha1.AnalysisPhaseSuccessful), nil)
1335+
f.provider.On("GetMetadata", mock.Anything, mock.Anything).Return(map[string]string{}, nil)
13001336
newRun := c.reconcileAnalysisRun(run)
13011337
assert.Equal(t, v1alpha1.AnalysisPhaseSuccessful, newRun.Status.Phase)
13021338
}

hack/tools.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build tools
12
// +build tools
23

34
package tools

manifests/crds/analysis-run-crd.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2817,6 +2817,10 @@ spec:
28172817
type: array
28182818
message:
28192819
type: string
2820+
metadata:
2821+
additionalProperties:
2822+
type: string
2823+
type: object
28202824
name:
28212825
type: string
28222826
phase:

manifests/install.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2818,6 +2818,10 @@ spec:
28182818
type: array
28192819
message:
28202820
type: string
2821+
metadata:
2822+
additionalProperties:
2823+
type: string
2824+
type: object
28212825
name:
28222826
type: string
28232827
phase:

manifests/namespace-install.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2818,6 +2818,10 @@ spec:
28182818
type: array
28192819
message:
28202820
type: string
2821+
metadata:
2822+
additionalProperties:
2823+
type: string
2824+
type: object
28212825
name:
28222826
type: string
28232827
phase:

metricproviders/cloudwatch/cloudwatch.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ func (p *Provider) Type() string {
5050
return ProviderType
5151
}
5252

53+
// GetMetadata returns any additional metadata which needs to be stored & displayed as part of the metrics result.
54+
func (p *Provider) GetMetadata(metric v1alpha1.Metric) map[string]string {
55+
return nil
56+
}
57+
5358
// Run queries with CloudWatch provider for the metric
5459
func (p *Provider) Run(run *v1alpha1.AnalysisRun, metric v1alpha1.Metric) v1alpha1.Measurement {
5560
startTime := timeutil.MetaNow()

metricproviders/cloudwatch/cloudwatch_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ func TestRunWithQueryError(t *testing.T) {
7878
},
7979
},
8080
}
81+
metricsMetadata := p.GetMetadata(metric)
82+
assert.Nil(t, metricsMetadata)
83+
8184
measurement := p.Run(newAnalysisRun(), metric)
8285
assert.Equal(t, expectedErr.Error(), measurement.Message)
8386
assert.NotNil(t, measurement.StartedAt)
@@ -103,6 +106,9 @@ func TestRunWithResolveArgsError(t *testing.T) {
103106
},
104107
},
105108
}
109+
metricsMetadata := p.GetMetadata(metric)
110+
assert.Nil(t, metricsMetadata)
111+
106112
measurement := p.Run(newAnalysisRun(), metric)
107113
assert.Equal(t, expectedErr.Error(), measurement.Message)
108114
assert.NotNil(t, measurement.StartedAt)
@@ -131,6 +137,9 @@ constraint: Member must not be null'`)
131137
},
132138
},
133139
}
140+
metricsMetadata := p.GetMetadata(metric)
141+
assert.Nil(t, metricsMetadata)
142+
134143
measurement := p.Run(newAnalysisRun(), metric)
135144
assert.Equal(t, expectedErr.Error(), measurement.Message)
136145
assert.NotNil(t, measurement.StartedAt)

metricproviders/datadog/datadog.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ func (p *Provider) Type() string {
5959
return ProviderType
6060
}
6161

62+
// GetMetadata returns any additional metadata which needs to be stored & displayed as part of the metrics result.
63+
func (p *Provider) GetMetadata(metric v1alpha1.Metric) map[string]string {
64+
return nil
65+
}
66+
6267
func (p *Provider) Run(run *v1alpha1.AnalysisRun, metric v1alpha1.Metric) v1alpha1.Measurement {
6368
startTime := timeutil.MetaNow()
6469

metricproviders/datadog/datadog_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,9 @@ func TestRunSuite(t *testing.T) {
314314

315315
provider, _ := NewDatadogProvider(*logCtx, fakeClient)
316316

317+
metricsMetadata := provider.GetMetadata(test.metric)
318+
assert.Nil(t, metricsMetadata)
319+
317320
// Get our result
318321
measurement := provider.Run(newAnalysisRun(), test.metric)
319322

0 commit comments

Comments
 (0)