@@ -69,14 +69,27 @@ class HeaderInterceptor implements ClientInterceptor {
69
69
70
70
HeaderInterceptor () {}
71
71
72
+ private class SpannerProperties {
73
+ String projectId ;
74
+ String instanceId ;
75
+ String databaseId ;
76
+
77
+ SpannerProperties (String projectId , String instanceId , String databaseId ) {
78
+ this .databaseId = databaseId ;
79
+ this .instanceId = instanceId ;
80
+ this .projectId = projectId ;
81
+ }
82
+ }
83
+
72
84
@ Override
73
85
public <ReqT , RespT > ClientCall <ReqT , RespT > interceptCall (
74
86
MethodDescriptor <ReqT , RespT > method , CallOptions callOptions , Channel next ) {
75
87
return new SimpleForwardingClientCall <ReqT , RespT >(next .newCall (method , callOptions )) {
76
88
@ Override
77
89
public void start (Listener <RespT > responseListener , Metadata headers ) {
78
- TagContext tagContext = getTagContext (headers , method .getFullMethodName ());
79
- Attributes attributes = getMetricAttributes (headers , method .getFullMethodName ());
90
+ SpannerProperties spannerProperties = setProjectPropertes (headers );
91
+ TagContext tagContext = getTagContext (method .getFullMethodName (), spannerProperties );
92
+ Attributes attributes = getMetricAttributes (method .getFullMethodName (), spannerProperties );
80
93
super .start (
81
94
new SimpleForwardingClientCallListener <RespT >(responseListener ) {
82
95
@ Override
@@ -114,18 +127,7 @@ private void processHeader(Metadata metadata, TagContext tagContext, Attributes
114
127
}
115
128
}
116
129
117
- private TagContext getTagContext (
118
- String method , String projectId , String instanceId , String databaseId ) {
119
- return TAGGER
120
- .currentBuilder ()
121
- .putLocal (PROJECT_ID , TagValue .create (projectId ))
122
- .putLocal (INSTANCE_ID , TagValue .create (instanceId ))
123
- .putLocal (DATABASE_ID , TagValue .create (databaseId ))
124
- .putLocal (METHOD , TagValue .create (method ))
125
- .build ();
126
- }
127
-
128
- private TagContext getTagContext (Metadata headers , String method ) {
130
+ private SpannerProperties setProjectPropertes (Metadata headers ) {
129
131
String projectId = "undefined-project" ;
130
132
String instanceId = "undefined-database" ;
131
133
String databaseId = "undefined-database" ;
@@ -144,32 +146,24 @@ private TagContext getTagContext(Metadata headers, String method) {
144
146
LOGGER .log (LEVEL , "Error parsing google cloud resource header: " + googleResourcePrefix );
145
147
}
146
148
}
147
- return getTagContext ( method , projectId , instanceId , databaseId );
149
+ return new SpannerProperties ( projectId , instanceId , databaseId );
148
150
}
149
151
150
- private Attributes getMetricAttributes (Metadata headers , String method ) {
151
- String projectId = "undefined-project" ;
152
- String instanceId = "undefined-database" ;
153
- String databaseId = "undefined-database" ;
154
- if (headers .get (GOOGLE_CLOUD_RESOURCE_PREFIX_KEY ) != null ) {
155
- String googleResourcePrefix = headers .get (GOOGLE_CLOUD_RESOURCE_PREFIX_KEY );
156
- Matcher matcher = GOOGLE_CLOUD_RESOURCE_PREFIX_PATTERN .matcher (googleResourcePrefix );
157
- if (matcher .find ()) {
158
- projectId = matcher .group ("project" );
159
- if (matcher .group ("instance" ) != null ) {
160
- instanceId = matcher .group ("instance" );
161
- }
162
- if (matcher .group ("database" ) != null ) {
163
- databaseId = matcher .group ("database" );
164
- }
165
- } else {
166
- LOGGER .log (LEVEL , "Error parsing google cloud resource header: " + googleResourcePrefix );
167
- }
168
- }
152
+ private TagContext getTagContext (String method , SpannerProperties spannerProperties ) {
153
+ return TAGGER
154
+ .currentBuilder ()
155
+ .putLocal (PROJECT_ID , TagValue .create (spannerProperties .projectId ))
156
+ .putLocal (INSTANCE_ID , TagValue .create (spannerProperties .instanceId ))
157
+ .putLocal (DATABASE_ID , TagValue .create (spannerProperties .databaseId ))
158
+ .putLocal (METHOD , TagValue .create (method ))
159
+ .build ();
160
+ }
161
+
162
+ private Attributes getMetricAttributes (String method , SpannerProperties spannerProperties ) {
169
163
AttributesBuilder attributesBuilder = Attributes .builder ();
170
- attributesBuilder .put ("database" , databaseId );
171
- attributesBuilder .put ("instance_id" , instanceId );
172
- attributesBuilder .put ("project_id" , projectId );
164
+ attributesBuilder .put ("database" , spannerProperties . databaseId );
165
+ attributesBuilder .put ("instance_id" , spannerProperties . instanceId );
166
+ attributesBuilder .put ("project_id" , spannerProperties . projectId );
173
167
attributesBuilder .put ("method" , method );
174
168
175
169
return attributesBuilder .build ();
0 commit comments