Skip to content

Commit

Permalink
Add name to multi to differentiate multiple instances.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcdee committed Mar 5, 2024
1 parent 48c9750 commit 3341a4b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 19 deletions.
8 changes: 4 additions & 4 deletions multi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (s *Service) deactivateClient(ctx context.Context, client consensusclient.S
for _, activeClient := range s.activeClients {
if activeClient == client {
inactiveClients = append(inactiveClients, activeClient)
setProviderStateMetric(ctx, client.Address(), "inactive")
s.setProviderStateMetric(ctx, client.Address(), "inactive")
} else {
activeClients = append(activeClients, activeClient)
}
Expand All @@ -95,7 +95,7 @@ func (s *Service) deactivateClient(ctx context.Context, client consensusclient.S

s.activeClients = activeClients
s.inactiveClients = inactiveClients
setConnectionsMetric(ctx, len(s.activeClients), len(s.inactiveClients))
s.setConnectionsMetric(ctx, len(s.activeClients), len(s.inactiveClients))
}

// activateClient activates a client, moving it to the active list if not currently on it.
Expand All @@ -110,7 +110,7 @@ func (s *Service) activateClient(ctx context.Context, client consensusclient.Ser
for _, inactiveClient := range s.inactiveClients {
if inactiveClient == client {
activeClients = append(activeClients, inactiveClient)
setProviderStateMetric(ctx, client.Address(), "active")
s.setProviderStateMetric(ctx, client.Address(), "active")
} else {
inactiveClients = append(inactiveClients, inactiveClient)
}
Expand All @@ -124,7 +124,7 @@ func (s *Service) activateClient(ctx context.Context, client consensusclient.Ser

s.activeClients = activeClients
s.inactiveClients = inactiveClients
setConnectionsMetric(ctx, len(s.activeClients), len(s.inactiveClients))
s.setConnectionsMetric(ctx, len(s.activeClients), len(s.inactiveClients))
}

// callFunc is the definition for a call function. It provides a generic return interface
Expand Down
20 changes: 10 additions & 10 deletions multi/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func registerPrometheusMetrics(_ context.Context) error {
Subsystem: "multi",
Name: "connections",
Help: "Number of connections",
}, []string{"state"})
}, []string{"name", "state"})
if err := prometheus.Register(connectionsMetric); err != nil {
return errors.Wrap(err, "failed to register connections")
}
Expand All @@ -57,34 +57,34 @@ func registerPrometheusMetrics(_ context.Context) error {
Subsystem: "multi",
Name: "connection_state",
Help: "The state of the client connection (active/inactive)",
}, []string{"server", "state"})
}, []string{"name", "server", "state"})
if err := prometheus.Register(stateMetric); err != nil {
return errors.Wrap(err, "failed to register connection_state")
}

return nil
}

func setProviderStateMetric(_ context.Context, server string, state string) {
func (s *Service) setProviderStateMetric(_ context.Context, server string, state string) {
if stateMetric == nil {
return
}

switch state {
case "active":
stateMetric.WithLabelValues(server, "active").Set(1)
stateMetric.WithLabelValues(server, "inactive").Set(0)
stateMetric.WithLabelValues(s.name, server, "active").Set(1)
stateMetric.WithLabelValues(s.name, server, "inactive").Set(0)
case "inactive":
stateMetric.WithLabelValues(server, "active").Set(0)
stateMetric.WithLabelValues(server, "inactive").Set(1)
stateMetric.WithLabelValues(s.name, server, "active").Set(0)
stateMetric.WithLabelValues(s.name, server, "inactive").Set(1)
}
}

func setConnectionsMetric(_ context.Context, active int, inactive int) {
func (s *Service) setConnectionsMetric(_ context.Context, active int, inactive int) {
if connectionsMetric == nil {
return
}

connectionsMetric.WithLabelValues("active").Set(float64(active))
connectionsMetric.WithLabelValues("inactive").Set(float64(inactive))
connectionsMetric.WithLabelValues(s.name, "active").Set(float64(active))
connectionsMetric.WithLabelValues(s.name, "inactive").Set(float64(inactive))
}
8 changes: 8 additions & 0 deletions multi/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type parameters struct {
extraHeaders map[string]string
enforceJSON bool
allowDelayedStart bool
name string
}

// Parameter is the interface for service parameters.
Expand Down Expand Up @@ -100,6 +101,13 @@ func WithExtraHeaders(headers map[string]string) Parameter {
})
}

// WithName sets the name for the multiclient.
func WithName(name string) Parameter {
return parameterFunc(func(p *parameters) {
p.name = name
})
}

// parseAndCheckParameters parses and checks parameters to ensure that mandatory parameters are present and correct.
func parseAndCheckParameters(params ...Parameter) (*parameters, error) {
parameters := parameters{
Expand Down
17 changes: 12 additions & 5 deletions multi/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (
type Service struct {
log zerolog.Logger

name string

clientsMu sync.RWMutex
activeClients []consensusclient.Service
inactiveClients []consensusclient.Service
Expand Down Expand Up @@ -63,10 +65,8 @@ func New(ctx context.Context, params ...Parameter) (consensusclient.Service, err
switch {
case client.IsSynced():
activeClients = append(activeClients, client)
setProviderStateMetric(ctx, client.Address(), "active")
default:
inactiveClients = append(inactiveClients, client)
setProviderStateMetric(ctx, client.Address(), "inactive")
}
}
for _, address := range parameters.addresses {
Expand All @@ -86,24 +86,31 @@ func New(ctx context.Context, params ...Parameter) (consensusclient.Service, err
switch {
case client.IsSynced():
activeClients = append(activeClients, client)
setProviderStateMetric(ctx, client.Address(), "active")
default:
inactiveClients = append(inactiveClients, client)
setProviderStateMetric(ctx, client.Address(), "inactive")
}
}
if len(activeClients) == 0 && !parameters.allowDelayedStart {
return nil, consensusclient.ErrNotActive
}
log.Trace().Int("active", len(activeClients)).Int("inactive", len(inactiveClients)).Msg("Initial providers")
setConnectionsMetric(ctx, len(activeClients), len(inactiveClients))

s := &Service{
log: log,
name: parameters.name,
activeClients: activeClients,
inactiveClients: inactiveClients,
}

// Set initial metrics.
for _, client := range s.activeClients {
s.setProviderStateMetric(ctx, client.Address(), "active")
}
for _, client := range s.inactiveClients {
s.setProviderStateMetric(ctx, client.Address(), "inactive")
}
s.setConnectionsMetric(ctx, len(activeClients), len(inactiveClients))

// Kick off monitor.
go s.monitor(ctx)

Expand Down

0 comments on commit 3341a4b

Please sign in to comment.