Skip to content

Commit

Permalink
fix: registered-metrics-endpoints lists secure endpoint registrations (
Browse files Browse the repository at this point in the history
…#75)

* fix: registered-metrics-endpoints lists secure endpoint registrations

* refactor: fetch multiple types at once
  • Loading branch information
ctlong authored Aug 28, 2023
1 parent f046e3d commit 155e2a0
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 11 deletions.
14 changes: 12 additions & 2 deletions command/command_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,18 @@ func (f *mockRegistrationFetcher) Fetch(appGuid, registrationType string) ([]reg
return reg, f.fetchError
}

func (f *mockRegistrationFetcher) FetchAll(registrationType string) (map[string][]registrations.Registration, error) {
return f.registrations, f.fetchError
func (f *mockRegistrationFetcher) FetchAll(registrationType ...string) (map[string][]registrations.Registration, error) {
result := make(map[string][]registrations.Registration)
for app, regs := range f.registrations {
for _, r := range regs {
for _, t := range registrationType {
if r.Type == t {
result[app] = append(result[app], r)
}
}
}
}
return result, f.fetchError
}

func getFakeAppsInfoResponse(ports []int) string {
Expand Down
2 changes: 1 addition & 1 deletion command/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func ListRegisteredLogFormats(writer io.Writer, fetcher registrationFetcher, lis
}

func ListRegisteredMetricsEndpoints(writer io.Writer, fetcher registrationFetcher, lister appLister, appName string) error {
regs, err := fetcher.FetchAll(metricsEndpoint)
regs, err := fetcher.FetchAll(metricsEndpoint, secureEndpoint)
if err != nil {
return err
}
Expand Down
11 changes: 9 additions & 2 deletions command/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,17 @@ var _ = Describe("List", func() {
})

Describe("ListRegisteredMetricsEndpoints", func() {
It("displays registered log formats", func() {
It("displays registered metric formats", func() {
registrationFetcher := newMockRegistrationFetcher()
registrationFetcher.registrations = map[string][]registrations.Registration{
"app-guid": {
{Type: "metrics-endpoint", Config: "/metrics"},
{Type: "metrics-endpoint", Config: "/promql"},
{Type: "secure-endpoint", Config: ":8081/metrics"},
},
"app-guid-2": {
{Type: "metrics-endpoint", Config: "/promql"},
{Type: "secure-endpoint", Config: ":1234/promql"},
},
}
writer := newSpyWriter()
Expand All @@ -153,20 +155,24 @@ var _ = Describe("List", func() {
"App Path",
"app-name /metrics",
"app-name /promql",
"app-name :8081/metrics",
"app-name-2 /promql",
"app-name-2 :1234/promql",
"",
}))
})

It("only displays registered log formats for the specified apps", func() {
It("only displays registered metric formats for the specified apps", func() {
registrationFetcher := newMockRegistrationFetcher()
registrationFetcher.registrations = map[string][]registrations.Registration{
"app-guid": {
{Type: "metrics-endpoint", Config: "/metrics"},
{Type: "metrics-endpoint", Config: "/promql"},
{Type: "secure-endpoint", Config: ":8081/metrics"},
},
"app-guid-2": {
{Type: "metrics-endpoint", Config: "/promql"},
{Type: "secure-endpoint", Config: ":1234/promql"},
},
}
writer := newSpyWriter()
Expand All @@ -183,6 +189,7 @@ var _ = Describe("List", func() {
"App Path",
"app-name /metrics",
"app-name /promql",
"app-name :8081/metrics",
"",
}))
})
Expand Down
2 changes: 1 addition & 1 deletion command/metric_registrar_cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type MetricRegistrarCli struct {

type registrationFetcher interface {
Fetch(string, string) ([]registrations.Registration, error)
FetchAll(string) (map[string][]registrations.Registration, error)
FetchAll(...string) (map[string][]registrations.Registration, error)
}

type cliCommandRunner interface {
Expand Down
15 changes: 10 additions & 5 deletions registrations/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,27 @@ func NewFetcher(conn cliConn) *Fetcher {
return &Fetcher{cliConn: conn}
}

func (f *Fetcher) FetchAll(registrationType string) (map[string][]Registration, error) {
registrations := map[string][]Registration{}
func (f *Fetcher) FetchAll(registrationTypes ...string) (map[string][]Registration, error) {
services, err := f.getServices()
if err != nil {
return nil, err
}

registrations := make(map[string][]Registration)
for _, s := range services {
r, isRegistration := registration(s.Entity)
if isRegistration && r.Type == registrationType {
r, ok := registration(s.Entity)
if !ok {
continue
}
for _, rt := range registrationTypes {
if r.Type != rt {
continue
}
bindings, err := f.serviceBindings(s.Entity.ServiceBindingsUrl)
if err != nil {
return nil, err
}
r.NumberOfBindings = len(bindings)

for _, binding := range bindings {
registrations[binding.Entity.AppGuid] = append(registrations[binding.Entity.AppGuid], r)
}
Expand Down

0 comments on commit 155e2a0

Please sign in to comment.