-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cli: support
metrics_config
metadata object
PR-URL: hasura/graphql-engine-mono#7225 GitOrigin-RevId: e0d6f940d4b62b520948ca179cac39474f4d6d84
- Loading branch information
1 parent
bffdc2f
commit fbfea54
Showing
10 changed files
with
275 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
cli/internal/metadataobject/metrics_config/metrics_config.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package metricsconfig | ||
|
||
import ( | ||
"io/ioutil" | ||
"path/filepath" | ||
|
||
"github.com/hasura/graphql-engine/cli/v2/internal/errors" | ||
"github.com/hasura/graphql-engine/cli/v2/internal/metadataobject" | ||
|
||
"github.com/sirupsen/logrus" | ||
|
||
"github.com/hasura/graphql-engine/cli/v2" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
type MetricsConfigObject struct { | ||
MetadataDir string | ||
|
||
logger *logrus.Logger | ||
} | ||
|
||
func New(ec *cli.ExecutionContext, baseDir string) *MetricsConfigObject { | ||
return &MetricsConfigObject{ | ||
MetadataDir: baseDir, | ||
logger: ec.Logger, | ||
} | ||
} | ||
|
||
func (m *MetricsConfigObject) Validate() error { | ||
return nil | ||
} | ||
|
||
func (m *MetricsConfigObject) CreateFiles() error { | ||
var op errors.Op = "metricsconfig.MetricsConfigObject.CreateFiles" | ||
var v interface{} | ||
data, err := yaml.Marshal(v) | ||
if err != nil { | ||
return errors.E(op, err) | ||
} | ||
err = ioutil.WriteFile(filepath.Join(m.MetadataDir, m.Filename()), data, 0644) | ||
if err != nil { | ||
return errors.E(op, err) | ||
} | ||
return nil | ||
} | ||
|
||
func (m *MetricsConfigObject) Build() (map[string]interface{}, error) { | ||
var op errors.Op = "metricsconfig.MetricsConfigObject.Build" | ||
data, err := metadataobject.ReadMetadataFile(filepath.Join(m.MetadataDir, m.Filename())) | ||
if err != nil { | ||
return nil, errors.E(op, m.error(err)) | ||
} | ||
var obj map[string]yaml.Node | ||
err = yaml.Unmarshal(data, &obj) | ||
if err != nil { | ||
return nil, errors.E(op, errors.KindBadInput, m.error(err)) | ||
} | ||
// if we have a metrics_config.yaml file which is empty | ||
// do not set this key in the generated metadata | ||
// if we do that and it will effectively send the following JSON to server | ||
// { | ||
// "version": 3, | ||
// "sources": [], | ||
// "metrics_config": {} | ||
// } | ||
// This will result in a server error like the following | ||
// { | ||
// "code": "parse-failed", | ||
// "error": "the key 'analyze_query_variables' was not present", | ||
// "path": "$.args.metadata.metrics_config" | ||
// } | ||
// The is a deviation from the normal server behavior | ||
if len(obj) == 0 { | ||
return nil, nil | ||
} | ||
return map[string]interface{}{m.Key(): obj}, nil | ||
} | ||
|
||
func (m *MetricsConfigObject) Export(metadata map[string]yaml.Node) (map[string][]byte, error) { | ||
var op errors.Op = "metricsconfig.MetricsConfigObject.Export" | ||
b, err := metadataobject.DefaultExport(m, metadata, m.error, metadataobject.DefaultObjectTypeMapping) | ||
if err != nil { | ||
return nil, errors.E(op, err) | ||
} | ||
return b, nil | ||
} | ||
|
||
func (m *MetricsConfigObject) Key() string { | ||
return metadataobject.MetricsConfigKey | ||
} | ||
|
||
func (m *MetricsConfigObject) Filename() string { | ||
return "metrics_config.yaml" | ||
} | ||
|
||
func (m *MetricsConfigObject) GetFiles() ([]string, error) { | ||
var op errors.Op = "metricsconfig.MetricsConfigObject.GetFiles" | ||
rootFile := filepath.Join(m.BaseDirectory(), m.Filename()) | ||
files, err := metadataobject.DefaultGetFiles(rootFile) | ||
if err != nil { | ||
return nil, errors.E(op, m.error(err)) | ||
} | ||
return files, nil | ||
} | ||
|
||
func (m *MetricsConfigObject) WriteDiff(opts metadataobject.WriteDiffOpts) error { | ||
var op errors.Op = "metricsconfig.MetricsConfigObject.WriteDiff" | ||
err := metadataobject.DefaultWriteDiff(metadataobject.DefaultWriteDiffOpts{From: m, WriteDiffOpts: opts}) | ||
if err != nil { | ||
return errors.E(op, m.error(err)) | ||
} | ||
return nil | ||
} | ||
|
||
func (m *MetricsConfigObject) BaseDirectory() string { | ||
return m.MetadataDir | ||
} | ||
|
||
func (m *MetricsConfigObject) error(err error, additionalContext ...string) metadataobject.ErrParsingMetadataObject { | ||
return metadataobject.NewErrParsingMetadataObject(m, err, additionalContext...) | ||
} |
137 changes: 137 additions & 0 deletions
137
cli/internal/metadataobject/metrics_config/metrics_config_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package metricsconfig | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"testing" | ||
|
||
goyaml "github.com/goccy/go-yaml" | ||
"github.com/hasura/graphql-engine/cli/v2/internal/metadatautil" | ||
|
||
"github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
func TestMetricsConfigBuild(t *testing.T) { | ||
type fields struct { | ||
MetadataDir string | ||
logger *logrus.Logger | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
wantGolden string | ||
wantErr bool | ||
assertErr require.ErrorAssertionFunc | ||
}{ | ||
{ | ||
"can build metrics config", | ||
fields{ | ||
MetadataDir: "testdata/build_test/t1/metadata", | ||
logger: logrus.New(), | ||
}, | ||
"testdata/build_test/t1/want.golden.json", | ||
false, | ||
require.NoError, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
tc := &MetricsConfigObject{ | ||
MetadataDir: tt.fields.MetadataDir, | ||
logger: tt.fields.logger, | ||
} | ||
got, err := tc.Build() | ||
tt.assertErr(t, err) | ||
if !tt.wantErr { | ||
assert.NoError(t, err) | ||
gotbs, err := yaml.Marshal(got) | ||
assert.NoError(t, err) | ||
jsonbs, err := goyaml.YAMLToJSON(gotbs) | ||
assert.NoError(t, err) | ||
|
||
// uncomment following lines to update golden file | ||
// assert.NoError(t, ioutil.WriteFile(tt.wantGolden, jsonbs, os.ModePerm)) | ||
|
||
wantbs, err := ioutil.ReadFile(tt.wantGolden) | ||
assert.NoError(t, err) | ||
assert.Equal(t, string(wantbs), string(jsonbs)) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestMetricsConfigExport(t *testing.T) { | ||
type fields struct { | ||
MetadataDir string | ||
logger *logrus.Logger | ||
} | ||
type args struct { | ||
metadata map[string]yaml.Node | ||
} | ||
tests := []struct { | ||
id string | ||
name string | ||
fields fields | ||
args args | ||
want map[string][]byte | ||
wantErr bool | ||
assertErr require.ErrorAssertionFunc | ||
}{ | ||
{ | ||
"t1", | ||
"can export metrics config", | ||
fields{ | ||
MetadataDir: "testdata/metadata", | ||
logger: logrus.New(), | ||
}, | ||
args{ | ||
metadata: func() map[string]yaml.Node { | ||
bs, err := ioutil.ReadFile("testdata/export_test/t1/metadata.json") | ||
assert.NoError(t, err) | ||
yamlbs, err := metadatautil.JSONToYAML(bs) | ||
assert.NoError(t, err) | ||
var v map[string]yaml.Node | ||
assert.NoError(t, yaml.Unmarshal(yamlbs, &v)) | ||
return v | ||
}(), | ||
}, | ||
func() map[string][]byte { | ||
m := map[string][]byte{ | ||
"testdata/metadata/metrics_config.yaml": func() []byte { | ||
bs, err := ioutil.ReadFile("testdata/export_test/t1/want.metrics_config.yaml") | ||
assert.NoError(t, err) | ||
return bs | ||
}(), | ||
} | ||
return m | ||
}(), | ||
false, | ||
require.NoError, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
tc := &MetricsConfigObject{ | ||
MetadataDir: tt.fields.MetadataDir, | ||
logger: tt.fields.logger, | ||
} | ||
got, err := tc.Export(tt.args.metadata) | ||
tt.assertErr(t, err) | ||
for k, v := range got { | ||
fmt.Println(k, string(v)) | ||
} | ||
if !tt.wantErr { | ||
for k, v := range got { | ||
assert.Contains(t, tt.want, k) | ||
// uncomment to update golden files | ||
//assert.NoError(t, ioutil.WriteFile(fmt.Sprintf("testdata/export_test/%v/want.%v", tt.id, filepath.Base(k)), v, os.ModePerm)) | ||
|
||
assert.Equalf(t, string(tt.want[k]), string(v), "%v", k) | ||
} | ||
} | ||
}) | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
...nternal/metadataobject/metrics_config/testdata/build_test/t1/metadata/metrics_config.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
analyze_query_variables: true | ||
analyze_response_body: false |
1 change: 1 addition & 0 deletions
1
cli/internal/metadataobject/metrics_config/testdata/build_test/t1/want.golden.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"metrics_config": {"analyze_query_variables": true, "analyze_response_body": false}} |
7 changes: 7 additions & 0 deletions
7
cli/internal/metadataobject/metrics_config/testdata/export_test/t1/metadata.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"version": 3, | ||
"metrics_config": { | ||
"analyze_query_variables": true, | ||
"analyze_response_body": false | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
cli/internal/metadataobject/metrics_config/testdata/export_test/t1/want.metrics_config.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
analyze_query_variables: true | ||
analyze_response_body: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters