Correlations: Allow correlations to target URLs (#92442)

* Pass one

* Fix linter and add new betterer problem (sorry)

* fix swagger

* Add type to tests and update single correlations sql

* Fix provisioning test and other function that needs a type

* Add errors around query/external typing and add tests

* increment number of correlations tested as we added one for testing v1 type placement

* try merging back the swagger that is in main

* try again?

* Style form a little

* Update public/app/features/logs/components/logParser.ts

Co-authored-by: Matias Chomicki <matyax@gmail.com>

* fix bad commit, simplify logic

* Demonstrating type difficulties

* Fix distributed union changes

* Additional type changes

* Update types in form

* Fix swagger

* Add comment around the assertion and explicit typing

---------

Co-authored-by: Matias Chomicki <matyax@gmail.com>
Co-authored-by: Andrej Ocenas <mr.ocenas@gmail.com>
This commit is contained in:
Kristina
2024-09-24 09:38:17 -05:00
committed by GitHub
parent 8da1d78c92
commit 002f872ce1
38 changed files with 574 additions and 228 deletions
@@ -193,6 +193,11 @@ func (c TestContext) createCorrelation(cmd correlations.CreateCorrelationCommand
return correlation
}
func (c TestContext) createCorrelationPassError(cmd correlations.CreateCorrelationCommand) (correlations.Correlation, error) {
c.t.Helper()
return c.env.Server.HTTPServer.CorrelationsService.CreateCorrelation(context.Background(), cmd)
}
func (c TestContext) createOrUpdateCorrelation(cmd correlations.CreateCorrelationCommand) {
c.t.Helper()
err := c.env.Server.HTTPServer.CorrelationsService.CreateOrUpdateCorrelation(context.Background(), cmd)
@@ -230,7 +230,7 @@ func TestIntegrationCreateCorrelation(t *testing.T) {
description := "a description"
label := "a label"
fieldName := "fieldName"
corrType := correlations.TypeQuery
corrType := correlations.CorrelationType("query")
transformation := correlations.Transformation{Type: "logfmt"}
transformation2 := correlations.Transformation{Type: "regex", Expression: "testExpression", MapValue: "testVar"}
res := ctx.Post(PostParams{
@@ -135,6 +135,7 @@ func TestIntegrationDeleteCorrelation(t *testing.T) {
TargetUID: &writableDs,
OrgId: writableDsOrgId,
Provisioned: true,
Type: correlations.CorrelationType("query"),
})
res := ctx.Delete(DeleteParams{
@@ -160,6 +161,7 @@ func TestIntegrationDeleteCorrelation(t *testing.T) {
SourceUID: writableDs,
TargetUID: &writableDs,
OrgId: writableDsOrgId,
Type: correlations.CorrelationType("query"),
})
res := ctx.Delete(DeleteParams{
@@ -192,6 +194,7 @@ func TestIntegrationDeleteCorrelation(t *testing.T) {
SourceUID: writableDs,
TargetUID: &readOnlyDS,
OrgId: writableDsOrgId,
Type: correlations.CorrelationType("query"),
})
res := ctx.Delete(DeleteParams{
@@ -225,6 +228,7 @@ func TestIntegrationDeleteCorrelation(t *testing.T) {
TargetUID: &readOnlyDS,
OrgId: writableDsOrgId,
Provisioned: false,
Type: correlations.CorrelationType("query"),
})
ctx.createCorrelation(correlations.CreateCorrelationCommand{
@@ -232,6 +236,7 @@ func TestIntegrationDeleteCorrelation(t *testing.T) {
TargetUID: &readOnlyDS,
OrgId: writableDsOrgId,
Provisioned: true,
Type: correlations.CorrelationType("query"),
})
res := ctx.Delete(DeleteParams{
@@ -2,6 +2,7 @@ package correlations
import (
"encoding/json"
"fmt"
"io"
"net/http"
"testing"
@@ -38,8 +39,8 @@ func TestIntegrationCreateOrUpdateCorrelation(t *testing.T) {
TargetUID: &dataSource.UID,
OrgId: dataSource.OrgID,
Label: "needs migration",
Type: correlations.CorrelationType("query"),
Config: correlations.CorrelationConfig{
Type: correlations.TypeQuery,
Field: "foo",
Target: map[string]any{},
Transformations: []correlations.Transformation{
@@ -54,8 +55,8 @@ func TestIntegrationCreateOrUpdateCorrelation(t *testing.T) {
TargetUID: &dataSource.UID,
OrgId: dataSource.OrgID,
Label: "existing",
Type: correlations.CorrelationType("query"),
Config: correlations.CorrelationConfig{
Type: correlations.TypeQuery,
Field: "foo",
Target: map[string]any{},
Transformations: []correlations.Transformation{
@@ -65,6 +66,23 @@ func TestIntegrationCreateOrUpdateCorrelation(t *testing.T) {
Provisioned: false,
})
// v1 correlation where type is in config
v1Correlation := ctx.createCorrelation(correlations.CreateCorrelationCommand{
SourceUID: dataSource.UID,
TargetUID: &dataSource.UID,
OrgId: dataSource.OrgID,
Label: "v1 correlation",
Config: correlations.CorrelationConfig{
Type: correlations.CorrelationType("query"),
Field: "foo",
Target: map[string]any{},
Transformations: []correlations.Transformation{
{Type: "logfmt"},
},
},
Provisioned: true,
})
t.Run("Correctly marks existing correlations as provisioned", func(t *testing.T) {
// should be updated
ctx.createOrUpdateCorrelation(correlations.CreateCorrelationCommand{
@@ -75,6 +93,7 @@ func TestIntegrationCreateOrUpdateCorrelation(t *testing.T) {
Description: needsMigration.Description,
Config: needsMigration.Config,
Provisioned: true,
Type: needsMigration.Type,
})
// should be added
@@ -86,6 +105,7 @@ func TestIntegrationCreateOrUpdateCorrelation(t *testing.T) {
Description: needsMigration.Description,
Config: needsMigration.Config,
Provisioned: true,
Type: needsMigration.Type,
})
res := ctx.Get(GetParams{
@@ -101,7 +121,7 @@ func TestIntegrationCreateOrUpdateCorrelation(t *testing.T) {
err = json.Unmarshal(responseBody, &response)
require.NoError(t, err)
require.Len(t, response.Correlations, 3)
require.Len(t, response.Correlations, 4)
unordered := make(map[string]correlations.Correlation)
for _, v := range response.Correlations {
@@ -117,4 +137,44 @@ func TestIntegrationCreateOrUpdateCorrelation(t *testing.T) {
require.NoError(t, res.Body.Close())
})
t.Run("If Config.Type is query, provision without error but have value outside of config", func(t *testing.T) {
res := ctx.Get(GetParams{
url: fmt.Sprintf("/api/datasources/uid/%s/correlations/%s", dataSource.UID, v1Correlation.UID),
user: adminUser,
})
require.Equal(t, http.StatusOK, res.StatusCode)
responseBody, err := io.ReadAll(res.Body)
require.NoError(t, err)
var response correlations.Correlation
err = json.Unmarshal(responseBody, &response)
require.NoError(t, err)
require.EqualValues(t, response.Config.Type, "")
require.EqualValues(t, v1Correlation.Config.Type, response.Type)
require.NoError(t, res.Body.Close())
})
t.Run("If Config.type is not query, throw an error", func(t *testing.T) {
_, err := ctx.createCorrelationPassError(correlations.CreateCorrelationCommand{
SourceUID: dataSource.UID,
TargetUID: &dataSource.UID,
OrgId: dataSource.OrgID,
Label: "bad v1 correlation",
Config: correlations.CorrelationConfig{
Type: correlations.CorrelationType("external"),
Field: "foo",
Target: map[string]any{},
Transformations: []correlations.Transformation{
{Type: "logfmt"},
},
},
Provisioned: true,
})
require.Error(t, err)
require.ErrorIs(t, err, correlations.ErrInvalidConfigType)
})
}
@@ -77,7 +77,7 @@ func TestIntegrationReadCorrelation(t *testing.T) {
SourceUID: dsWithCorrelations.UID,
TargetUID: &dsWithCorrelations.UID,
OrgId: dsWithCorrelations.OrgID,
Type: correlations.TypeQuery,
Type: correlations.CorrelationType("query"),
Config: correlations.CorrelationConfig{
Field: "foo",
Target: map[string]any{},
@@ -128,6 +128,7 @@ func TestIntegrationUpdateCorrelation(t *testing.T) {
TargetUID: &writableDs,
OrgId: writableDsOrgId,
Provisioned: true,
Type: correlations.CorrelationType("query"),
})
res := ctx.Patch(PatchParams{
@@ -155,6 +156,7 @@ func TestIntegrationUpdateCorrelation(t *testing.T) {
SourceUID: writableDs,
TargetUID: &writableDs,
OrgId: writableDsOrgId,
Type: correlations.CorrelationType("query"),
})
// no params
@@ -220,6 +222,7 @@ func TestIntegrationUpdateCorrelation(t *testing.T) {
TargetUID: &writableDs,
OrgId: writableDsOrgId,
Label: "a label",
Type: correlations.CorrelationType("query"),
})
res := ctx.Patch(PatchParams{
@@ -249,9 +252,9 @@ func TestIntegrationUpdateCorrelation(t *testing.T) {
OrgId: writableDsOrgId,
Label: "0",
Description: "0",
Type: correlations.CorrelationType("query"),
Config: correlations.CorrelationConfig{
Field: "fieldName",
Type: "query",
Target: map[string]any{"expr": "foo"},
},
})