-
Notifications
You must be signed in to change notification settings - Fork 72
[Feature] Exporter - network mesh check #823
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
12c31e9
TG-153 Exporter - network mesh check
jwierzbo cd82636
TG-153 Exporter - use signal to graceful monitor shutdown
jwierzbo eb5f4b5
fix linter issues
informalict f376c39
TG-153 Exporter - fix linter errors
jwierzbo 9b616fa
TG-153 Exporter - fix linter errors - macOS
jwierzbo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| // | ||
| // DISCLAIMER | ||
| // | ||
| // Copyright 2016-2021 ArangoDB GmbH, Cologne, Germany | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // | ||
| // Copyright holder is ArangoDB GmbH, Cologne, Germany | ||
| // | ||
| // Author Jakub Wierzbowski | ||
| // | ||
|
|
||
| package exporter | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "io/ioutil" | ||
| "net/url" | ||
| "os" | ||
| "path" | ||
| "strings" | ||
| "sync/atomic" | ||
| "time" | ||
|
|
||
| "github.com/arangodb/go-driver" | ||
| "github.com/arangodb/kube-arangodb/pkg/util/k8sutil" | ||
|
|
||
| "github.com/rs/zerolog/log" | ||
| ) | ||
|
|
||
| const ( | ||
| monitorMetricTemplate = "arangodb_member_health{role=\"%s\",id=\"%s\"} %d \n" | ||
| successRefreshInterval = time.Second * 120 | ||
| failRefreshInterval = time.Second * 15 | ||
| ) | ||
|
|
||
| var currentMembersStatus atomic.Value | ||
|
|
||
| func NewMonitor(arangodbEndpoint string, auth Authentication, sslVerify bool, timeout time.Duration) *monitor { | ||
informalict marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| uri, err := setPath(arangodbEndpoint, k8sutil.ArangoExporterClusterHealthEndpoint) | ||
| if err != nil { | ||
| log.Error().Err(err).Msgf("Fatal") | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| return &monitor{ | ||
| factory: newHttpClientFactory(arangodbEndpoint, auth, sslVerify, timeout), | ||
| healthURI: uri, | ||
| } | ||
| } | ||
|
|
||
| type monitor struct { | ||
| factory httpClientFactory | ||
| healthURI *url.URL | ||
| } | ||
|
|
||
| // UpdateMonitorStatus load monitor metrics for current cluster into currentMembersStatus | ||
| func (m monitor) UpdateMonitorStatus(ctx context.Context) { | ||
| for { | ||
| sleep := successRefreshInterval | ||
|
|
||
| health, err := m.GetClusterHealth() | ||
| if err != nil { | ||
| log.Error().Err(err).Msg("GetClusterHealth error") | ||
| sleep = failRefreshInterval | ||
| } else { | ||
| var output strings.Builder | ||
| for key, value := range health.Health { | ||
| entry, err := m.GetMemberStatus(key, value) | ||
| if err != nil { | ||
| log.Error().Err(err).Msg("GetMemberStatus error") | ||
| sleep = failRefreshInterval | ||
| } | ||
| output.WriteString(entry) | ||
| } | ||
| currentMembersStatus.Store(output.String()) | ||
| } | ||
|
|
||
| select { | ||
| case <-ctx.Done(): | ||
| return | ||
| case <-time.After(sleep): | ||
| continue | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // GetClusterHealth returns current ArangoDeployment cluster health status | ||
| func (m monitor) GetClusterHealth() (*driver.ClusterHealth, error) { | ||
| c, req, err := m.factory() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| req.URL = m.healthURI | ||
| resp, err := c.Do(req) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| defer resp.Body.Close() | ||
| body, err := ioutil.ReadAll(resp.Body) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| var result driver.ClusterHealth | ||
| if err := json.Unmarshal(body, &result); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return &result, err | ||
| } | ||
|
|
||
| // GetMemberStatus returns Prometheus monitor metric for specific member | ||
| func (m monitor) GetMemberStatus(id driver.ServerID, member driver.ServerHealth) (string, error) { | ||
| result := fmt.Sprintf(monitorMetricTemplate, member.Role, id, 0) | ||
|
|
||
| c, req, err := m.factory() | ||
| if err != nil { | ||
| return result, err | ||
| } | ||
|
|
||
| req.URL, err = setPath(member.Endpoint, k8sutil.ArangoExporterStatusEndpoint) | ||
| if err != nil { | ||
| return result, err | ||
| } | ||
|
|
||
| resp, err := c.Do(req) | ||
| if err != nil { | ||
| return result, err | ||
| } | ||
|
|
||
| if resp.StatusCode != 200 { | ||
| defer resp.Body.Close() | ||
| body, err := ioutil.ReadAll(resp.Body) | ||
| if err != nil { | ||
| return result, err | ||
| } | ||
| return result, errors.New(string(body)) | ||
| } | ||
| return fmt.Sprintf(monitorMetricTemplate, member.Role, id, 1), nil | ||
| } | ||
|
|
||
| func setPath(uri, uriPath string) (*url.URL, error) { | ||
| u, err := url.Parse(uri) | ||
| if err != nil { | ||
| return u, err | ||
| } | ||
| u.Path = path.Join(uriPath) | ||
| u.Scheme = "https" | ||
| return u, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| // | ||
| // DISCLAIMER | ||
| // | ||
| // Copyright 2016-2021 ArangoDB GmbH, Cologne, Germany | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // | ||
| // Copyright holder is ArangoDB GmbH, Cologne, Germany | ||
| // | ||
| // Author Jakub Wierzbowski | ||
| // | ||
|
|
||
| package util | ||
|
|
||
| import ( | ||
| "context" | ||
| "os" | ||
| "os/signal" | ||
| "syscall" | ||
| ) | ||
|
|
||
| // CreateSignalContext creates and returns the context which is closed when one of the provided signal occurs. | ||
| // If the provided list of signals is empty, then SIGINT and SIGTERM is used by default. | ||
| func CreateSignalContext(ctx context.Context, signals ...os.Signal) context.Context { | ||
| if ctx == nil { | ||
| ctx = context.Background() | ||
| } | ||
| ctxSignal, cancelSignal := context.WithCancel(ctx) | ||
| sigChannel := make(chan os.Signal, 2) | ||
|
|
||
| if len(signals) > 0 { | ||
| signal.Notify(sigChannel, signals...) | ||
| } else { | ||
| signal.Notify(sigChannel, os.Interrupt, syscall.SIGTERM) | ||
| } | ||
|
|
||
| go func() { | ||
| // Wait until signal occurs. | ||
| <-sigChannel | ||
| close(sigChannel) | ||
| // Close the context which is used by the caller. | ||
| cancelSignal() | ||
| }() | ||
|
|
||
| return ctxSignal | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.