Skip to content

Commit 428c4cf

Browse files
authored
atlasexec: update schema stats inspect command (#3632)
* atlasexec: update schema stats inspect command * test * remove unused function
1 parent 6e2d61f commit 428c4cf

File tree

5 files changed

+9
-103
lines changed

5 files changed

+9
-103
lines changed

atlasexec/atlas_models.go

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,11 @@ package atlasexec
66

77
import (
88
"errors"
9-
"fmt"
10-
"strings"
119
"time"
1210

1311
"ariga.io/atlas/sql/schema"
1412
"ariga.io/atlas/sql/sqlcheck"
1513
"ariga.io/atlas/sql/sqlclient"
16-
17-
"github.com/prometheus/common/expfmt"
18-
"github.com/prometheus/common/model"
1914
)
2015

2116
type (
@@ -174,41 +169,3 @@ func (r *SummaryReport) Errors() []error {
174169
}
175170
return errs
176171
}
177-
178-
// ParsePrometheusMetrics parses Prometheus format metrics and extract table size metrics
179-
// data is expected to be of this format:
180-
// atlas_table_size_bytes{schema="public",table="users"} 123456
181-
// atlas_table_size_bytes{schema="public",table="orders"} 789012
182-
func ParsePrometheusMetrics(data string) ([]TableSizeMetric, error) {
183-
var metrics []TableSizeMetric
184-
parser := expfmt.NewTextParser(model.LegacyValidation)
185-
metricFamilies, err := parser.TextToMetricFamilies(strings.NewReader(data))
186-
if err != nil {
187-
return nil, fmt.Errorf("failed to parse prometheus metrics: %w", err)
188-
}
189-
for _, mf := range metricFamilies {
190-
if mf.GetName() == MetricTableSizeBytes {
191-
for _, metric := range mf.GetMetric() {
192-
var schema, table string
193-
for _, label := range metric.GetLabel() {
194-
switch label.GetName() {
195-
case "schema":
196-
schema = label.GetValue()
197-
case "table":
198-
table = label.GetValue()
199-
}
200-
}
201-
var value float64
202-
if gauge := metric.GetGauge(); gauge != nil {
203-
value = gauge.GetValue()
204-
}
205-
metrics = append(metrics, TableSizeMetric{
206-
Schema: schema,
207-
Table: table,
208-
Value: value,
209-
})
210-
}
211-
}
212-
}
213-
return metrics, nil
214-
}

atlasexec/atlas_schema.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,6 @@ type (
267267
Vars VarArgs
268268

269269
URL string
270-
Format string
271270
Exclude []string
272271
Include []string
273272
Schema []string
@@ -804,8 +803,8 @@ func (c *Client) SchemaLint(ctx context.Context, params *SchemaLintParams) (*Sch
804803
}
805804

806805
// SchemaStatsInspect runs the 'schema stats inspect' command.
807-
func (c *Client) SchemaStatsInspect(ctx context.Context, params *SchemaStatsInspectParams) ([]TableSizeMetric, error) {
808-
args := []string{"schema", "stats", "inspect"}
806+
func (c *Client) SchemaStatsInspect(ctx context.Context, params *SchemaStatsInspectParams) (string, error) {
807+
args := []string{"schema", "stats", "inspect", "--format", "{{ json .Realm }}"}
809808
if params.Env != "" {
810809
args = append(args, "--env", params.Env)
811810
}
@@ -815,9 +814,6 @@ func (c *Client) SchemaStatsInspect(ctx context.Context, params *SchemaStatsInsp
815814
if params.URL != "" {
816815
args = append(args, "--url", params.URL)
817816
}
818-
if params.Format != "" {
819-
args = append(args, "--format", params.Format)
820-
}
821817
if len(params.Schema) > 0 {
822818
args = append(args, "--schema", listString(params.Schema))
823819
}
@@ -830,11 +826,7 @@ func (c *Client) SchemaStatsInspect(ctx context.Context, params *SchemaStatsInsp
830826
if params.Vars != nil {
831827
args = append(args, params.Vars.AsArgs()...)
832828
}
833-
output, err := stringVal(c.runCommand(ctx, args))
834-
if err != nil {
835-
return nil, err
836-
}
837-
return ParsePrometheusMetrics(output)
829+
return stringVal(c.runCommand(ctx, args))
838830
}
839831

840832
// AsArgs returns the parameters as arguments.

atlasexec/atlas_schema_test.go

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -914,35 +914,3 @@ func TestSchema_Lint(t *testing.T) {
914914
})
915915
}
916916
}
917-
918-
func TestSchema_StatsInspect(t *testing.T) {
919-
wd, err := os.Getwd()
920-
require.NoError(t, err)
921-
c, err := atlasexec.NewClient(t.TempDir(), filepath.Join(wd, "./mock-atlas.sh"))
922-
require.NoError(t, err)
923-
924-
// Test case with Prometheus metrics output
925-
prometheusOutput := `# HELP atlas_table_size_bytes Size of the table in bytes.
926-
# TYPE atlas_table_size_bytes gauge
927-
atlas_table_size_bytes{schema="test",table="test"} 16384.0
928-
atlas_table_size_bytes{schema="test",table="users"} 6.832128e+06
929-
`
930-
t.Run("basic stats with prometheus metrics", func(t *testing.T) {
931-
params := &atlasexec.SchemaStatsInspectParams{
932-
URL: "sqlite://test.db",
933-
}
934-
t.Setenv("TEST_ARGS", "schema stats inspect --url sqlite://test.db")
935-
t.Setenv("TEST_STDOUT", prometheusOutput)
936-
result, err := c.SchemaStatsInspect(context.Background(), params)
937-
938-
require.NoError(t, err)
939-
require.Len(t, result, 2)
940-
// Verify metrics
941-
require.Equal(t, "test", result[0].Schema)
942-
require.Equal(t, "test", result[0].Table)
943-
require.Equal(t, 16384.0, result[0].Value)
944-
require.Equal(t, "test", result[1].Schema)
945-
require.Equal(t, "users", result[1].Table)
946-
require.Equal(t, 6832128.0, result[1].Value)
947-
})
948-
}

go.mod

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ require (
88
github.com/go-openapi/inflect v0.19.0
99
github.com/hashicorp/hcl/v2 v2.13.0
1010
github.com/mattn/go-sqlite3 v1.14.28
11-
github.com/prometheus/common v0.66.1
1211
github.com/stretchr/testify v1.11.1
1312
github.com/zclconf/go-cty v1.14.4
1413
github.com/zclconf/go-cty-yaml v1.1.0
@@ -22,13 +21,11 @@ require (
2221
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
2322
github.com/davecgh/go-spew v1.1.1 // indirect
2423
github.com/google/go-cmp v0.7.0 // indirect
24+
github.com/kr/pretty v0.2.1 // indirect
2525
github.com/kr/text v0.2.0 // indirect
2626
github.com/kylelemons/godebug v1.1.0 // indirect
2727
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 // indirect
28-
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
2928
github.com/pmezard/go-difflib v1.0.0 // indirect
30-
github.com/prometheus/client_model v0.6.2 // indirect
31-
go.yaml.in/yaml/v2 v2.4.2 // indirect
3229
golang.org/x/text v0.28.0 // indirect
33-
google.golang.org/protobuf v1.36.8 // indirect
30+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
3431
)

go.sum

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
1919
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
2020
github.com/hashicorp/hcl/v2 v2.13.0 h1:0Apadu1w6M11dyGFxWnmhhcMjkbAiKCv7G1r/2QgCNc=
2121
github.com/hashicorp/hcl/v2 v2.13.0/go.mod h1:e4z5nxYlWNPdDSNYX+ph14EvWYMFm3eP0zIUqPc2jr0=
22-
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
23-
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
22+
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
23+
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
24+
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
25+
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
2426
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
2527
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
2628
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
@@ -29,14 +31,8 @@ github.com/mattn/go-sqlite3 v1.14.28 h1:ThEiQrnbtumT+QMknw63Befp/ce/nUPgBPMlRFEu
2931
github.com/mattn/go-sqlite3 v1.14.28/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
3032
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 h1:DpOJ2HYzCv8LZP15IdmG+YdwD2luVPHITV96TkirNBM=
3133
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
32-
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
33-
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
3434
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
3535
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
36-
github.com/prometheus/client_model v0.6.2 h1:oBsgwpGs7iVziMvrGhE53c/GrLUsZdHnqNwqPLxwZyk=
37-
github.com/prometheus/client_model v0.6.2/go.mod h1:y3m2F6Gdpfy6Ut/GBsUqTWZqCUvMVzSfMLjcu6wAwpE=
38-
github.com/prometheus/common v0.66.1 h1:h5E0h5/Y8niHc5DlaLlWLArTQI7tMrsfQjHV+d9ZoGs=
39-
github.com/prometheus/common v0.66.1/go.mod h1:gcaUsgf3KfRSwHY4dIMXLPV0K/Wg1oZ8+SbZk/HH/dA=
4036
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
4137
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
4238
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
@@ -45,14 +41,10 @@ github.com/zclconf/go-cty v1.14.4 h1:uXXczd9QDGsgu0i/QFR/hzI5NYCHLf6NQw/atrbnhq8
4541
github.com/zclconf/go-cty v1.14.4/go.mod h1:VvMs5i0vgZdhYawQNq5kePSpLAoz8u1xvZgrPIxfnZE=
4642
github.com/zclconf/go-cty-yaml v1.1.0 h1:nP+jp0qPHv2IhUVqmQSzjvqAWcObN0KBkUl2rWBdig0=
4743
github.com/zclconf/go-cty-yaml v1.1.0/go.mod h1:9YLUH4g7lOhVWqUbctnVlZ5KLpg7JAprQNgxSZ1Gyxs=
48-
go.yaml.in/yaml/v2 v2.4.2 h1:DzmwEr2rDGHl7lsFgAHxmNz/1NlQ7xLIrlN2h5d1eGI=
49-
go.yaml.in/yaml/v2 v2.4.2/go.mod h1:081UH+NErpNdqlCXm3TtEran0rJZGxAYx9hb/ELlsPU=
5044
golang.org/x/mod v0.26.0 h1:EGMPT//Ezu+ylkCijjPc+f4Aih7sZvaAr+O3EHBxvZg=
5145
golang.org/x/mod v0.26.0/go.mod h1:/j6NAhSk8iQ723BGAUyoAcn7SlD7s15Dp9Nd/SfeaFQ=
5246
golang.org/x/text v0.28.0 h1:rhazDwis8INMIwQ4tpjLDzUhx6RlXqZNPEM0huQojng=
5347
golang.org/x/text v0.28.0/go.mod h1:U8nCwOR8jO/marOQ0QbDiOngZVEBB7MAiitBuMjXiNU=
54-
google.golang.org/protobuf v1.36.8 h1:xHScyCOEuuwZEc6UtSOvPbAT4zRh0xcNRYekJwfqyMc=
55-
google.golang.org/protobuf v1.36.8/go.mod h1:fuxRtAxBytpl4zzqUh6/eyUujkJdNiuEkXntxiD/uRU=
5648
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
5749
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
5850
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=

0 commit comments

Comments
 (0)