com.yelp.nrtsearch.server.monitoring.NrtsearchMonitoringServerInterceptor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of server Show documentation
Show all versions of server Show documentation
GRPC lucene server using near-real-time replication
/*
* Copyright 2020 Yelp 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 com.yelp.nrtsearch.server.monitoring;
import io.grpc.Metadata;
import io.grpc.MethodDescriptor;
import io.grpc.ServerCall;
import io.grpc.ServerCallHandler;
import io.grpc.ServerInterceptor;
import java.time.Clock;
/** A {@link ServerInterceptor} which sends stats about incoming grpc calls to Prometheus. */
public class NrtsearchMonitoringServerInterceptor implements ServerInterceptor {
private final Clock clock;
private final Configuration configuration;
private final ServerMetrics.Factory serverMetricsFactory;
public static NrtsearchMonitoringServerInterceptor create(Configuration configuration) {
return new NrtsearchMonitoringServerInterceptor(
Clock.systemDefaultZone(), configuration, new ServerMetrics.Factory(configuration));
}
private NrtsearchMonitoringServerInterceptor(
Clock clock, Configuration configuration, ServerMetrics.Factory serverMetricsFactory) {
this.clock = clock;
this.configuration = configuration;
this.serverMetricsFactory = serverMetricsFactory;
}
@Override
public ServerCall.Listener interceptCall(
ServerCall call, Metadata requestHeaders, ServerCallHandler next) {
MethodDescriptor method = call.getMethodDescriptor();
com.yelp.nrtsearch.server.monitoring.ServerMetrics metrics =
serverMetricsFactory.createMetricsForMethod(method);
com.yelp.nrtsearch.server.monitoring.GrpcMethod grpcMethod =
com.yelp.nrtsearch.server.monitoring.GrpcMethod.of(method);
ServerCall monitoringCall =
new MonitoringServerCall(call, clock, grpcMethod, metrics, configuration);
return new MonitoringServerCallListener<>(
next.startCall(monitoringCall, requestHeaders),
metrics,
com.yelp.nrtsearch.server.monitoring.GrpcMethod.of(method));
}
}