Skip to content

Commit f0325f2

Browse files
authored
support separate metrics config per process group (#2825)
* support separate metrics config per process group Similar to other app-config constructs, allow specifying a separate Metrics config per process group with a `processes` field.
1 parent 393e287 commit f0325f2

File tree

10 files changed

+92
-19
lines changed

10 files changed

+92
-19
lines changed

internal/appconfig/config.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ func NewConfig() *Config {
3333
}
3434
}
3535

36+
type Metrics struct {
37+
*api.MachineMetrics
38+
Processes []string `json:"processes,omitempty" toml:"processes,omitempty"`
39+
}
40+
3641
// Config wraps the properties of app configuration.
3742
// NOTE: If you any new setting here, please also add a value for it at testdata/rull-reference.toml
3843
type Config struct {
@@ -61,8 +66,8 @@ type Config struct {
6166
MergedFiles []*api.File `toml:"-" json:"-"`
6267

6368
// Others, less important.
64-
Statics []Static `toml:"statics,omitempty" json:"statics,omitempty"`
65-
Metrics *api.MachineMetrics `toml:"metrics,omitempty" json:"metrics,omitempty"`
69+
Statics []Static `toml:"statics,omitempty" json:"statics,omitempty"`
70+
Metrics []*Metrics `toml:"metrics,omitempty" json:"metrics,omitempty"`
6671

6772
// RawDefinition contains fly.toml parsed as-is
6873
// If you add any config field that is v2 specific, be sure to remove it in SanitizeDefinition()

internal/appconfig/definition_test.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,16 @@ func TestToDefinition(t *testing.T) {
243243
"env": map[string]any{
244244
"FOO": "BAR",
245245
},
246-
"metrics": map[string]any{
247-
"port": int64(9999),
248-
"path": "/metrics",
246+
"metrics": []map[string]any{
247+
{
248+
"port": int64(9999),
249+
"path": "/metrics",
250+
},
251+
{
252+
"port": int64(9998),
253+
"path": "/metrics",
254+
"processes": []any{"web"},
255+
},
249256
},
250257
"statics": []map[string]any{
251258
{

internal/appconfig/from_machine_set.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ fly.toml only supports one mount per machine at this time. These mounts will be
132132
cfg.AppName = appCompact.Name
133133
cfg.PrimaryRegion = primaryRegion
134134
cfg.Env = m.Machine().Config.Env
135-
cfg.Metrics = m.Machine().Config.Metrics
135+
cfg.Metrics = []*Metrics{
136+
{MachineMetrics: m.Machine().Config.Metrics},
137+
}
136138
cfg.Statics = statics
137139
cfg.Mounts = mounts
138140
cfg.Processes = processGroups.processes

internal/appconfig/machines.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,10 @@ func (c *Config) updateMachineConfig(src *api.MachineConfig) (*api.MachineConfig
105105
}
106106

107107
// Metrics
108-
mConfig.Metrics = c.Metrics
108+
mConfig.Metrics = nil
109+
if len(c.Metrics) > 0 {
110+
mConfig.Metrics = c.Metrics[0].MachineMetrics
111+
}
109112

110113
// Init
111114
cmd, err := c.InitCmd(processGroup)

internal/appconfig/patches.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var configPatches = []patchFuncType{
1717
patchExperimental,
1818
patchTopLevelChecks,
1919
patchMounts,
20+
patchMetrics,
2021
patchTopFields,
2122
patchBuild,
2223
}
@@ -235,6 +236,21 @@ func patchMounts(cfg map[string]any) (map[string]any, error) {
235236
return cfg, nil
236237
}
237238

239+
func patchMetrics(cfg map[string]any) (map[string]any, error) {
240+
var metrics []map[string]any
241+
for _, k := range []string{"metric", "metrics"} {
242+
if raw, ok := cfg[k]; ok {
243+
cast, err := ensureArrayOfMap(raw)
244+
if err != nil {
245+
return nil, fmt.Errorf("Error processing mounts: %w", err)
246+
}
247+
metrics = append(metrics, cast...)
248+
}
249+
}
250+
cfg["metrics"] = metrics
251+
return cfg, nil
252+
}
253+
238254
func patchTopLevelChecks(cfg map[string]any) (map[string]any, error) {
239255
raw, ok := cfg["checks"]
240256
if !ok {

internal/appconfig/processgroups.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func (c *Config) DefaultProcessName() string {
8181

8282
// Flatten generates a machine config specific to a process_group.
8383
//
84-
// Only services, mounts, checks & files specific to the provided progress group will be in the returned config.
84+
// Only services, mounts, checks, metrics & files specific to the provided progress group will be in the returned config.
8585
func (c *Config) Flatten(groupName string) (*Config, error) {
8686
if err := c.SetMachinesPlatform(); err != nil {
8787
return nil, fmt.Errorf("can not flatten an invalid v2 application config: %w", err)
@@ -154,6 +154,11 @@ func (c *Config) Flatten(groupName string) (*Config, error) {
154154
return matchesGroups(x.Processes)
155155
})
156156

157+
// [[metrics]]
158+
dst.Metrics = lo.Filter(c.Metrics, func(x *Metrics, _ int) bool {
159+
return matchesGroups(x.Processes)
160+
})
161+
157162
return dst, nil
158163
}
159164

internal/appconfig/serde_test.go

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,12 @@ func TestLoadTOMLAppConfigExperimental(t *testing.T) {
189189
defaultGroupName: "app",
190190
AppName: "foo",
191191
KillTimeout: api.MustParseDuration("3s"),
192-
Metrics: &api.MachineMetrics{
193-
Path: "/foo",
194-
Port: 9000,
195-
},
192+
Metrics: []*Metrics{{
193+
MachineMetrics: &api.MachineMetrics{
194+
Path: "/foo",
195+
Port: 9000,
196+
},
197+
}},
196198
Experimental: &Experimental{
197199
Cmd: []string{"cmd"},
198200
Entrypoint: []string{"entrypoint"},
@@ -266,6 +268,12 @@ func TestLoadTOMLAppConfigOldFormat(t *testing.T) {
266268
Source: "data",
267269
Destination: "/data",
268270
}},
271+
Metrics: []*Metrics{{
272+
MachineMetrics: &api.MachineMetrics{
273+
Port: 9999,
274+
Path: "/metrics",
275+
},
276+
}},
269277
Services: []Service{
270278
{
271279
InternalPort: 8080,
@@ -324,6 +332,10 @@ func TestLoadTOMLAppConfigOldFormat(t *testing.T) {
324332
"source": "data",
325333
"destination": "/data",
326334
},
335+
"metrics": map[string]any{
336+
"port": int64(9999),
337+
"path": "/metrics",
338+
},
327339
"processes": []map[string]any{{}},
328340
"services": []map[string]any{{
329341
"internal_port": "8080",
@@ -465,9 +477,20 @@ func TestLoadTOMLAppConfigReferenceFormat(t *testing.T) {
465477
"FOO": "BAR",
466478
},
467479

468-
Metrics: &api.MachineMetrics{
469-
Port: 9999,
470-
Path: "/metrics",
480+
Metrics: []*Metrics{
481+
{
482+
MachineMetrics: &api.MachineMetrics{
483+
Port: 9999,
484+
Path: "/metrics",
485+
},
486+
},
487+
{
488+
MachineMetrics: &api.MachineMetrics{
489+
Port: 9998,
490+
Path: "/metrics",
491+
},
492+
Processes: []string{"web"},
493+
},
471494
},
472495

473496
HTTPService: &HTTPService{

internal/appconfig/testdata/full-reference.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,15 @@ host_dedication_id = "06031957"
3939
[env]
4040
FOO = "BAR"
4141

42-
[metrics]
42+
[[metrics]]
4343
port = 9999
4444
path = "/metrics"
4545

46+
[[metrics]]
47+
port = 9998
48+
path = "/metrics"
49+
processes = ["web"]
50+
4651
[http_service]
4752
internal_port = 8080
4853
force_https = true

internal/appconfig/testdata/old-format.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,8 @@ build_target = "thalayer"
5959
# singular mount
6060
source = "data"
6161
destination = "/data"
62+
63+
[metrics]
64+
# singular metrics
65+
port = 9999
66+
path = "/metrics"

internal/command/deploy/machines_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,11 @@ func Test_resolveUpdatedMachineConfig_ReleaseCommand(t *testing.T) {
6666
"PRIMARY_REGION": "scl",
6767
"OTHER": "value",
6868
},
69-
Metrics: &api.MachineMetrics{
70-
Port: 9000,
71-
Path: "/prometheus",
69+
Metrics: []*appconfig.Metrics{{
70+
MachineMetrics: &api.MachineMetrics{
71+
Port: 9000,
72+
Path: "/prometheus",
73+
}},
7274
},
7375
Deploy: &appconfig.Deploy{
7476
ReleaseCommand: "touch sky",

0 commit comments

Comments
 (0)