3333import io .opentelemetry .api .common .Attributes ;
3434import io .opentelemetry .api .common .AttributesBuilder ;
3535import io .opentelemetry .api .internal .StringUtils ;
36+ import io .opentelemetry .api .metrics .MeterProvider ;
3637import io .opentelemetry .contrib .gcp .resource .GCPResourceProvider ;
3738import io .opentelemetry .sdk .OpenTelemetrySdk ;
3839import io .opentelemetry .sdk .common .CompletableResultCode ;
6162import java .util .Objects ;
6263import java .util .UUID ;
6364import java .util .concurrent .atomic .AtomicBoolean ;
65+ import java .util .function .Supplier ;
6466import java .util .logging .Logger ;
6567import java .util .stream .Collectors ;
6668import org .checkerframework .checker .nullness .qual .NonNull ;
6769import org .checkerframework .checker .nullness .qual .Nullable ;
6870
6971final class OpenTelemetryBootstrappingUtils {
72+ static final String CHANNEL_CREATION_LATENCY_INSTRUMENT =
73+ "internal/client/channel_creation_latency" ;
74+ static final String CHANNEL_CREATION_LATENCY_VIEW_NAME =
75+ "storage.googleapis.com/internal/client/channel_creation_latency" ;
76+ static final String CHANNEL_CREATION_LATENCY_DESCRIPTION =
77+ "Time from client construction until the first gRPC channel became ready." ;
78+ static final String CHANNEL_CREATION_LATENCY_UNIT = "s" ;
79+
80+ private static final String C2P_PREFIX = "google-c2p:///" ;
81+ private static final String C2P_EXPERIMENTAL_PREFIX = "google-c2p-experimental:///" ;
82+ private static final String DIRECT_PATH_INTERCONNECT_HOST = "storage-direct.googleapis.com" ;
83+ private static final String DIRECT_PATH_INTERCONNECT_PREPROD_HOST =
84+ "storage-preprod-direct.sandbox.googleapis.com" ;
85+
7086 private static final Collection <String > METRICS_TO_ENABLE =
7187 ImmutableList .of (
7288 "grpc.lb.wrr.rr_fallback" ,
@@ -128,7 +144,30 @@ static ChannelConfigurator enableGrpcMetrics(
128144 grpcOpenTelemetry .configureChannelBuilder (b );
129145 return b ;
130146 };
131- return otelConfigurator .andThen (channelConfigurator );
147+ return new GrpcMetricsChannelConfigurator (
148+ otelConfigurator .andThen (channelConfigurator ), provider );
149+ }
150+
151+ @ SuppressWarnings ("rawtypes" ) // ManagedChannelBuilder
152+ static final class GrpcMetricsChannelConfigurator
153+ implements ChannelConfigurator , Supplier <MeterProvider > {
154+ private final ChannelConfigurator delegate ;
155+ private final MeterProvider meterProvider ;
156+
157+ GrpcMetricsChannelConfigurator (ChannelConfigurator delegate , MeterProvider meterProvider ) {
158+ this .delegate = delegate ;
159+ this .meterProvider = meterProvider ;
160+ }
161+
162+ @ Override
163+ public ManagedChannelBuilder apply (ManagedChannelBuilder input ) {
164+ return delegate .apply (input );
165+ }
166+
167+ @ Override
168+ public MeterProvider get () {
169+ return meterProvider ;
170+ }
132171 }
133172
134173 @ SuppressWarnings ("rawtypes" ) // ManagedChannelBuilder
@@ -168,13 +207,21 @@ public ManagedChannelBuilder apply(ManagedChannelBuilder input) {
168207 }
169208
170209 @ VisibleForTesting
171- static String getCloudMonitoringEndpoint (String endpoint , String universeDomain ) {
210+ static String getCloudMonitoringEndpoint (
211+ @ Nullable String endpoint , @ Nullable String universeDomain ) {
212+ if (endpoint == null || endpoint .isEmpty ()) {
213+ return "monitoring.googleapis.com:443" ;
214+ }
172215 String metricServiceEndpoint = "monitoring.googleapis.com" ;
216+ HostAndPort parsed = parseHostAndPort (endpoint );
217+ String host = parsed != null ? parsed .getHost () : extractHostFallback (endpoint );
218+ int port = parsed != null ? parsed .getPortOrDefault (443 ) : 443 ;
173219
174- // use contains instead of equals because endpoint has a port in it
175- if (universeDomain != null && endpoint .contains ("storage." + universeDomain )) {
220+ if (universeDomain != null
221+ && (host .equals ("storage." + universeDomain )
222+ || host .equals ("storage-direct." + universeDomain ))) {
176223 metricServiceEndpoint = "monitoring." + universeDomain ;
177- } else if (!endpoint . contains ("storage.googleapis.com" )) {
224+ } else if (!host . equals ("storage.googleapis.com" ) && ! isDirectPathInterconnectHost ( host )) {
178225 String canonicalEndpoint = "storage.googleapis.com" ;
179226 String privateEndpoint = "private.googleapis.com" ;
180227 String restrictedEndpoint = "restricted.googleapis.com" ;
@@ -183,35 +230,66 @@ static String getCloudMonitoringEndpoint(String endpoint, String universeDomain)
183230 privateEndpoint = "private." + universeDomain ;
184231 restrictedEndpoint = "restricted." + universeDomain ;
185232 }
233+ final String resolvedHost = host ;
186234 String match =
187235 ImmutableList .of (canonicalEndpoint , privateEndpoint , restrictedEndpoint ).stream ()
188- .filter (s -> endpoint .contains (s ) || endpoint .contains ("google-c2p:///" + s ))
236+ .filter (s -> resolvedHost .contains (s ) || endpoint .contains (C2P_PREFIX + s ))
189237 .collect (Collectors .joining ());
190238 if (!StringUtils .isNullOrEmpty (match )) {
191239 metricServiceEndpoint = match ;
192240 }
193241 }
194- HostAndPort parsed = parseHostAndPort (endpoint );
195- int port = parsed != null ? parsed .getPortOrDefault (443 ) : 443 ;
196242 return metricServiceEndpoint + ":" + port ;
197243 }
198244
245+ private static boolean isDirectPathInterconnectHost (String host ) {
246+ return DIRECT_PATH_INTERCONNECT_HOST .equals (host )
247+ || DIRECT_PATH_INTERCONNECT_PREPROD_HOST .equals (host );
248+ }
249+
250+ private static String extractHostFallback (String endpoint ) {
251+ String s = stripEndpointScheme (endpoint .trim ());
252+ int end = indexOfPathQueryOrFragment (s );
253+ String hostPort = end >= 0 ? s .substring (0 , end ) : s ;
254+ int colon = hostPort .lastIndexOf (':' );
255+ return colon >= 0 ? hostPort .substring (0 , colon ) : hostPort ;
256+ }
257+
258+ private static String stripEndpointScheme (String s ) {
259+ if (s .startsWith (C2P_PREFIX )) {
260+ return s .substring (C2P_PREFIX .length ());
261+ }
262+ if (s .startsWith (C2P_EXPERIMENTAL_PREFIX )) {
263+ return s .substring (C2P_EXPERIMENTAL_PREFIX .length ());
264+ }
265+ if (s .startsWith ("https://" )) {
266+ return s .substring ("https://" .length ());
267+ }
268+ if (s .startsWith ("http://" )) {
269+ return s .substring ("http://" .length ());
270+ }
271+ return s ;
272+ }
273+
274+ private static int indexOfPathQueryOrFragment (String s ) {
275+ for (int i = 0 ; i < s .length (); i ++) {
276+ char c = s .charAt (i );
277+ if (c == '/' || c == '?' || c == '#' ) {
278+ return i ;
279+ }
280+ }
281+ return -1 ;
282+ }
283+
199284 @ Nullable
200285 private static HostAndPort parseHostAndPort (@ Nullable String host ) {
201286 if (host == null || host .trim ().isEmpty ()) {
202287 return null ;
203288 }
204- String s = host .trim ();
205- if (s .startsWith ("google-c2p:///" )) {
206- s = s .substring ("google-c2p:///" .length ());
207- } else if (s .startsWith ("https://" )) {
208- s = s .substring ("https://" .length ());
209- } else if (s .startsWith ("http://" )) {
210- s = s .substring ("http://" .length ());
211- }
212- int slashIdx = s .indexOf ('/' );
213- if (slashIdx >= 0 ) {
214- s = s .substring (0 , slashIdx );
289+ String s = stripEndpointScheme (host .trim ());
290+ int endIdx = indexOfPathQueryOrFragment (s );
291+ if (endIdx >= 0 ) {
292+ s = s .substring (0 , endIdx );
215293 }
216294 if (s .isEmpty ()) {
217295 return null ;
@@ -415,6 +493,7 @@ static SdkMeterProvider createMeterProvider(
415493 "By" ,
416494 "A view of grpc/client/attempt/sent_total_compressed_message_size with histogram boundaries"
417495 + " more appropriate for Google Cloud Storage RPCs" );
496+ registerGrpcChannelViews (providerBuilder );
418497
419498 return buildMeterProvider (
420499 cloudMonitoringExporter ,
@@ -569,6 +648,19 @@ private static SdkMeterProvider buildMeterProvider(
569648 return providerBuilder .build ();
570649 }
571650
651+ @ VisibleForTesting
652+ static SdkMeterProviderBuilder registerGrpcChannelViews (SdkMeterProviderBuilder providerBuilder ) {
653+ addHistogramView (
654+ providerBuilder ,
655+ latencyHistogramBoundaries (),
656+ CHANNEL_CREATION_LATENCY_INSTRUMENT ,
657+ CHANNEL_CREATION_LATENCY_VIEW_NAME ,
658+ StorageClientMetrics .METER_NAME ,
659+ CHANNEL_CREATION_LATENCY_UNIT ,
660+ CHANNEL_CREATION_LATENCY_DESCRIPTION );
661+ return providerBuilder ;
662+ }
663+
572664 @ VisibleForTesting
573665 static SdkMeterProviderBuilder registerClientViews (SdkMeterProviderBuilder providerBuilder ) {
574666 for (String metric : CLIENT_COUNTERS ) {
0 commit comments