Skip to content

Commit

Permalink
Merge pull request #268 from attestantio/standard-provider
Browse files Browse the repository at this point in the history
Standardise provider format
  • Loading branch information
Bez625 authored Oct 15, 2024
2 parents e6039ab + 1a77653 commit 428035c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
1.9.1:
- ensure that secondary validator registrations take place for all accounts
- reduce the log level of successful sync committee duties
- standardise provider address format across client and strategy operations

1.9.0:
- allow Vouch to start with some consensus nodes unavailable
Expand Down
43 changes: 43 additions & 0 deletions services/metrics/prometheus/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ package prometheus

import (
"errors"
"fmt"
"net/url"
"regexp"
"strings"
"time"

"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -97,6 +101,10 @@ func (s *Service) setupClientMetrics() error {

// ClientOperation registers an operation.
func (s *Service) ClientOperation(provider string, operation string, succeeded bool, duration time.Duration) {
address, err := parseAddress(provider)
if err == nil && address != nil {
provider = address.String()
}
if succeeded {
s.clientOperationCounter.WithLabelValues(provider, operation, "succeeded").Add(1)
s.clientOperationTimer.WithLabelValues(provider, operation).Observe(duration.Seconds())
Expand All @@ -107,6 +115,41 @@ func (s *Service) ClientOperation(provider string, operation string, succeeded b

// StrategyOperation provides a generic monitor for strategy operations.
func (s *Service) StrategyOperation(strategy string, provider string, operation string, duration time.Duration) {
address, err := parseAddress(provider)
if err == nil && address != nil {
provider = address.String()
}
s.strategyOperationCounter.WithLabelValues(strategy, provider, operation).Add(1)
s.strategyOperationTimer.WithLabelValues(strategy, provider, operation).Observe(duration.Seconds())
}

func parseAddress(address string) (*url.URL, error) {
if !strings.HasPrefix(address, "http") {
address = fmt.Sprintf("http://%s", address)
}
base, err := url.Parse(address)
if err != nil {
return nil, errors.Join(errors.New("invalid URL"), err)
}
// Remove any trailing slash from the path.
base.Path = strings.TrimSuffix(base.Path, "/")

// Attempt to mask any sensitive information in the URL, for logging purposes.
baseAddress := *base
if _, pwExists := baseAddress.User.Password(); pwExists {
// Mask the password.
user := baseAddress.User.Username()
baseAddress.User = url.UserPassword(user, "xxxxx")
}
if baseAddress.Path != "" {
// Mask the path.
baseAddress.Path = "xxxxx"
}
if baseAddress.RawQuery != "" {
// Mask all query values.
sensitiveRegex := regexp.MustCompile("=([^&]*)(&)?")
baseAddress.RawQuery = sensitiveRegex.ReplaceAllString(baseAddress.RawQuery, "=xxxxx$2")
}

return &baseAddress, nil
}
27 changes: 27 additions & 0 deletions services/metrics/prometheus/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright © 2021 Attestant Limited.
// 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.

package prometheus

import (
"github.com/stretchr/testify/require"
"testing"
)

func TestParseAddress(t *testing.T) {
provider := "eth-val-d03-01.attestant.io:15100"
url, err := parseAddress(provider)
require.NoError(t, err)
require.Equal(t, "http://eth-val-d03-01.attestant.io:15100", url.String())

}

0 comments on commit 428035c

Please sign in to comment.