Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

Include K8 configuration bean for stackdriver monitoring in metrics starter #2479

Open
dzou opened this issue Jul 28, 2020 · 1 comment
Open

Comments

@dzou
Copy link
Contributor

dzou commented Jul 28, 2020

When deploying an app to kubernetes, in order to get JVM application metrics with proper labels (i.e. get RAM usage by containerName or podName) you must add the following bean:

    @Bean
    public static StackdriverMeterRegistry stackdriver() {

        return StackdriverMeterRegistry.builder(new StackdriverConfig() {
            @Override
            public String projectId() {
                return MetadataConfig.getProjectId();
            }

            @Override
            public String get(String key) {
                return null;
            }
            @Override
            public String resourceType() {
                return "k8s_container";
            }

            @Override
            public Map<String, String> resourceLabels() {
                Map<String, String> map = new HashMap<>();
                String podName = System.getenv("HOSTNAME");
                String containerName = podName.substring(0,
                    podName.indexOf("-"));
                map.put("location", MetadataConfig.getZone());
                map.put("container_name", containerName);
                map.put("pod_name", podName);
                map.put("cluster_name", MetadataConfig.getClusterName());
                map.put("namespace_name", System.getenv("NAMESPACE"));
                return map;
            }
        }).build();
    }

See this for reference:
https://github.com/GoogleCloudPlatform/bank-of-anthos/pull/289/files#diff-ea214d77e7f8cce9af9624c294899ab3

I wonder if we can somehow incorporate this into the metrics starter in a clean way rather than have users provide this configuration. Note that System.getenv("HOSTNAME") is only set in linux environment.

@elefeint
Copy link
Contributor

Seems like a good idea. I am surprised Project ID is not autopopulated with our starter, though -- seems like it should, since our project autodetermination includes GKE.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

No branches or pull requests

2 participants