|
26 | 26 | import com.google.common.collect.ImmutableList; |
27 | 27 | import com.google.common.collect.ImmutableSet; |
28 | 28 | import com.google.common.collect.Iterables; |
| 29 | +import com.google.common.net.HostAndPort; |
29 | 30 | import io.grpc.ManagedChannelBuilder; |
30 | 31 | import io.grpc.opentelemetry.GrpcOpenTelemetry; |
31 | 32 | import io.opentelemetry.api.common.AttributeKey; |
@@ -103,28 +104,12 @@ static ChannelConfigurator enableGrpcMetrics( |
103 | 104 | GCPResourceProvider resourceProvider = new GCPResourceProvider(); |
104 | 105 | Attributes detectedAttributes = resourceProvider.getAttributes(); |
105 | 106 |
|
106 | | - @Nullable String detectedProjectId = |
107 | | - detectedAttributes.get(AttributeKey.stringKey("cloud.account.id")); |
108 | | - if (projectId == null && detectedProjectId == null) { |
109 | | - log.warning( |
110 | | - "Unable to determine the Project ID in order to report metrics. No gRPC client metrics" |
111 | | - + " will be reported."); |
| 107 | + String projectIdToUse = |
| 108 | + resolveProjectIdForMetrics(detectedAttributes, projectId, "gRPC client metrics"); |
| 109 | + if (projectIdToUse == null) { |
112 | 110 | return channelConfigurator != null ? channelConfigurator : ChannelConfigurator.identity(); |
113 | 111 | } |
114 | 112 |
|
115 | | - String projectIdToUse = detectedProjectId == null ? projectId : detectedProjectId; |
116 | | - if (!projectIdToUse.equals(projectId)) { |
117 | | - log.warning( |
118 | | - "The Project ID configured for gRPC client metrics is " |
119 | | - + projectIdToUse |
120 | | - + ", but the Project ID of the storage client is " |
121 | | - + projectId |
122 | | - + ". Make sure that the service account in use has the required metric writing role " |
123 | | - + "(roles/monitoring.metricWriter) in the project " |
124 | | - + projectIdToUse |
125 | | - + ", or metrics will not be written."); |
126 | | - } |
127 | | - |
128 | 113 | String metricServiceEndpoint = getCloudMonitoringEndpoint(endpoint, universeDomain); |
129 | 114 | SdkMeterProvider provider = |
130 | 115 | createMeterProvider( |
@@ -206,7 +191,143 @@ static String getCloudMonitoringEndpoint(String endpoint, String universeDomain) |
206 | 191 | metricServiceEndpoint = match; |
207 | 192 | } |
208 | 193 | } |
209 | | - return metricServiceEndpoint + ":" + endpoint.split(":")[1]; |
| 194 | + HostAndPort parsed = parseHostAndPort(endpoint); |
| 195 | + int port = parsed != null ? parsed.getPortOrDefault(443) : 443; |
| 196 | + return metricServiceEndpoint + ":" + port; |
| 197 | + } |
| 198 | + |
| 199 | + @Nullable |
| 200 | + private static HostAndPort parseHostAndPort(@Nullable String host) { |
| 201 | + if (host == null || host.trim().isEmpty()) { |
| 202 | + return null; |
| 203 | + } |
| 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); |
| 215 | + } |
| 216 | + if (s.isEmpty()) { |
| 217 | + return null; |
| 218 | + } |
| 219 | + try { |
| 220 | + return HostAndPort.fromString(s); |
| 221 | + } catch (IllegalArgumentException e) { |
| 222 | + return null; |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + static String formatStorageEndpoint(@Nullable String host) { |
| 227 | + HostAndPort parsed = parseHostAndPort(host); |
| 228 | + if (parsed == null) { |
| 229 | + return "storage.googleapis.com:443"; |
| 230 | + } |
| 231 | + return parsed.withDefaultPort(443).toString(); |
| 232 | + } |
| 233 | + |
| 234 | + @Nullable |
| 235 | + static String extractServerAddress(@Nullable String host) { |
| 236 | + HostAndPort parsed = parseHostAndPort(host); |
| 237 | + return parsed != null ? parsed.getHost() : null; |
| 238 | + } |
| 239 | + |
| 240 | + @Nullable |
| 241 | + private static String resolveProjectIdForMetrics( |
| 242 | + Attributes detectedAttributes, |
| 243 | + @Nullable String configuredProjectId, |
| 244 | + String metricsTypeLabel) { |
| 245 | + @Nullable String detectedProjectId = |
| 246 | + detectedAttributes.get(AttributeKey.stringKey("cloud.account.id")); |
| 247 | + if (detectedProjectId != null && detectedProjectId.trim().isEmpty()) { |
| 248 | + detectedProjectId = null; |
| 249 | + } |
| 250 | + @Nullable String cleanConfiguredProjectId = |
| 251 | + (configuredProjectId != null && !configuredProjectId.trim().isEmpty()) |
| 252 | + ? configuredProjectId.trim() |
| 253 | + : null; |
| 254 | + if (cleanConfiguredProjectId == null && detectedProjectId == null) { |
| 255 | + log.warning( |
| 256 | + "Unable to determine the Project ID in order to report metrics. No " |
| 257 | + + metricsTypeLabel |
| 258 | + + " will be reported."); |
| 259 | + return null; |
| 260 | + } |
| 261 | + String projectIdToUse = |
| 262 | + detectedProjectId != null ? detectedProjectId : cleanConfiguredProjectId; |
| 263 | + if (cleanConfiguredProjectId != null && !projectIdToUse.equals(cleanConfiguredProjectId)) { |
| 264 | + log.warning( |
| 265 | + "The Project ID configured for " |
| 266 | + + metricsTypeLabel |
| 267 | + + " is " |
| 268 | + + projectIdToUse |
| 269 | + + ", but the Project ID of the storage client is " |
| 270 | + + cleanConfiguredProjectId |
| 271 | + + ". Make sure that the service account in use has the required metric writing" |
| 272 | + + " role (roles/monitoring.metricWriter) in the project " |
| 273 | + + projectIdToUse |
| 274 | + + ", or metrics will not be written."); |
| 275 | + } |
| 276 | + return projectIdToUse; |
| 277 | + } |
| 278 | + |
| 279 | + @Nullable |
| 280 | + static SdkMeterProvider createDefaultClientMeterProvider( |
| 281 | + @Nullable String host, |
| 282 | + @Nullable String configuredProjectId, |
| 283 | + @Nullable String universeDomain, |
| 284 | + @Nullable Duration metricInterval, |
| 285 | + String api) { |
| 286 | + try { |
| 287 | + GCPResourceProvider resourceProvider = new GCPResourceProvider(); |
| 288 | + Attributes detectedAttributes = resourceProvider.getAttributes(); |
| 289 | + return createDefaultClientMeterProvider( |
| 290 | + host, configuredProjectId, universeDomain, metricInterval, api, detectedAttributes); |
| 291 | + } catch (Exception e) { |
| 292 | + log.warning("Failed to create default client MeterProvider: " + e.getMessage()); |
| 293 | + return null; |
| 294 | + } |
| 295 | + } |
| 296 | + |
| 297 | + @VisibleForTesting |
| 298 | + @Nullable |
| 299 | + static SdkMeterProvider createDefaultClientMeterProvider( |
| 300 | + @Nullable String host, |
| 301 | + @Nullable String configuredProjectId, |
| 302 | + @Nullable String universeDomain, |
| 303 | + @Nullable Duration metricInterval, |
| 304 | + String api, |
| 305 | + Attributes detectedAttributes) { |
| 306 | + try { |
| 307 | + String projectIdToUse = |
| 308 | + resolveProjectIdForMetrics( |
| 309 | + detectedAttributes, configuredProjectId, "storage client metrics"); |
| 310 | + if (projectIdToUse == null) { |
| 311 | + return null; |
| 312 | + } |
| 313 | + Attributes attributesToUse = |
| 314 | + Attributes.builder() |
| 315 | + .putAll(detectedAttributes) |
| 316 | + .put(AttributeKey.stringKey("cloud.account.id"), projectIdToUse) |
| 317 | + .build(); |
| 318 | + String formattedEndpoint = formatStorageEndpoint(host); |
| 319 | + String metricServiceEndpoint = getCloudMonitoringEndpoint(formattedEndpoint, universeDomain); |
| 320 | + return createClientMeterProvider( |
| 321 | + metricServiceEndpoint, |
| 322 | + projectIdToUse, |
| 323 | + attributesToUse, |
| 324 | + metricInterval, |
| 325 | + /* shouldSuppressExceptions= */ true, |
| 326 | + api); |
| 327 | + } catch (Exception e) { |
| 328 | + log.warning("Failed to create default client MeterProvider: " + e.getMessage()); |
| 329 | + return null; |
| 330 | + } |
210 | 331 | } |
211 | 332 |
|
212 | 333 | static final ImmutableList<String> CLIENT_LATENCY_HISTOGRAMS = |
|
0 commit comments