3636import com .google .api .Distribution ;
3737import com .google .api .Metric ;
3838import com .google .api .MonitoredResource ;
39+ import com .google .cloud .bigtable .Version ;
40+ import com .google .cloud .bigtable .data .v2 .stub .EnhancedBigtableStubSettings ;
3941import com .google .cloud .opentelemetry .detection .AttributeKeys ;
4042import com .google .cloud .opentelemetry .detection .DetectedPlatform ;
4143import com .google .cloud .opentelemetry .detection .GCPPlatformDetector ;
42- import com .google .common .base .MoreObjects ;
4344import com .google .common .base .Preconditions ;
4445import com .google .common .base .Supplier ;
4546import com .google .common .base .Suppliers ;
47+ import com .google .common .collect .ImmutableList ;
48+ import com .google .common .collect .ImmutableMap ;
4649import com .google .common .collect .ImmutableSet ;
4750import com .google .monitoring .v3 .Point ;
4851import com .google .monitoring .v3 .ProjectName ;
6568import java .net .InetAddress ;
6669import java .net .UnknownHostException ;
6770import java .util .ArrayList ;
71+ import java .util .Arrays ;
6872import java .util .Collection ;
6973import java .util .HashMap ;
7074import java .util .List ;
7175import java .util .Map ;
76+ import java .util .Objects ;
7277import java .util .Set ;
7378import java .util .UUID ;
7479import java .util .logging .Level ;
7580import java .util .logging .Logger ;
81+ import java .util .stream .Collectors ;
7682import javax .annotation .Nullable ;
7783
7884/** Utils to convert OpenTelemetry types to Google Cloud Monitoring types. */
7985class BigtableExporterUtils {
86+ private static final String CLIENT_NAME = "java-bigtable/" + Version .VERSION ;
8087
8188 private static final Logger logger = Logger .getLogger (BigtableExporterUtils .class .getName ());
8289
@@ -87,6 +94,11 @@ class BigtableExporterUtils {
8794 ImmutableSet .of (
8895 BIGTABLE_PROJECT_ID_KEY , INSTANCE_ID_KEY , TABLE_ID_KEY , CLUSTER_ID_KEY , ZONE_ID_KEY );
8996
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+
90102 private BigtableExporterUtils () {}
91103
92104 /**
@@ -146,7 +158,7 @@ static Map<ProjectName, List<TimeSeries>> convertToBigtableTimeSeries(
146158 }
147159
148160 static List <TimeSeries > convertToApplicationResourceTimeSeries (
149- Collection <MetricData > collection , String taskId , MonitoredResource applicationResource ) {
161+ Collection <MetricData > collection , MonitoredResource applicationResource ) {
150162 Preconditions .checkNotNull (
151163 applicationResource ,
152164 "convert application metrics is called when the supported resource is not detected" );
@@ -160,16 +172,18 @@ static List<TimeSeries> convertToApplicationResourceTimeSeries(
160172 .map (
161173 pointData ->
162174 convertPointToApplicationResourceTimeSeries (
163- metricData , pointData , taskId , applicationResource ))
175+ metricData , pointData , applicationResource ))
164176 .forEach (allTimeSeries ::add );
165177 }
166178 return allTimeSeries ;
167179 }
168180
169181 @ Nullable
170- static MonitoredResource detectResourceSafe ( ) {
182+ static MonitoredResource createInternalMonitoredResource ( EnhancedBigtableStubSettings settings ) {
171183 try {
172- return detectResource ();
184+ MonitoredResource monitoredResource = detectResource (settings );
185+ logger .log (Level .FINE , "Internal metrics monitored resource: %s" , monitoredResource );
186+ return monitoredResource ;
173187 } catch (Exception e ) {
174188 logger .log (
175189 Level .WARNING ,
@@ -180,64 +194,66 @@ static MonitoredResource detectResourceSafe() {
180194 }
181195
182196 @ Nullable
183- private static MonitoredResource detectResource () {
197+ private static MonitoredResource detectResource (EnhancedBigtableStubSettings settings ) {
184198 GCPPlatformDetector detector = GCPPlatformDetector .DEFAULT_INSTANCE ;
185199 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 ;
205205 }
206- return monitoredResource ;
207- }
208206
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+ }
218241
219- private static MonitoredResource createGkeMonitoredResource (
220- String projectId , Map <String , String > attributes ) {
221242 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 ())
229254 .build ();
230255 }
231256
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-
241257 private static TimeSeries convertPointToBigtableTimeSeries (
242258 MetricData metricData , PointData pointData , String taskId ) {
243259 TimeSeries .Builder builder =
@@ -275,10 +291,7 @@ private static TimeSeries convertPointToBigtableTimeSeries(
275291 }
276292
277293 private static TimeSeries convertPointToApplicationResourceTimeSeries (
278- MetricData metricData ,
279- PointData pointData ,
280- String taskId ,
281- MonitoredResource applicationResource ) {
294+ MetricData metricData , PointData pointData , MonitoredResource applicationResource ) {
282295 TimeSeries .Builder builder =
283296 TimeSeries .newBuilder ()
284297 .setMetricKind (convertMetricKind (metricData ))
@@ -292,7 +305,6 @@ private static TimeSeries convertPointToApplicationResourceTimeSeries(
292305 metricBuilder .putLabels (key .getKey (), String .valueOf (attributes .get (key )));
293306 }
294307
295- metricBuilder .putLabels (CLIENT_UID_KEY .getKey (), taskId );
296308 builder .setMetric (metricBuilder .build ());
297309
298310 TimeInterval timeInterval =
0 commit comments