|
| 1 | +/* |
| 2 | + * Copyright 2021 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.google.cloud.spanner.spi.v1; |
| 17 | + |
| 18 | +import static com.google.cloud.spanner.spi.v1.SpannerRpcViews.DATABASE_ID; |
| 19 | +import static com.google.cloud.spanner.spi.v1.SpannerRpcViews.INSTANCE_ID; |
| 20 | +import static com.google.cloud.spanner.spi.v1.SpannerRpcViews.METHOD; |
| 21 | +import static com.google.cloud.spanner.spi.v1.SpannerRpcViews.PROJECT_ID; |
| 22 | +import static com.google.cloud.spanner.spi.v1.SpannerRpcViews.SPANNER_GFE_HEADER_MISSING_COUNT; |
| 23 | +import static com.google.cloud.spanner.spi.v1.SpannerRpcViews.SPANNER_GFE_LATENCY; |
| 24 | + |
| 25 | +import io.grpc.CallOptions; |
| 26 | +import io.grpc.Channel; |
| 27 | +import io.grpc.ClientCall; |
| 28 | +import io.grpc.ClientInterceptor; |
| 29 | +import io.grpc.ForwardingClientCall.SimpleForwardingClientCall; |
| 30 | +import io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener; |
| 31 | +import io.grpc.Metadata; |
| 32 | +import io.grpc.MethodDescriptor; |
| 33 | +import io.opencensus.stats.MeasureMap; |
| 34 | +import io.opencensus.stats.Stats; |
| 35 | +import io.opencensus.stats.StatsRecorder; |
| 36 | +import io.opencensus.tags.TagContext; |
| 37 | +import io.opencensus.tags.TagValue; |
| 38 | +import io.opencensus.tags.Tagger; |
| 39 | +import io.opencensus.tags.Tags; |
| 40 | +import java.util.logging.Level; |
| 41 | +import java.util.logging.Logger; |
| 42 | +import java.util.regex.Matcher; |
| 43 | +import java.util.regex.Pattern; |
| 44 | + |
| 45 | +/** |
| 46 | + * Intercepts all gRPC calls to extract server-timing header. Captures GFE Latency and GFE Header |
| 47 | + * Missing count metrics. |
| 48 | + */ |
| 49 | +class HeaderInterceptor implements ClientInterceptor { |
| 50 | + |
| 51 | + private static final Metadata.Key<String> SERVER_TIMING_HEADER_KEY = |
| 52 | + Metadata.Key.of("server-timing", Metadata.ASCII_STRING_MARSHALLER); |
| 53 | + private static final Pattern SERVER_TIMING_HEADER_PATTERN = Pattern.compile(".*dur=(?<dur>\\d+)"); |
| 54 | + private static final Metadata.Key<String> GOOGLE_CLOUD_RESOURCE_PREFIX_KEY = |
| 55 | + Metadata.Key.of("google-cloud-resource-prefix", Metadata.ASCII_STRING_MARSHALLER); |
| 56 | + private static final Pattern GOOGLE_CLOUD_RESOURCE_PREFIX_PATTERN = |
| 57 | + Pattern.compile( |
| 58 | + ".*projects/(?<project>\\p{ASCII}[^/]*)(/instances/(?<instance>\\p{ASCII}[^/]*))?(/databases/(?<database>\\p{ASCII}[^/]*))?"); |
| 59 | + |
| 60 | + // Get the global singleton Tagger object. |
| 61 | + private static final Tagger TAGGER = Tags.getTagger(); |
| 62 | + private static final StatsRecorder STATS_RECORDER = Stats.getStatsRecorder(); |
| 63 | + |
| 64 | + private static final Logger LOGGER = Logger.getLogger(HeaderInterceptor.class.getName()); |
| 65 | + private static final Level LEVEL = Level.INFO; |
| 66 | + |
| 67 | + HeaderInterceptor() {} |
| 68 | + |
| 69 | + @Override |
| 70 | + public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall( |
| 71 | + MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) { |
| 72 | + return new SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) { |
| 73 | + @Override |
| 74 | + public void start(Listener<RespT> responseListener, Metadata headers) { |
| 75 | + TagContext tagContext = getTagContext(headers, method.getFullMethodName()); |
| 76 | + super.start( |
| 77 | + new SimpleForwardingClientCallListener<RespT>(responseListener) { |
| 78 | + @Override |
| 79 | + public void onHeaders(Metadata metadata) { |
| 80 | + |
| 81 | + processHeader(metadata, tagContext); |
| 82 | + super.onHeaders(metadata); |
| 83 | + } |
| 84 | + }, |
| 85 | + headers); |
| 86 | + } |
| 87 | + }; |
| 88 | + } |
| 89 | + |
| 90 | + private void processHeader(Metadata metadata, TagContext tagContext) { |
| 91 | + MeasureMap measureMap = STATS_RECORDER.newMeasureMap(); |
| 92 | + if (metadata.get(SERVER_TIMING_HEADER_KEY) != null) { |
| 93 | + String serverTiming = metadata.get(SERVER_TIMING_HEADER_KEY); |
| 94 | + Matcher matcher = SERVER_TIMING_HEADER_PATTERN.matcher(serverTiming); |
| 95 | + if (matcher.find()) { |
| 96 | + try { |
| 97 | + long latency = Long.parseLong(matcher.group("dur")); |
| 98 | + measureMap.put(SPANNER_GFE_LATENCY, latency).record(tagContext); |
| 99 | + measureMap.put(SPANNER_GFE_HEADER_MISSING_COUNT, 0L).record(tagContext); |
| 100 | + } catch (NumberFormatException e) { |
| 101 | + LOGGER.log(LEVEL, "Invalid server-timing object in header", matcher.group("dur")); |
| 102 | + } |
| 103 | + } |
| 104 | + } else { |
| 105 | + measureMap.put(SPANNER_GFE_HEADER_MISSING_COUNT, 1L).record(tagContext); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + private TagContext getTagContext( |
| 110 | + String method, String projectId, String instanceId, String databaseId) { |
| 111 | + return TAGGER |
| 112 | + .currentBuilder() |
| 113 | + .putLocal(PROJECT_ID, TagValue.create(projectId)) |
| 114 | + .putLocal(INSTANCE_ID, TagValue.create(instanceId)) |
| 115 | + .putLocal(DATABASE_ID, TagValue.create(databaseId)) |
| 116 | + .putLocal(METHOD, TagValue.create(method)) |
| 117 | + .build(); |
| 118 | + } |
| 119 | + |
| 120 | + private TagContext getTagContext(Metadata headers, String method) { |
| 121 | + String projectId = "undefined-project"; |
| 122 | + String instanceId = "undefined-database"; |
| 123 | + String databaseId = "undefined-database"; |
| 124 | + if (headers.get(GOOGLE_CLOUD_RESOURCE_PREFIX_KEY) != null) { |
| 125 | + String googleResourcePrefix = headers.get(GOOGLE_CLOUD_RESOURCE_PREFIX_KEY); |
| 126 | + Matcher matcher = GOOGLE_CLOUD_RESOURCE_PREFIX_PATTERN.matcher(googleResourcePrefix); |
| 127 | + if (matcher.find()) { |
| 128 | + projectId = matcher.group("project"); |
| 129 | + if (matcher.group("instance") != null) { |
| 130 | + instanceId = matcher.group("instance"); |
| 131 | + } |
| 132 | + if (matcher.group("database") != null) { |
| 133 | + databaseId = matcher.group("database"); |
| 134 | + } |
| 135 | + } else { |
| 136 | + LOGGER.log(LEVEL, "Error parsing google cloud resource header: " + googleResourcePrefix); |
| 137 | + } |
| 138 | + } |
| 139 | + return getTagContext(method, projectId, instanceId, databaseId); |
| 140 | + } |
| 141 | +} |
0 commit comments