Skip to content

Commit

Permalink
cli: support metrics_config metadata object
Browse files Browse the repository at this point in the history
PR-URL: hasura/graphql-engine-mono#7225
GitOrigin-RevId: e0d6f940d4b62b520948ca179cac39474f4d6d84
  • Loading branch information
scriptonist authored and hasura-bot committed Dec 13, 2022
1 parent bffdc2f commit fbfea54
Show file tree
Hide file tree
Showing 10 changed files with 275 additions and 0 deletions.
1 change: 1 addition & 0 deletions cli/internal/metadataobject/metadataobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const (
InheritedRolesKey string = "inherited_roles"
GraphQLSchemaIntrospectionKey string = "graphql_schema_introspection"
NetworkKey string = "network"
MetricsConfigKey string = "metrics_config"
)

type Objects []Object
Expand Down
121 changes: 121 additions & 0 deletions cli/internal/metadataobject/metrics_config/metrics_config.go
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 cli/internal/metadataobject/metrics_config/metrics_config_test.go
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)
}
}
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
analyze_query_variables: true
analyze_response_body: false
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"metrics_config": {"analyze_query_variables": true, "analyze_response_body": false}}
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
}
}
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 cli/internal/metadatautil/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type metadata struct {

// HGE Pro
GraphQLSchemaIntrospection v3yaml.Node `yaml:"graphql_schema_introspection,omitempty" mapstructure:"graphql_schema_introspection,omitempty"`
MetricsConfig v3yaml.Node `yaml:"metrics_config,omitempty" mapstructure:"metrics_config,omitempty"`

// note: update projectmetadata/handler.go.Metadata to reflect changes made here
// TODO: remove this note once the TODO item above (code reuse) is addressed
Expand Down
1 change: 1 addition & 0 deletions cli/internal/projectmetadata/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ type Metadata struct {

// HGE Pro
GraphQLSchemaIntrospection interface{} `yaml:"graphql_schema_introspection,omitempty" mapstructure:"graphql_schema_introspection,omitempty"`
MetricsConfig interface{} `yaml:"metrics_config,omitempty" mapstructure:"metrics_config,omitempty"`

// note: update metadatautil/json.metadata to reflect changes made here
}
Expand Down
2 changes: 2 additions & 0 deletions cli/internal/projectmetadata/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/hasura/graphql-engine/cli/v2/internal/metadataobject/functions"
graphqlschemaintrospection "github.com/hasura/graphql-engine/cli/v2/internal/metadataobject/graphql_schema_introspection"
inheritedroles "github.com/hasura/graphql-engine/cli/v2/internal/metadataobject/inherited_roles"
metricsconfig "github.com/hasura/graphql-engine/cli/v2/internal/metadataobject/metrics_config"
"github.com/hasura/graphql-engine/cli/v2/internal/metadataobject/network"
"github.com/hasura/graphql-engine/cli/v2/internal/metadataobject/querycollections"
"github.com/hasura/graphql-engine/cli/v2/internal/metadataobject/remoteschemas"
Expand Down Expand Up @@ -62,6 +63,7 @@ func GetMetadataObjectsWithDir(ec *cli.ExecutionContext, dir ...string) metadata
// hasura pro specific metadata objects
objects = append(objects, apilimits.New(ec, metadataDir))
objects = append(objects, graphqlschemaintrospection.New(ec, metadataDir))
objects = append(objects, metricsconfig.New(ec, metadataDir))
}
}

Expand Down

0 comments on commit fbfea54

Please sign in to comment.