io.micrometer.core.instrument.binder.mongodb.MongoMetricsCommandListener Maven / Gradle / Ivy
/**
* Copyright 2019 VMware, 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
*
* https://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 io.micrometer.core.instrument.binder.mongodb;
import com.mongodb.MongoClient;
import com.mongodb.event.*;
import io.micrometer.core.annotation.Incubating;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Timer;
import io.micrometer.core.lang.NonNullApi;
import io.micrometer.core.lang.NonNullFields;
import java.util.concurrent.TimeUnit;
/**
* {@link CommandListener} for collecting command metrics from {@link MongoClient}.
*
* @author Christophe Bornet
* @since 1.2.0
*/
@NonNullApi
@NonNullFields
@Incubating(since = "1.2.0")
public class MongoMetricsCommandListener implements CommandListener {
private final Timer.Builder timerBuilder = Timer.builder("mongodb.driver.commands")
.description("Timer of mongodb commands");
private final MeterRegistry registry;
public MongoMetricsCommandListener(MeterRegistry registry) {
this.registry = registry;
}
@Override
public void commandStarted(CommandStartedEvent commandStartedEvent) {
}
@Override
public void commandSucceeded(CommandSucceededEvent event) {
timeCommand(event, "SUCCESS", event.getElapsedTime(TimeUnit.NANOSECONDS));
}
@Override
public void commandFailed(CommandFailedEvent event) {
timeCommand(event, "FAILED", event.getElapsedTime(TimeUnit.NANOSECONDS));
}
private void timeCommand(CommandEvent event, String status, long elapsedTimeInNanoseconds) {
timerBuilder
.tag("command", event.getCommandName())
.tag("cluster.id", event.getConnectionDescription().getConnectionId().getServerId().getClusterId().getValue())
.tag("server.address", event.getConnectionDescription().getServerAddress().toString())
.tag("status", status)
.register(registry)
.record(elapsedTimeInNanoseconds, TimeUnit.NANOSECONDS);
}
}