Skip to content

Commit

Permalink
CIRC-9964: Implement deny hosts configuration item (#99)
Browse files Browse the repository at this point in the history
* CIRC-9964: Implement deny hosts configuration item

* CIRC-9964: Additional place to check deny hosts list

* CIRC-9964: Check servers during new client
  • Loading branch information
dhaifley committed Mar 9, 2023
1 parent f2ff324 commit d0d5164
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
6 changes: 6 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ to [Semantic Versioning](http://semver.org/) rules.

## [Next Release]

## [v1.13.2] - 2023-03-09

* add: Adds a configuration item, DenyHosts, to allow a list of hosts to
always be considered inactive, regardless of topology discovery.

## [v1.13.1] - 2023-02-23

* upd: Modifies the PromQLResponse and PromQLError types to support more types
Expand Down Expand Up @@ -460,6 +465,7 @@ writing to histogram endpoints.
any delay, once started. Created: 2019-03-12. Fixed: 2019-03-13.

[Next Release]: https://github.com/circonus-labs/gosnowth
[v1.13.2]: https://github.com/circonus-labs/gosnowth/releases/tag/v1.13.2
[v1.13.1]: https://github.com/circonus-labs/gosnowth/releases/tag/v1.13.1
[v1.13.0]: https://github.com/circonus-labs/gosnowth/releases/tag/v1.13.0
[v1.12.5]: https://github.com/circonus-labs/gosnowth/releases/tag/v1.12.5
Expand Down
50 changes: 45 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type Config struct {
Retries int64 `json:"retries,omitempty"`
ConnectRetries int64 `json:"connect_retries,omitempty"`
Servers []string `json:"servers,omitempty"`
DenyHosts []string `json:"deny_hosts,omitempty"`
CtxKeyTraceID interface{} `json:"-"`
}

Expand Down Expand Up @@ -109,6 +110,9 @@ type SnowthClient struct {
activeNodes []*SnowthNode
inactiveNodes []*SnowthNode

// denyHosts are a list of hosts to always keep inactive.
denyHosts []string

// watchInterval is the duration between checks to tell if a node is active
// or inactive.
watchInterval time.Duration
Expand Down Expand Up @@ -181,6 +185,7 @@ func NewClient(ctx context.Context, cfg *Config,
connRetries: cfg.ConnectRetries,
dumpRequests: os.Getenv("GOSNOWTH_DUMP_REQUESTS"),
traceRequests: os.Getenv("GOSNOWTH_TRACE_REQUESTS"),
denyHosts: cfg.DenyHosts,
ctxKeyTraceID: cfg.CtxKeyTraceID,
}

Expand Down Expand Up @@ -217,6 +222,15 @@ func NewClient(ctx context.Context, cfg *Config,
return
}

for _, dh := range cfg.DenyHosts {
if url.Host == dh {
errCh <- fmt.Errorf("deny host found in servers: %s",
url.Host)

return
}
}

node := &SnowthNode{url: url}

stats, err := sc.GetStatsNodeContext(ctx, node)
Expand Down Expand Up @@ -423,6 +437,19 @@ func (sc *SnowthClient) FindMetricNodeIDs(uuid, metric string) []string {
func (sc *SnowthClient) isNodeActive(ctx context.Context,
node *SnowthNode,
) bool {
sc.RLock()
dhosts := sc.denyHosts
sc.RUnlock()

for _, dh := range dhosts {
if node.GetURL().Host == dh {
sc.LogWarnf("deny host from active node check: %s",
node.GetURL().Host)

return false
}
}

if node.identifier == "" || node.semVer == "" {
// go get state to figure out identity
stats, err := sc.GetStatsNodeContext(ctx, node)
Expand Down Expand Up @@ -598,6 +625,8 @@ func (sc *SnowthClient) populateNodeInfo(ctx context.Context, hash string,
sc.currentTopologyCompiled = nil
}

dhosts := sc.denyHosts

sc.Unlock()

if !found {
Expand All @@ -611,17 +640,28 @@ func (sc *SnowthClient) populateNodeInfo(ctx context.Context, hash string,
currentTopology: hash,
}

for _, dh := range dhosts {
if newNode.GetURL().Host == dh {
sc.LogWarnf("deny host found from topology: %s",
newNode.GetURL().Host)

return
}
}

stats, err := sc.GetStatsNodeContext(ctx, newNode)
if err != nil {
// This node is not returning stats, put it on the inactive list.
sc.AddNodes(newNode)
} else {
newNode.identifier = stats.Identity()
newNode.semVer = stats.SemVer()

sc.AddNodes(newNode)
sc.ActivateNodes(newNode)
return
}

newNode.identifier = stats.Identity()
newNode.semVer = stats.SemVer()

sc.AddNodes(newNode)
sc.ActivateNodes(newNode)
}
}

Expand Down

0 comments on commit d0d5164

Please sign in to comment.