co.cask.tigon.sql.internal.MetricsRecorder.java Source code

Java tutorial

Introduction

Here is the source code for co.cask.tigon.sql.internal.MetricsRecorder.java

Source

/*
 * Copyright  2014 Cask Data, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package co.cask.tigon.sql.internal;

import co.cask.tigon.api.metrics.Metrics;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import java.util.Map;

/**
 * This class is a wrapper on the {@link Metrics} object to be used by
 * {@link co.cask.tigon.sql.flowlet.AbstractInputFlowlet}. It parses the incoming metrics and adds that data to the
 * underlying {@link Metrics} object.
 */
public class MetricsRecorder {
    private final Metrics metrics;

    /**
     * Constructor
     *
     * @param metrics The underlying {@link Metrics} object
     */
    public MetricsRecorder(Metrics metrics) {
        this.metrics = metrics;
    }

    /**
     * This methods logs the received metrics into the underlying {@link Metrics} object.
     * This method should be invoked every time metrics are received from a SQL Compiler process.
     *
     * @param processName Name of the process which sent these metrics
     * @param metricsData The metrics sent by the process in a JSON format
     */
    public void recordMetrics(String processName, JsonObject metricsData) {
        for (Map.Entry<String, JsonElement> entry : metricsData.entrySet()) {
            metrics.count(processName + "." + entry.getKey(), Math.round(entry.getValue().getAsFloat()));
        }
    }
}