36
36
import com .google .api .Distribution ;
37
37
import com .google .api .Metric ;
38
38
import com .google .api .MonitoredResource ;
39
+ import com .google .cloud .bigtable .Version ;
40
+ import com .google .cloud .bigtable .data .v2 .stub .EnhancedBigtableStubSettings ;
39
41
import com .google .cloud .opentelemetry .detection .AttributeKeys ;
40
42
import com .google .cloud .opentelemetry .detection .DetectedPlatform ;
41
43
import com .google .cloud .opentelemetry .detection .GCPPlatformDetector ;
42
- import com .google .common .base .MoreObjects ;
43
44
import com .google .common .base .Preconditions ;
44
45
import com .google .common .base .Supplier ;
45
46
import com .google .common .base .Suppliers ;
47
+ import com .google .common .collect .ImmutableList ;
48
+ import com .google .common .collect .ImmutableMap ;
46
49
import com .google .common .collect .ImmutableSet ;
47
50
import com .google .monitoring .v3 .Point ;
48
51
import com .google .monitoring .v3 .ProjectName ;
65
68
import java .net .InetAddress ;
66
69
import java .net .UnknownHostException ;
67
70
import java .util .ArrayList ;
71
+ import java .util .Arrays ;
68
72
import java .util .Collection ;
69
73
import java .util .HashMap ;
70
74
import java .util .List ;
71
75
import java .util .Map ;
76
+ import java .util .Objects ;
72
77
import java .util .Set ;
73
78
import java .util .UUID ;
74
79
import java .util .logging .Level ;
75
80
import java .util .logging .Logger ;
81
+ import java .util .stream .Collectors ;
76
82
import javax .annotation .Nullable ;
77
83
78
84
/** Utils to convert OpenTelemetry types to Google Cloud Monitoring types. */
79
85
class BigtableExporterUtils {
86
+ private static final String CLIENT_NAME = "java-bigtable/" + Version .VERSION ;
80
87
81
88
private static final Logger logger = Logger .getLogger (BigtableExporterUtils .class .getName ());
82
89
@@ -87,6 +94,11 @@ class BigtableExporterUtils {
87
94
ImmutableSet .of (
88
95
BIGTABLE_PROJECT_ID_KEY , INSTANCE_ID_KEY , TABLE_ID_KEY , CLUSTER_ID_KEY , ZONE_ID_KEY );
89
96
97
+ private static final Map <GCPPlatformDetector .SupportedPlatform , String > SUPPORTED_PLATFORM_MAP =
98
+ ImmutableMap .of (
99
+ GCPPlatformDetector .SupportedPlatform .GOOGLE_COMPUTE_ENGINE , "gcp_compute_engine" ,
100
+ GCPPlatformDetector .SupportedPlatform .GOOGLE_KUBERNETES_ENGINE , "gcp_kubernetes_engine" );
101
+
90
102
private BigtableExporterUtils () {}
91
103
92
104
/**
@@ -146,7 +158,7 @@ static Map<ProjectName, List<TimeSeries>> convertToBigtableTimeSeries(
146
158
}
147
159
148
160
static List <TimeSeries > convertToApplicationResourceTimeSeries (
149
- Collection <MetricData > collection , String taskId , MonitoredResource applicationResource ) {
161
+ Collection <MetricData > collection , MonitoredResource applicationResource ) {
150
162
Preconditions .checkNotNull (
151
163
applicationResource ,
152
164
"convert application metrics is called when the supported resource is not detected" );
@@ -160,16 +172,18 @@ static List<TimeSeries> convertToApplicationResourceTimeSeries(
160
172
.map (
161
173
pointData ->
162
174
convertPointToApplicationResourceTimeSeries (
163
- metricData , pointData , taskId , applicationResource ))
175
+ metricData , pointData , applicationResource ))
164
176
.forEach (allTimeSeries ::add );
165
177
}
166
178
return allTimeSeries ;
167
179
}
168
180
169
181
@ Nullable
170
- static MonitoredResource detectResourceSafe ( ) {
182
+ static MonitoredResource createInternalMonitoredResource ( EnhancedBigtableStubSettings settings ) {
171
183
try {
172
- return detectResource ();
184
+ MonitoredResource monitoredResource = detectResource (settings );
185
+ logger .log (Level .FINE , "Internal metrics monitored resource: %s" , monitoredResource );
186
+ return monitoredResource ;
173
187
} catch (Exception e ) {
174
188
logger .log (
175
189
Level .WARNING ,
@@ -180,64 +194,66 @@ static MonitoredResource detectResourceSafe() {
180
194
}
181
195
182
196
@ Nullable
183
- private static MonitoredResource detectResource () {
197
+ private static MonitoredResource detectResource (EnhancedBigtableStubSettings settings ) {
184
198
GCPPlatformDetector detector = GCPPlatformDetector .DEFAULT_INSTANCE ;
185
199
DetectedPlatform detectedPlatform = detector .detectPlatform ();
186
- MonitoredResource monitoredResource = null ;
187
- try {
188
- switch (detectedPlatform .getSupportedPlatform ()) {
189
- case GOOGLE_COMPUTE_ENGINE :
190
- monitoredResource =
191
- createGceMonitoredResource (
192
- detectedPlatform .getProjectId (), detectedPlatform .getAttributes ());
193
- break ;
194
- case GOOGLE_KUBERNETES_ENGINE :
195
- monitoredResource =
196
- createGkeMonitoredResource (
197
- detectedPlatform .getProjectId (), detectedPlatform .getAttributes ());
198
- break ;
199
- }
200
- } catch (IllegalStateException e ) {
201
- logger .log (
202
- Level .WARNING ,
203
- "Failed to create monitored resource for " + detectedPlatform .getSupportedPlatform (),
204
- e );
200
+
201
+ @ Nullable
202
+ String cloud_platform = SUPPORTED_PLATFORM_MAP .get (detectedPlatform .getSupportedPlatform ());
203
+ if (cloud_platform == null ) {
204
+ return null ;
205
205
}
206
- return monitoredResource ;
207
- }
208
206
209
- private static MonitoredResource createGceMonitoredResource (
210
- String projectId , Map <String , String > attributes ) {
211
- return MonitoredResource .newBuilder ()
212
- .setType ("gce_instance" )
213
- .putLabels ("project_id" , projectId )
214
- .putLabels ("instance_id" , getAttribute (attributes , AttributeKeys .GCE_INSTANCE_ID ))
215
- .putLabels ("zone" , getAttribute (attributes , AttributeKeys .GCE_AVAILABILITY_ZONE ))
216
- .build ();
217
- }
207
+ Map <String , String > attrs = detectedPlatform .getAttributes ();
208
+ ImmutableList <String > locationKeys =
209
+ ImmutableList .of (
210
+ AttributeKeys .GCE_CLOUD_REGION ,
211
+ AttributeKeys .GCE_AVAILABILITY_ZONE ,
212
+ AttributeKeys .GKE_LOCATION_TYPE_REGION ,
213
+ AttributeKeys .GKE_CLUSTER_LOCATION );
214
+
215
+ String region =
216
+ locationKeys .stream ().map (attrs ::get ).filter (Objects ::nonNull ).findFirst ().orElse ("global" );
217
+
218
+ // Deal with possibility of a zone. Zones are of the form us-east1-c, but we want a region
219
+ // which, which is us-east1.
220
+ region = Arrays .stream (region .split ("-" )).limit (2 ).collect (Collectors .joining ("-" ));
221
+
222
+ String hostname = attrs .get (AttributeKeys .GCE_INSTANCE_HOSTNAME );
223
+ // if (hostname == null) {
224
+ // hostname = attrs.get(AttributeKeys.SERVERLESS_COMPUTE_NAME);
225
+ // }
226
+ // if (hostname == null) {
227
+ // hostname = attrs.get(AttributeKeys.GAE_MODULE_NAME);
228
+ // }
229
+ if (hostname == null ) {
230
+ hostname = System .getenv ("HOSTNAME" );
231
+ }
232
+ if (hostname == null ) {
233
+ try {
234
+ hostname = InetAddress .getLocalHost ().getHostName ();
235
+ } catch (UnknownHostException ignored ) {
236
+ }
237
+ }
238
+ if (hostname == null ) {
239
+ hostname = "" ;
240
+ }
218
241
219
- private static MonitoredResource createGkeMonitoredResource (
220
- String projectId , Map <String , String > attributes ) {
221
242
return MonitoredResource .newBuilder ()
222
- .setType ("k8s_container" )
223
- .putLabels ("project_id" , projectId )
224
- .putLabels ("location" , getAttribute (attributes , AttributeKeys .GKE_CLUSTER_LOCATION ))
225
- .putLabels ("cluster_name" , getAttribute (attributes , AttributeKeys .GKE_CLUSTER_NAME ))
226
- .putLabels ("namespace_name" , MoreObjects .firstNonNull (System .getenv ("NAMESPACE" ), "" ))
227
- .putLabels ("pod_name" , MoreObjects .firstNonNull (System .getenv ("HOSTNAME" ), "" ))
228
- .putLabels ("container_name" , MoreObjects .firstNonNull (System .getenv ("CONTAINER_NAME" ), "" ))
243
+ .setType ("bigtable_client" )
244
+ .putLabels ("project_id" , settings .getProjectId ())
245
+ .putLabels ("instance" , settings .getInstanceId ())
246
+ .putLabels ("app_profile" , settings .getAppProfileId ())
247
+ .putLabels ("client_project" , detectedPlatform .getProjectId ())
248
+ .putLabels ("region" , region )
249
+ .putLabels ("cloud_platform" , cloud_platform )
250
+ .putLabels ("host_id" , attrs .get (AttributeKeys .GKE_HOST_ID ))
251
+ .putLabels ("host_name" , hostname )
252
+ .putLabels ("client_name" , CLIENT_NAME )
253
+ .putLabels ("uuid" , DEFAULT_TASK_VALUE .get ())
229
254
.build ();
230
255
}
231
256
232
- private static String getAttribute (Map <String , String > attributes , String key ) {
233
- String value = attributes .get (key );
234
- if (value == null ) {
235
- throw new IllegalStateException (
236
- "Required attribute " + key + " does not exist in the attributes map " + attributes );
237
- }
238
- return value ;
239
- }
240
-
241
257
private static TimeSeries convertPointToBigtableTimeSeries (
242
258
MetricData metricData , PointData pointData , String taskId ) {
243
259
TimeSeries .Builder builder =
@@ -275,10 +291,7 @@ private static TimeSeries convertPointToBigtableTimeSeries(
275
291
}
276
292
277
293
private static TimeSeries convertPointToApplicationResourceTimeSeries (
278
- MetricData metricData ,
279
- PointData pointData ,
280
- String taskId ,
281
- MonitoredResource applicationResource ) {
294
+ MetricData metricData , PointData pointData , MonitoredResource applicationResource ) {
282
295
TimeSeries .Builder builder =
283
296
TimeSeries .newBuilder ()
284
297
.setMetricKind (convertMetricKind (metricData ))
@@ -292,7 +305,6 @@ private static TimeSeries convertPointToApplicationResourceTimeSeries(
292
305
metricBuilder .putLabels (key .getKey (), String .valueOf (attributes .get (key )));
293
306
}
294
307
295
- metricBuilder .putLabels (CLIENT_UID_KEY .getKey (), taskId );
296
308
builder .setMetric (metricBuilder .build ());
297
309
298
310
TimeInterval timeInterval =
0 commit comments