Skip to content

xds: add MetricsReporter for generic xds client #8274

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
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
10 changes: 10 additions & 0 deletions xds/internal/clients/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,13 @@ type Locality struct {
// SubZone is the further subdivision within a zone.
SubZone string
}

// MetricsReporter is used by the XDSClient to report metrics.
type MetricsReporter interface {
// ReportMetric reports a metric. The metric will be one of the predefined
// set of types depending on the client (XDSClient or LRSClient).
//
// Each client will produce different metrics. Please see the client's
// documentation for a list of possible metrics events.
ReportMetric(metric any)
}
16 changes: 16 additions & 0 deletions xds/internal/clients/xdsclient/authority.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ import (

"google.golang.org/grpc/grpclog"
igrpclog "google.golang.org/grpc/internal/grpclog"
"google.golang.org/grpc/xds/internal/clients"
"google.golang.org/grpc/xds/internal/clients/internal/syncutil"
"google.golang.org/grpc/xds/internal/clients/xdsclient/internal/xdsresource"
"google.golang.org/grpc/xds/internal/clients/xdsclient/metrics"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/timestamppb"

Expand Down Expand Up @@ -87,6 +89,7 @@ type authority struct {
xdsClientSerializerClose func() // Function to close the above serializer.
logger *igrpclog.PrefixLogger // Logger for this authority.
target string // The gRPC Channel target.
metricsReporter clients.MetricsReporter

// The below defined fields must only be accessed in the context of the
// serializer callback, owned by this authority.
Expand Down Expand Up @@ -121,6 +124,7 @@ type authorityBuildOptions struct {
getChannelForADS xdsChannelForADS // Function to acquire a reference to an xdsChannel
logPrefix string // Prefix for logging
target string // Target for the gRPC Channel that owns xDS Client/Authority
metricsReporter clients.MetricsReporter // Metrics reporter for the authority
}

// newAuthority creates a new authority instance with the provided
Expand All @@ -145,6 +149,7 @@ func newAuthority(args authorityBuildOptions) *authority {
logger: igrpclog.NewPrefixLogger(l, logPrefix),
resources: make(map[ResourceType]map[string]*resourceState),
target: args.target,
metricsReporter: args.metricsReporter,
}

// Create an ordered list of xdsChannels with their server configs. The
Expand Down Expand Up @@ -363,6 +368,11 @@ func (a *authority) handleADSResourceUpdate(serverConfig *ServerConfig, rType Re
// On error, keep previous version of the resource. But update status
// and error.
if uErr.Err != nil {
if a.metricsReporter != nil {
a.metricsReporter.ReportMetric(&metrics.ResourceUpdateInvalid{
ServerURI: serverConfig.ServerIdentifier.ServerURI, ResourceType: rType.TypeName,
})
}
state.md.ErrState = md.ErrState
state.md.Status = md.Status
for watcher := range state.watchers {
Expand All @@ -378,6 +388,12 @@ func (a *authority) handleADSResourceUpdate(serverConfig *ServerConfig, rType Re
continue
}

if a.metricsReporter != nil {
a.metricsReporter.ReportMetric(&metrics.ResourceUpdateValid{
ServerURI: serverConfig.ServerIdentifier.ServerURI, ResourceType: rType.TypeName,
})
}

if state.deletionIgnored {
state.deletionIgnored = false
a.logger.Infof("A valid update was received for resource %q of type %q after previously ignoring a deletion", name, rType.TypeName)
Expand Down
42 changes: 42 additions & 0 deletions xds/internal/clients/xdsclient/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
*
* Copyright 2025 gRPC authors.
*
* 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 metrics defines all metrics that can be produced by an xDS client.
// All calls to the MetricsRecorder by the xDS client will contain a struct
// from this package passed by pointer.
package metrics

// ResourceUpdateValid is a metric to report a valid resource update from
// the xDS management server for a given resource type.
type ResourceUpdateValid struct {
ServerURI string
ResourceType string
}

// ResourceUpdateInvalid is a metric to report an invalid resource update
// from the xDS management server for a given resource type.
type ResourceUpdateInvalid struct {
ServerURI string
ResourceType string
}

// ServerFailure is a metric to report a server failure of the xDS
// management server.
type ServerFailure struct {
ServerURI string
}
36 changes: 36 additions & 0 deletions xds/internal/clients/xdsclient/test/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package xdsclient_test

import (
"bytes"
"context"
"errors"
"fmt"
"strconv"
Expand All @@ -29,12 +30,14 @@ import (

"google.golang.org/grpc/internal/grpctest"
"google.golang.org/grpc/xds/internal/clients/internal/pretty"
"google.golang.org/grpc/xds/internal/clients/internal/testutils"
"google.golang.org/grpc/xds/internal/clients/xdsclient"
"google.golang.org/grpc/xds/internal/clients/xdsclient/internal/xdsresource"
"google.golang.org/protobuf/proto"

v3listenerpb "github.com/envoyproxy/go-control-plane/envoy/config/listener/v3"
v3httppb "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/network/http_connection_manager/v3"
"github.com/google/go-cmp/cmp"
)

type s struct {
Expand Down Expand Up @@ -262,3 +265,36 @@ func buildResourceName(typeName, auth, id string, ctxParams map[string]string) s
ContextParams: ctxParams,
}).String()
}

// testMetricsReporter is a MetricsReporter to be used in tests. It sends
// recording events on channels and provides helpers to check if certain events
// have taken place.
type testMetricsReporter struct {
metricsCh *testutils.Channel
}

// newTestMetricsReporter returns a new testMetricsReporter.
func newTestMetricsReporter() *testMetricsReporter {
return &testMetricsReporter{
metricsCh: testutils.NewChannelWithSize(1),
}
}

// waitForMetric waits for a metric to be recorded and verifies that the
// recorded metrics data matches the expected metricsDataWant. Returns
// an error if failed to wait or received wrong data.
func (r *testMetricsReporter) waitForMetric(ctx context.Context, metricsDataWant any) error {
got, err := r.metricsCh.Receive(ctx)
if err != nil {
return fmt.Errorf("timeout waiting for int64Count")
}
if diff := cmp.Diff(got, metricsDataWant); diff != "" {
return fmt.Errorf("received unexpected metrics value (-got, +want): %v", diff)
}
return nil
}

// ReportMetric sends the metrics data to the metricsCh channel.
func (r *testMetricsReporter) ReportMetric(m any) {
r.metricsCh.Send(m)
}
Loading