Skip to content

Commit

Permalink
Merge pull request #260 from attestantio/standardise_provider
Browse files Browse the repository at this point in the history
Standardise provider format for client operations
  • Loading branch information
Bez625 authored Oct 10, 2024
2 parents e824a6a + af742b5 commit c0eef33
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
dev:
- use multisign for sync committee messages with enabled accounts
- use multisign for sync committee selections with enabled accounts
- use multisign for contributions and proofs with enabled accounts
- use multisign for signing slot selections with enabled accounts
- 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 c0eef33

Please sign in to comment.