Skip to content

Commit 1dd830b

Browse files
authored
Correlations: Migrate config type to root (grafana#91855)
* WIP * Validate new field, and add value in provisioning if not defined in correct spot * Simplify logic, use correct value * fix tests * Fix linter errors * fix swagger and tests * 😬 * Auto-generation isnt doing this.. * Fix linter * test if nullable is the issue… * Change structure on the frontend fields * Try with backtick * try programatic quoting * Try only quote non-ints * quoting, no backticks * Remove debugging
1 parent 84e5c3a commit 1dd830b

File tree

21 files changed

+114
-113
lines changed

21 files changed

+114
-113
lines changed

docs/sources/developers/http_api/correlations.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk
3838
"targetUID": "PDDA8E780A17E7EF1",
3939
"label": "My Label",
4040
"description": "Logs to Traces",
41+
"type": "query",
4142
"config": {
42-
"type": "query",
4343
"field": "message",
4444
"target": {},
4545
}
@@ -65,8 +65,8 @@ Content-Type: application/json
6565
"sourceUID": "uyBf2637k",
6666
"targetUID": "PDDA8E780A17E7EF1",
6767
"uid": "50xhMlg9k",
68+
"type": "query",
6869
"config": {
69-
"type": "query",
7070
"field": "message",
7171
"target": {},
7272
}
@@ -153,8 +153,8 @@ Content-Type: application/json
153153
"sourceUID": "uyBf2637k",
154154
"targetUID": "PDDA8E780A17E7EF1",
155155
"uid": "J6gn7d31L",
156+
"type": "query",
156157
"config": {
157-
"type": "query",
158158
"field": "message",
159159
"target": {}
160160
}
@@ -197,8 +197,8 @@ Content-Type: application/json
197197
"targetUID": "PDDA8E780A17E7EF1",
198198
"uid": "J6gn7d31L",
199199
"provisioned": false,
200+
"type": "query",
200201
"config": {
201-
"type": "query",
202202
"field": "message",
203203
"target": {},
204204
}
@@ -239,8 +239,8 @@ Content-Type: application/json
239239
"targetUID": "PDDA8E780A17E7EF1",
240240
"uid": "J6gn7d31L",
241241
"provisioned": false,
242+
"type": "query",
242243
"config": {
243-
"type": "query",
244244
"field": "message",
245245
"target": {},
246246
}
@@ -252,8 +252,8 @@ Content-Type: application/json
252252
"targetUID": "P15396BDD62B2BE29",
253253
"uid": "uWCpURgVk",
254254
"provisioned": false,
255+
"type": "query",
255256
"config": {
256-
"type": "query",
257257
"field": "message",
258258
"target": {},
259259
}
@@ -301,8 +301,8 @@ Content-Type: application/json
301301
"targetUID": "PDDA8E780A17E7EF1",
302302
"uid": "J6gn7d31L",
303303
"provisioned": false,
304+
"type": "query",
304305
"config": {
305-
"type": "query",
306306
"field": "message",
307307
"target": {},
308308
}
@@ -314,8 +314,8 @@ Content-Type: application/json
314314
"targetUID": "P15396BDD62B2BE29",
315315
"uid": "uWCpURgVk",
316316
"provisioned": false,
317+
"type": "query",
317318
"config": {
318-
"type": "query",
319319
"field": "message",
320320
"target": {},
321321
}

pkg/services/correlations/database.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func (s CorrelationsService) createCorrelation(ctx context.Context, cmd CreateCo
2222
Description: cmd.Description,
2323
Config: cmd.Config,
2424
Provisioned: cmd.Provisioned,
25+
Type: cmd.Type,
2526
}
2627

2728
err := s.SQLStore.WithTransactionalDbSession(ctx, func(session *db.Session) error {
@@ -131,14 +132,14 @@ func (s CorrelationsService) updateCorrelation(ctx context.Context, cmd UpdateCo
131132
correlation.Description = *cmd.Description
132133
session.MustCols("description")
133134
}
135+
if cmd.Type != nil {
136+
correlation.Type = *cmd.Type
137+
}
134138
if cmd.Config != nil {
135139
session.MustCols("config")
136140
if cmd.Config.Field != nil {
137141
correlation.Config.Field = *cmd.Config.Field
138142
}
139-
if cmd.Config.Type != nil {
140-
correlation.Config.Type = *cmd.Config.Type
141-
}
142143
if cmd.Config.Target != nil {
143144
correlation.Config.Target = *cmd.Config.Target
144145
}

pkg/services/correlations/models.go

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const (
2727
QuotaTarget quota.Target = "correlations"
2828
)
2929

30-
type CorrelationConfigType string
30+
type CorrelationType string
3131

3232
type Transformation struct {
3333
//Enum: regex,logfmt
@@ -38,11 +38,11 @@ type Transformation struct {
3838
}
3939

4040
const (
41-
ConfigTypeQuery CorrelationConfigType = "query"
41+
TypeQuery CorrelationType = "query"
4242
)
4343

44-
func (t CorrelationConfigType) Validate() error {
45-
if t != ConfigTypeQuery {
44+
func (t CorrelationType) Validate() error {
45+
if t != TypeQuery {
4646
return fmt.Errorf("%s: \"%s\"", ErrInvalidConfigType, t)
4747
}
4848
return nil
@@ -68,8 +68,9 @@ type CorrelationConfig struct {
6868
// example: message
6969
Field string `json:"field" binding:"Required"`
7070
// Target type
71-
// required:true
72-
Type CorrelationConfigType `json:"type" binding:"Required"`
71+
// This is deprecated: use the type property outside of config
72+
// deprecated:true
73+
Type CorrelationType `json:"type"`
7374
// Target data query
7475
// required:true
7576
// example: {"prop1":"value1","prop2":"value"}
@@ -87,12 +88,10 @@ func (c CorrelationConfig) MarshalJSON() ([]byte, error) {
8788
target = map[string]any{}
8889
}
8990
return json.Marshal(struct {
90-
Type CorrelationConfigType `json:"type"`
91-
Field string `json:"field"`
92-
Target map[string]any `json:"target"`
93-
Transformations Transformations `json:"transformations,omitempty"`
91+
Field string `json:"field"`
92+
Target map[string]any `json:"target"`
93+
Transformations Transformations `json:"transformations,omitempty"`
9494
}{
95-
Type: ConfigTypeQuery,
9695
Field: c.Field,
9796
Target: target,
9897
Transformations: transformations,
@@ -124,6 +123,8 @@ type Correlation struct {
124123
Config CorrelationConfig `json:"config" xorm:"jsonb config"`
125124
// Provisioned True if the correlation was created during provisioning
126125
Provisioned bool `json:"provisioned"`
126+
// The type of correlation. Currently, only valid value is "query"
127+
Type CorrelationType `json:"type" binding:"Required"`
127128
}
128129

129130
type GetCorrelationsResponseBody struct {
@@ -147,7 +148,7 @@ type CreateCorrelationCommand struct {
147148
// UID of the data source for which correlation is created.
148149
SourceUID string `json:"-"`
149150
OrgId int64 `json:"-"`
150-
// Target data source UID to which the correlation is created. required if config.type = query
151+
// Target data source UID to which the correlation is created. required if type = query
151152
// example: PE1C5CBDA0504A6A3
152153
TargetUID *string `json:"targetUID"`
153154
// Optional label identifying the correlation
@@ -160,14 +161,16 @@ type CreateCorrelationCommand struct {
160161
Config CorrelationConfig `json:"config" binding:"Required"`
161162
// True if correlation was created with provisioning. This makes it read-only.
162163
Provisioned bool `json:"provisioned"`
164+
// correlation type, currently only valid value is "query"
165+
Type CorrelationType `json:"type" binding:"Required"`
163166
}
164167

165168
func (c CreateCorrelationCommand) Validate() error {
166-
if err := c.Config.Type.Validate(); err != nil {
169+
if err := c.Type.Validate(); err != nil {
167170
return err
168171
}
169-
if c.TargetUID == nil && c.Config.Type == ConfigTypeQuery {
170-
return fmt.Errorf("correlations of type \"%s\" must have a targetUID", ConfigTypeQuery)
172+
if c.TargetUID == nil && c.Type == TypeQuery {
173+
return fmt.Errorf("correlations of type \"%s\" must have a targetUID", TypeQuery)
171174
}
172175

173176
if err := c.Config.Transformations.Validate(); err != nil {
@@ -202,8 +205,6 @@ type CorrelationConfigUpdateDTO struct {
202205
// Field used to attach the correlation link
203206
// example: message
204207
Field *string `json:"field"`
205-
// Target type
206-
Type *CorrelationConfigType `json:"type"`
207208
// Target data query
208209
// example: {"prop1":"value1","prop2":"value"}
209210
Target *map[string]any `json:"target"`
@@ -212,16 +213,6 @@ type CorrelationConfigUpdateDTO struct {
212213
Transformations []Transformation `json:"transformations"`
213214
}
214215

215-
func (c CorrelationConfigUpdateDTO) Validate() error {
216-
if c.Type != nil {
217-
if err := c.Type.Validate(); err != nil {
218-
return err
219-
}
220-
}
221-
222-
return nil
223-
}
224-
225216
// UpdateCorrelationCommand is the command for updating a correlation
226217
// swagger:model
227218
type UpdateCorrelationCommand struct {
@@ -238,16 +229,12 @@ type UpdateCorrelationCommand struct {
238229
Description *string `json:"description"`
239230
// Correlation Configuration
240231
Config *CorrelationConfigUpdateDTO `json:"config"`
232+
// correlation type
233+
Type *CorrelationType `json:"type"`
241234
}
242235

243236
func (c UpdateCorrelationCommand) Validate() error {
244-
if c.Config != nil {
245-
if err := c.Config.Validate(); err != nil {
246-
return err
247-
}
248-
}
249-
250-
if c.Label == nil && c.Description == nil && (c.Config == nil || (c.Config.Field == nil && c.Config.Type == nil && c.Config.Target == nil)) {
237+
if c.Label == nil && c.Description == nil && c.Type == nil && (c.Config == nil || (c.Config.Field == nil && c.Config.Target == nil)) {
251238
return ErrUpdateCorrelationEmptyParams
252239
}
253240

pkg/services/correlations/models_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ func TestCorrelationModels(t *testing.T) {
1414
config := &CorrelationConfig{
1515
Field: "field",
1616
Target: map[string]any{},
17-
Type: ConfigTypeQuery,
1817
}
1918
cmd := &CreateCorrelationCommand{
2019
SourceUID: "some-uid",
2120
OrgId: 1,
2221
TargetUID: &targetUid,
2322
Config: *config,
23+
Type: TypeQuery,
2424
}
2525

2626
require.NoError(t, cmd.Validate())
@@ -30,7 +30,7 @@ func TestCorrelationModels(t *testing.T) {
3030
config := &CorrelationConfig{
3131
Field: "field",
3232
Target: map[string]any{},
33-
Type: ConfigTypeQuery,
33+
Type: TypeQuery,
3434
}
3535
cmd := &CreateCorrelationCommand{
3636
SourceUID: "some-uid",
@@ -60,7 +60,7 @@ func TestCorrelationModels(t *testing.T) {
6060
t.Run("CorrelationConfigType Validate", func(t *testing.T) {
6161
t.Run("Successfully validates a correct type", func(t *testing.T) {
6262
type test struct {
63-
input CorrelationConfigType
63+
input CorrelationType
6464
assertion require.ErrorAssertionFunc
6565
}
6666

@@ -79,13 +79,12 @@ func TestCorrelationModels(t *testing.T) {
7979
t.Run("Applies a default empty object if target is not defined", func(t *testing.T) {
8080
config := CorrelationConfig{
8181
Field: "field",
82-
Type: ConfigTypeQuery,
8382
}
8483

8584
data, err := json.Marshal(config)
8685
require.NoError(t, err)
8786

88-
require.Equal(t, `{"type":"query","field":"field","target":{}}`, string(data))
87+
require.Equal(t, `{"field":"field","target":{}}`, string(data))
8988
})
9089
})
9190
}

pkg/services/provisioning/datasources/datasources.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,21 @@ func (dc *DatasourceProvisioner) applyChanges(ctx context.Context, configPath st
192192
}
193193

194194
func makeCreateCorrelationCommand(correlation map[string]any, SourceUID string, OrgId int64) (correlations.CreateCorrelationCommand, error) {
195+
// we look for a correlation type at the root if it is defined, if not use default
196+
// we ignore the legacy config.type value - the only valid value at that version was "query"
197+
var corrType = correlation["type"]
198+
if corrType == nil || corrType == "" {
199+
corrType = correlations.TypeQuery
200+
}
201+
195202
var json = jsoniter.ConfigCompatibleWithStandardLibrary
196203
createCommand := correlations.CreateCorrelationCommand{
197204
SourceUID: SourceUID,
198205
Label: correlation["label"].(string),
199206
Description: correlation["description"].(string),
200207
OrgId: OrgId,
201208
Provisioned: true,
209+
Type: corrType.(correlations.CorrelationType),
202210
}
203211

204212
targetUID, ok := correlation["targetUID"].(string)
@@ -222,11 +230,6 @@ func makeCreateCorrelationCommand(correlation map[string]any, SourceUID string,
222230
}
223231

224232
createCommand.Config = config
225-
} else {
226-
// when provisioning correlations without config we default to type="query"
227-
createCommand.Config = correlations.CorrelationConfig{
228-
Type: correlations.ConfigTypeQuery,
229-
}
230233
}
231234
if err := createCommand.Validate(); err != nil {
232235
return correlations.CreateCorrelationCommand{}, err

pkg/services/sqlstore/migrations/correlations_mig.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ func addCorrelationsMigrations(mg *Migrator) {
3838
// All existing records will have '0' assigned
3939
{Name: "org_id", Type: DB_BigInt, IsPrimaryKey: true, Default: "0"},
4040
{Name: "source_uid", Type: DB_NVarchar, Length: 40, Nullable: false, IsPrimaryKey: true},
41-
// Nullable because in the future we want to have correlations to external resources
4241
{Name: "target_uid", Type: DB_NVarchar, Length: 40, Nullable: true},
4342
{Name: "label", Type: DB_Text, Nullable: false},
4443
{Name: "description", Type: DB_Text, Nullable: false},
@@ -63,4 +62,8 @@ func addCorrelationsMigrations(mg *Migrator) {
6362
mg.AddMigration("add provisioning column", NewAddColumnMigration(correlationsV2, &Column{
6463
Name: "provisioned", Type: DB_Bool, Nullable: false, Default: "0",
6564
}))
65+
66+
mg.AddMigration("add type column", NewAddColumnMigration(correlationsV2, &Column{
67+
Name: "type", Type: DB_NVarchar, Length: 40, Nullable: false, Default: "'query'",
68+
}))
6669
}

pkg/services/stats/statsimpl/stats_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func populateDB(t *testing.T, db db.DB, cfg *setting.Cfg) {
104104
Config: correlations.CorrelationConfig{
105105
Field: "field",
106106
Target: map[string]any{},
107-
Type: correlations.ConfigTypeQuery,
107+
Type: correlations.TypeQuery,
108108
},
109109
}
110110
correlation, err := correlationsSvc.CreateCorrelation(context.Background(), cmd)

0 commit comments

Comments
 (0)