From e910c2c30003e74d3c04e3cafd1227cc91276b86 Mon Sep 17 00:00:00 2001 From: Mark Pollack Date: Fri, 11 Feb 2011 14:46:59 -0500 Subject: [PATCH] Additional JMX Metrics --- .../mongodb/monitor/AssertMetrics.java | 69 ++++++++++++++++ .../monitor/BackgroundFlushingMetrics.java | 80 +++++++++++++++++++ .../mongodb/monitor/BtreeIndexCounters.java | 76 ++++++++++++++++++ .../mongodb/monitor/ConnectionMetrics.java | 55 +++++++++++++ .../mongodb/monitor/MemoryMetrics.java | 73 +++++++++++++++++ 5 files changed, 353 insertions(+) create mode 100644 spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/monitor/AssertMetrics.java create mode 100644 spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/monitor/BackgroundFlushingMetrics.java create mode 100644 spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/monitor/BtreeIndexCounters.java create mode 100644 spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/monitor/ConnectionMetrics.java create mode 100644 spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/monitor/MemoryMetrics.java diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/monitor/AssertMetrics.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/monitor/AssertMetrics.java new file mode 100644 index 000000000..6356edd6d --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/monitor/AssertMetrics.java @@ -0,0 +1,69 @@ +/* + * Copyright 2002-2011 the original author or authors. + * + * 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 org.springframework.data.document.mongodb.monitor; + +import org.springframework.jmx.export.annotation.ManagedMetric; +import org.springframework.jmx.export.annotation.ManagedResource; +import org.springframework.jmx.support.MetricType; + +import com.mongodb.DBObject; +import com.mongodb.Mongo; + +/** + * JMX Metrics for assertions + * + * @author Mark Pollack + * + */ +@ManagedResource(description="Assertion Metrics") +public class AssertMetrics extends AbstractMonitor { + + public AssertMetrics(Mongo mongo) { + this.mongo = mongo; + } + + @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Regular") + public int getRegular() { + return getBtree("regular"); + } + + @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Warning") + public int getWarning() { + return getBtree("hits"); + } + + @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Msg") + public int getMsg() { + return getBtree("msg"); + } + + @ManagedMetric(metricType = MetricType.COUNTER, displayName = "User") + public int getUser() { + return getBtree("user"); + } + + @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Rollovers") + public int getRollovers() { + return getBtree("rollovers"); + } + + private int getBtree(String key) { + DBObject asserts = (DBObject) getServerStatus().get("asserts"); + //Class c = btree.get(key).getClass(); + return (Integer) asserts.get(key); + } + +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/monitor/BackgroundFlushingMetrics.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/monitor/BackgroundFlushingMetrics.java new file mode 100644 index 000000000..aed6413f2 --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/monitor/BackgroundFlushingMetrics.java @@ -0,0 +1,80 @@ +/* + * Copyright 2002-2011 the original author or authors. + * + * 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 org.springframework.data.document.mongodb.monitor; + +import java.util.Date; + +import org.springframework.jmx.export.annotation.ManagedMetric; +import org.springframework.jmx.export.annotation.ManagedResource; +import org.springframework.jmx.support.MetricType; + +import com.mongodb.DBObject; +import com.mongodb.Mongo; + +/** + * JMX Metrics for Background Flushing + * + * @author Mark Pollack + * + */ +@ManagedResource(description="Background Flushing Metrics") +public class BackgroundFlushingMetrics extends AbstractMonitor { + + + public BackgroundFlushingMetrics(Mongo mongo) { + this.mongo = mongo; + } + + @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Flushes") + public int getFlushes() { + return getFlushingData("flushes", java.lang.Integer.class); + } + + @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Total ms", unit="ms") + public int getTotalMs() { + return getFlushingData("total_ms", java.lang.Integer.class); + } + + @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Average ms", unit="ms") + public double getAverageMs() { + return getFlushingData("average_ms", java.lang.Double.class); + } + + @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Last Ms", unit="ms") + public int getLastMs() { + return getFlushingData("last_ms", java.lang.Integer.class); + } + + + @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Last finished") + public Date getLastFinished() { + return getLast(); + } + + @SuppressWarnings("unchecked") + private T getFlushingData(String key, Class targetClass) { + DBObject mem = (DBObject) getServerStatus().get("backgroundFlushing"); + return (T) mem.get(key); + } + + private Date getLast() { + DBObject bgFlush = (DBObject) getServerStatus().get("backgroundFlushing"); + Date lastFinished = (Date)bgFlush.get("last_finished"); + return lastFinished; + } + + +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/monitor/BtreeIndexCounters.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/monitor/BtreeIndexCounters.java new file mode 100644 index 000000000..cc9051156 --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/monitor/BtreeIndexCounters.java @@ -0,0 +1,76 @@ +/* + * Copyright 2002-2011 the original author or authors. + * + * 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 org.springframework.data.document.mongodb.monitor; + +import org.springframework.jmx.export.annotation.ManagedMetric; +import org.springframework.jmx.export.annotation.ManagedResource; +import org.springframework.jmx.support.MetricType; + +import com.mongodb.DBObject; +import com.mongodb.Mongo; + +/** + * JMX Metrics for B-tree index counters + * + * @author Mark Pollack + * + */ +@ManagedResource(description="Btree Metrics") +public class BtreeIndexCounters extends AbstractMonitor { + + public BtreeIndexCounters(Mongo mongo) { + this.mongo = mongo; + } + + @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Accesses") + public int getAccesses() { + return getBtree("accesses"); + } + + @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Hits") + public int getHits() { + return getBtree("hits"); + } + + @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Misses") + public int getMisses() { + return getBtree("misses"); + } + + @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Resets") + public int getResets() { + return getBtree("resets"); + } + + @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Miss Ratio") + public int getMissRatio() { + return getBtree("missRatio"); + } + + private int getBtree(String key) { + DBObject indexCounters = (DBObject) getServerStatus().get("indexCounters"); + if (indexCounters.get("note") != null) { + String message = (String) indexCounters.get("note"); + if (message.contains("not supported")) { + return -1; + } + } + DBObject btree = (DBObject) indexCounters.get("btree"); + //Class c = btree.get(key).getClass(); + return (Integer) btree.get(key); + } + +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/monitor/ConnectionMetrics.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/monitor/ConnectionMetrics.java new file mode 100644 index 000000000..bd9e809ec --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/monitor/ConnectionMetrics.java @@ -0,0 +1,55 @@ +/* + * Copyright 2002-2011 the original author or authors. + * + * 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 org.springframework.data.document.mongodb.monitor; + +import org.springframework.jmx.export.annotation.ManagedMetric; +import org.springframework.jmx.export.annotation.ManagedResource; +import org.springframework.jmx.support.MetricType; + +import com.mongodb.DBObject; +import com.mongodb.Mongo; + +/** + * JMX Metrics for Connections + * + * @author Mark Pollack + * + */ +@ManagedResource(description="Connection metrics") +public class ConnectionMetrics extends AbstractMonitor { + + public ConnectionMetrics(Mongo mongo) { + this.mongo = mongo; + } + + @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Current Connections") + public int getCurrent() { + return getConnectionData("current", java.lang.Integer.class); + } + + @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Available Connections") + public int getAvailable() { + return getConnectionData("available", java.lang.Integer.class); + } + + @SuppressWarnings("unchecked") + private T getConnectionData(String key, Class targetClass) { + DBObject mem = (DBObject) getServerStatus().get("connections"); + //Class c = mem.get(key).getClass(); + return (T) mem.get(key); + } + +} diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/monitor/MemoryMetrics.java b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/monitor/MemoryMetrics.java new file mode 100644 index 000000000..1b3086ed3 --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/monitor/MemoryMetrics.java @@ -0,0 +1,73 @@ +/* + * Copyright 2002-2011 the original author or authors. + * + * 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 org.springframework.data.document.mongodb.monitor; + +import org.springframework.jmx.export.annotation.ManagedMetric; +import org.springframework.jmx.export.annotation.ManagedResource; +import org.springframework.jmx.support.MetricType; + +import com.mongodb.DBObject; +import com.mongodb.Mongo; + +/** + * JMX Metrics for Memory + * @author Mark Pollack + * + */ +@ManagedResource(description="Memory Metrics") +public class MemoryMetrics extends AbstractMonitor { + + + public MemoryMetrics(Mongo mongo) { + this.mongo = mongo; + } + + @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Memory address size") + public int getBits() { + return getMemData("bits", java.lang.Integer.class); + } + + @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Resident in Physical Memory", unit="MB") + public int getResidentSpace() { + return getMemData("resident", java.lang.Integer.class); + } + + @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Virtual Address Space", unit="MB") + public int getVirtualAddressSpace() { + return getMemData("virtual", java.lang.Integer.class); + } + + @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Is memory info supported on this platform") + public boolean getMemoryInfoSupported() { + return getMemData("supported", java.lang.Boolean.class); + } + + + @ManagedMetric(metricType = MetricType.GAUGE, displayName = "Memory Mapped Space", unit="MB") + public int getMemoryMappedSpace() { + return getMemData("mapped", java.lang.Integer.class); + } + + + @SuppressWarnings("unchecked") + private T getMemData(String key, Class targetClass) { + DBObject mem = (DBObject) getServerStatus().get("mem"); + //Class c = mem.get(key).getClass(); + return (T) mem.get(key); + } + + +}