Skip to content
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

feat: emit metrics when serving p2p registry API #53

Merged
merged 1 commit into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions pkg/discovery/content/registry/mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

pcontext "github.com/azure/peerd/pkg/context"
"github.com/azure/peerd/pkg/discovery/routing"
"github.com/azure/peerd/pkg/metrics"
"github.com/azure/peerd/pkg/peernet"
)

Expand All @@ -30,7 +31,8 @@
router routing.Router
resolveRetries int

n peernet.Network
n peernet.Network
metricsRecorder metrics.Metrics
}

// Handle handles a request to this registry mirror.
Expand All @@ -56,6 +58,8 @@
c.AbortWithError(http.StatusInternalServerError, errors.New("neither digest nor reference provided"))
}

startTime := time.Now()
peerCount := 0
peersChan, err := m.router.Resolve(resolveCtx, key, false, m.resolveRetries)
if err != nil {
//nolint
Expand All @@ -79,6 +83,12 @@
return
}

if peerCount == 0 {
// Only report the time it took to discover the first peer.
m.metricsRecorder.RecordPeerDiscovery(peer.HttpHost, time.Since(startTime).Seconds())
peerCount++
}

succeeded := false
u, err := url.Parse(peer.HttpHost)
if err != nil {
Expand All @@ -94,12 +104,16 @@
r.URL.RawQuery = c.Request.URL.RawQuery
pcontext.SetOutboundHeaders(r, c)
}

count := int64(0)

proxy.ModifyResponse = func(resp *http.Response) error {
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("expected peer to respond with 200, got: %s", resp.Status)
}

succeeded = true
count = resp.ContentLength
return nil
}
proxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) {
Expand All @@ -112,18 +126,20 @@
break
}

l.Info().Str("peer", u.Host).Msg("request served from peer")
m.metricsRecorder.RecordPeerResponse(peer.HttpHost, key, "pull", time.Since(startTime).Seconds(), count)
l.Info().Str("peer", u.Host).Int64("count", count).Msg("request served from peer")
return
}
}
}

// New creates a new mirror handler.
func New(router routing.Router) *Mirror {
func New(ctx context.Context, router routing.Router) *Mirror {

Check warning on line 137 in pkg/discovery/content/registry/mirror.go

View check run for this annotation

Codecov / codecov/patch

pkg/discovery/content/registry/mirror.go#L137

Added line #L137 was not covered by tests
return &Mirror{
resolveTimeout: ResolveTimeout,
router: router,
resolveRetries: ResolveRetries,
n: router.Net(),
metricsRecorder: metrics.FromContext(ctx),
resolveTimeout: ResolveTimeout,
router: router,
resolveRetries: ResolveRetries,
n: router.Net(),

Check warning on line 143 in pkg/discovery/content/registry/mirror.go

View check run for this annotation

Codecov / codecov/patch

pkg/discovery/content/registry/mirror.go#L139-L143

Added lines #L139 - L143 were not covered by tests
}
}
11 changes: 7 additions & 4 deletions pkg/discovery/content/registry/mirror_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import (

pcontext "github.com/azure/peerd/pkg/context"
"github.com/azure/peerd/pkg/discovery/routing/mocks"
"github.com/azure/peerd/pkg/metrics"
"github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -64,10 +66,11 @@ func TestMirrorHandler(t *testing.T) {
}
router := mocks.NewMockRouter(resolver)
m := &Mirror{
router: router,
resolveRetries: ResolveRetries,
resolveTimeout: ResolveTimeout,
n: router.Net(),
metricsRecorder: metrics.NewPromMetrics(prometheus.DefaultRegisterer, "test", "test"),
router: router,
resolveRetries: ResolveRetries,
resolveTimeout: ResolveTimeout,
n: router.Net(),
}

tests := []struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/handlers/v2/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (h *V2Handler) fill(c pcontext.Context) error {
// New creates a new OCI content handler.
func New(ctx context.Context, router routing.Router, containerdStore containerd.Store) (*V2Handler, error) {
return &V2Handler{
proxy: registry.New(router),
proxy: registry.New(ctx, router),
registry: containerd.NewRegistry(containerdStore),
metricsRecorder: metrics.FromContext(ctx),
}, nil
Expand Down
Loading