src/main/java/org/jmxtrans/agent/google/ApiFacade.java
private static void createCustomMetricDescriptor(
            @Nonnull String metricName,
            @Nonnull String metricKind,
            @Nonnull String metricUnit,
            @Nonnull String metricValueType,
            @Nonnull List<String> classificationLabels
    ) throws Exception {

        logger.info("Registering new custom metric : " + metricName + " : " + metricKind + " : " + metricUnit + " : " + metricValueType + " : " + classificationLabels);

        String url = "projects/" + projectId + "/metricDescriptors";
        String fullName = "custom.googleapis.com/" + metricName;

        JsonObject md = new JsonObject();
        md.add("name", url + "/" + URLEncoder.encode(fullName, "UTF-8"));
        md.add("type", fullName);

        md.add("metricKind", metricKind.toUpperCase());
        md.add("valueType", metricValueType.toUpperCase());
        md.add("unit", metricUnit);

        // Metric Labels
        JsonArray labels = new JsonArray();
        for (String entry : classificationLabels) {
            JsonObject label = new JsonObject();
            label.add("key", entry);
            label.add("description", entry);
            label.add("valueType", "STRING");
            labels.add(label);
        }
        md.add("labels", labels);

        String request = md.toString();
        logger.fine("Metric creation request : " + request);

        String response = cf.doPost(url, md.toString());
        logger.finer("Metric creation response" + response);

        logger.info("Created custom metric : " + metricName);
    }
private static void createCustomMetricDescriptor(
            @Nonnull String metricName,
            @Nonnull String metricKind,
            @Nonnull String metricUnit,
            @Nonnull String metricValueType,
            @Nonnull List<String> classificationLabels
    ) throws Exception {

        logger.info("Registering new custom metric : " + metricName + " : " + metricKind + " : " + metricUnit + " : " + metricValueType + " : " + classificationLabels);

        String url = "projects/" + projectId + "/metricDescriptors";
        String fullName = "custom.googleapis.com/" + metricName;

        JsonObject md = new JsonObject();
        md.add("name", url + "/" + URLEncoder.encode(fullName, "UTF-8"));
        md.add("type", fullName);

        md.add("metricKind", metricKind.toUpperCase());
        md.add("valueType", metricValueType.toUpperCase());
        md.add("unit", metricUnit);

        // Metric Labels
        JsonArray labels = new JsonArray();
        for (String entry : classificationLabels) {
            JsonObject label = new JsonObject();
            label.add("key", entry);
            label.add("description", entry);
            label.add("valueType", "STRING");
            labels.add(label);
        }
        md.add("labels", labels);

        String request = md.toString();
        logger.fine("Metric creation request : " + request);

        String response = cf.doPost(url, md.toString());
        logger.finer("Metric creation response" + response);

        logger.info("Created custom metric : " + metricName);
    }
private static void createCustomMetricDescriptor ( @ Nonnull String metricName , @ Nonnull String metricKind , @ Nonnull String metricUnit , @ Nonnull String metricValueType , @ Nonnull List < String > classificationLabels ) throws Exception { logger . info ( "Registering new custom metric : " + metricName + " : " + metricKind + " : " + metricUnit + " : " + metricValueType + " : " + classificationLabels ) ; String url = "projects/" + projectId + "/metricDescriptors" ; String fullName = "custom.googleapis.com/" + metricName ; JsonObject md = new JsonObject ( ) ; md . add ( "name" , url + "/" + URLEncoder . encode ( fullName , "UTF-8" ) ) ; md . add ( "type" , fullName ) ; md . add ( "metricKind" , metricKind . toUpperCase ( ) ) ; md . add ( "valueType" , metricValueType . toUpperCase ( ) ) ; md . add ( "unit" , metricUnit ) ; // Metric Labels JsonArray labels = new JsonArray ( ) ; for ( String entry : classificationLabels ) { JsonObject label = new JsonObject ( ) ; label . add ( "key" , entry ) ; label . add ( "description" , entry ) ; label . add ( "valueType" , "STRING" ) ; labels . add ( label ) ; } md . add ( "labels" , labels ) ; String request = md . toString ( ) ; logger . fine ( "Metric creation request : " + request ) ; String response = cf . doPost ( url , md . toString ( ) ) ; logger . finer ( "Metric creation response" + response ) ; logger . info ( "Created custom metric : " + metricName ) ; }
This method creates a custom metric with arbitrary names, description,
and units.
This method creates a custom metric with arbitrary names description and units .