All Downloads are FREE. Search and download functionalities are using the official Maven repository.

hu.icellmobilsoft.coffee.grpc.metrics.micrometer.server.MetricsServerCall Maven / Gradle / Ivy

There is a newer version: 2.9.0
Show newest version
/*-
 * #%L
 * Coffee
 * %%
 * Copyright (C) 2020 - 2023 i-Cell Mobilsoft Zrt.
 * %%
 * 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.
 * #L%
 */
package hu.icellmobilsoft.coffee.grpc.metrics.micrometer.server;

import hu.icellmobilsoft.coffee.grpc.metrics.micrometer.bundle.MetricsBundle;
import io.grpc.ForwardingServerCall;
import io.grpc.Metadata;
import io.grpc.MethodDescriptor;
import io.grpc.ServerCall;
import io.grpc.Status;
import io.grpc.Status.Code;

/**
 * 
 * Forwarding server call to handle metrics. Handles response counter metrics.
 * 
 * @author czenczl
 * @author Imre Scheffer
 * @since 2.5.0
 * 
 * @param 
 *            The type of message received.
 * @param 
 *            The type of message sent.
 */
public class MetricsServerCall extends ForwardingServerCall.SimpleForwardingServerCall {

    private Code responseCode = Code.UNKNOWN;

    private MetricsBundle metricBundle;

    /**
     * Creates a new server call to collect metrics
     * 
     * @param delegate
     *            original call
     * @param metricBundle
     *            counter and timer function container
     */
    public MetricsServerCall(ServerCall delegate, MetricsBundle metricBundle) {
        super(delegate);
        this.metricBundle = metricBundle;
    }

    /**
     * Gets the server response code to use with mertics data
     * 
     * @return server response code
     */
    public Code getResponseCode() {
        return this.responseCode;
    }

    @Override
    protected ServerCall delegate() {
        return super.delegate();
    }

    @Override
    public MethodDescriptor getMethodDescriptor() {
        return super.getMethodDescriptor();
    }

    @Override
    public void sendMessage(RespT message) {
        super.sendMessage(message);
    }

    @Override
    public void close(Status status, Metadata trailers) {
        // set status code for metric
        this.responseCode = status.getCode();
        // inc response counter because communication close
        metricBundle.getResponseCounter().increment();
        super.close(status, trailers);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy