@ -1715,14 +1715,8 @@ TEST_F(OpenTelemetryPluginNPCMetricsTest, InstrumentsEnabledTest) {
EXPECT_FALSE ( stats_plugins . IsInstrumentEnabled ( counter_handle ) ) ;
}
class OpenTelemetryPluginCallbackMetricsTest
: public OpenTelemetryPluginEnd2EndTest {
protected :
OpenTelemetryPluginCallbackMetricsTest ( )
: endpoint_config_ ( grpc_core : : ChannelArgs ( ) ) { }
grpc_event_engine : : experimental : : ChannelArgsEndpointConfig endpoint_config_ ;
} ;
using OpenTelemetryPluginCallbackMetricsTest =
OpenTelemetryPluginNPCMetricsTest ;
// The callback minimal interval is longer than the OT reporting interval, so we
// expect to collect duplicated (cached) values.
@ -1981,6 +1975,116 @@ TEST_F(OpenTelemetryPluginCallbackMetricsTest,
kOptionalLabelKeys , kOptionalLabelValuesSet2 , 0.0 , true ) ) ;
}
// Verifies that callbacks are cleaned up when the OpenTelemetry plugin is
// destroyed.
TEST_F ( OpenTelemetryPluginCallbackMetricsTest , VerifyCallbacksAreCleanedUp ) {
constexpr absl : : string_view kInt64CallbackGaugeMetric =
" yet_another_int64_callback_gauge " ;
constexpr absl : : string_view kDoubleCallbackGaugeMetric =
" yet_another_double_callback_gauge " ;
auto integer_gauge_handle =
grpc_core : : GlobalInstrumentsRegistry : : RegisterCallbackInt64Gauge (
kInt64CallbackGaugeMetric , " An int64 callback gauge. " , " unit " ,
/*enable_by_default=*/ true )
. Build ( ) ;
auto double_gauge_handle =
grpc_core : : GlobalInstrumentsRegistry : : RegisterCallbackDoubleGauge (
kDoubleCallbackGaugeMetric , " A double callback gauge. " , " unit " ,
/*enable_by_default=*/ true )
. Build ( ) ;
Init ( std : : move ( Options ( ) . set_metric_names (
{ kInt64CallbackGaugeMetric , kDoubleCallbackGaugeMetric } ) ) ) ;
auto stats_plugins =
grpc_core : : GlobalStatsPluginRegistry : : GetStatsPluginsForChannel (
grpc_core : : experimental : : StatsPluginChannelScope (
" dns:///localhost:8080 " , " " , endpoint_config_ ) ) ;
// Multiple callbacks for the same metrics, each reporting different
// label values.
int report_count_1 = 0 ;
int64_t int_value_1 = 1 ;
double double_value_1 = 0.5 ;
auto registered_metric_callback_1 = stats_plugins . RegisterCallback (
[ & ] ( grpc_core : : CallbackMetricReporter & reporter ) {
+ + report_count_1 ;
reporter . Report ( integer_gauge_handle , int_value_1 + + , { } , { } ) ;
reporter . Report ( double_gauge_handle , double_value_1 + + , { } , { } ) ;
} ,
grpc_core : : Duration : : Milliseconds ( 50 ) * grpc_test_slowdown_factor ( ) ,
integer_gauge_handle , double_gauge_handle ) ;
int report_count_2 = 0 ;
int64_t int_value_2 = 1 ;
double double_value_2 = 0.5 ;
auto registered_metric_callback_2 = stats_plugins . RegisterCallback (
[ & ] ( grpc_core : : CallbackMetricReporter & reporter ) {
+ + report_count_2 ;
reporter . Report ( integer_gauge_handle , int_value_2 + + , { } , { } ) ;
reporter . Report ( double_gauge_handle , double_value_2 + + , { } , { } ) ;
} ,
grpc_core : : Duration : : Milliseconds ( 50 ) * grpc_test_slowdown_factor ( ) ,
integer_gauge_handle , double_gauge_handle ) ;
constexpr int kIterations = 50 ;
{
MetricsCollectorThread collector {
this ,
grpc_core : : Duration : : Milliseconds ( 100 ) * grpc_test_slowdown_factor ( ) ,
kIterations ,
[ & ] ( const absl : : flat_hash_map <
std : : string ,
std : : vector < opentelemetry : : sdk : : metrics : : PointDataAttributes > > &
data ) {
return ! data . contains ( kInt64CallbackGaugeMetric ) | |
! data . contains ( kDoubleCallbackGaugeMetric ) ;
} } ;
}
// Verify that callbacks are invoked
EXPECT_EQ ( report_count_1 , kIterations ) ;
EXPECT_EQ ( report_count_2 , kIterations ) ;
// Remove one of the callbacks
registered_metric_callback_1 . reset ( ) ;
{
MetricsCollectorThread new_collector {
this ,
grpc_core : : Duration : : Milliseconds ( 100 ) * grpc_test_slowdown_factor ( ) ,
kIterations ,
[ & ] ( const absl : : flat_hash_map <
std : : string ,
std : : vector < opentelemetry : : sdk : : metrics : : PointDataAttributes > > &
data ) { return false ; } } ;
}
EXPECT_EQ ( report_count_1 , kIterations ) ; // No change since previous
EXPECT_EQ ( report_count_2 , 2 * kIterations ) ; // Gets another kIterations
// Remove the other callback as well
registered_metric_callback_2 . reset ( ) ;
MetricsCollectorThread new_new_collector {
this ,
grpc_core : : Duration : : Milliseconds ( 100 ) * grpc_test_slowdown_factor ( ) ,
kIterations ,
[ & ] ( const absl : : flat_hash_map <
std : : string ,
std : : vector < opentelemetry : : sdk : : metrics : : PointDataAttributes > > &
data ) { return false ; } } ;
// We shouldn't get any new callbacks
EXPECT_THAT ( new_new_collector . Stop ( ) , : : testing : : IsEmpty ( ) ) ;
EXPECT_EQ ( report_count_1 , kIterations ) ;
EXPECT_EQ ( report_count_2 , 2 * kIterations ) ;
// Reset stats plugins as well
grpc_core : : GlobalStatsPluginRegistryTestPeer : :
ResetGlobalStatsPluginRegistry ( ) ;
registered_metric_callback_2 . reset ( ) ;
MetricsCollectorThread new_new_new_collector {
this ,
grpc_core : : Duration : : Milliseconds ( 100 ) * grpc_test_slowdown_factor ( ) ,
kIterations ,
[ & ] ( const absl : : flat_hash_map <
std : : string ,
std : : vector < opentelemetry : : sdk : : metrics : : PointDataAttributes > > &
data ) { return false ; } } ;
// Still no new callbacks
EXPECT_THAT ( new_new_new_collector . Stop ( ) , : : testing : : IsEmpty ( ) ) ;
EXPECT_EQ ( report_count_1 , kIterations ) ;
EXPECT_EQ ( report_count_2 , 2 * kIterations ) ;
}
TEST ( OpenTelemetryPluginMetricsEnablingDisablingTest , TestEnableDisableAPIs ) {
grpc : : internal : : OpenTelemetryPluginBuilderImpl builder ;
// First disable all metrics