Metrics implementation


(Gianluca Valentini) #1

Hi,
I’d like to include some metrics in my input plugin.

In my Trasporter I added the code like below:

this.localRegistry.register("jdbc_input_rows_1sec", new Gauge<Long>() {
			@Override
			public Long getValue() {
				return lastSecRowsRead.get();
			}
		});
...
@Override
	public MetricSet getMetricSet() {
		return localRegistry;
	}

Where localRegistry is the injected LocalMetricRegistry localRegistry

Looking the Graylog metrics page System->Node->Metrics I hoped to se my metric too, but it is missing.
Where I’m wrong?

Can someone help me to understand how it works?

Just to complete my scenario the events come in Graylog correctly.
Thanks

Gianluca


(Kay Roepke) #2

Hi!

This should work like you expect, all you should have to do is to return the local metric registry as you did, and register the metrics themselves early, i.e. typically in the constructor.
The result of getMetricSet() is being prefixed by the class name and id of your input.

I’ve just tried to add a simple counter to the random http input and it showed up without problems in the metric browser.

Do you have a link to the complete source somewhere?

My diff:

diff --git a/graylog2-server/src/main/java/org/graylog2/inputs/transports/RandomMessageTransport.java b/graylog2-server/src/main/java/org/graylog2/inputs/transports/RandomMessageTransport.java
index f42f904c4..d64309f80 100644
--- a/graylog2-server/src/main/java/org/graylog2/inputs/transports/RandomMessageTransport.java
+++ b/graylog2-server/src/main/java/org/graylog2/inputs/transports/RandomMessageTransport.java
@@ -22,6 +22,7 @@ import com.google.common.eventbus.EventBus;
 import com.google.inject.assistedinject.Assisted;
 import com.google.inject.assistedinject.AssistedInject;
 import org.graylog2.inputs.random.generators.FakeHttpRawMessageGenerator;
+import org.graylog2.plugin.LocalMetricRegistry;
 import org.graylog2.plugin.configuration.Configuration;
 import org.graylog2.plugin.configuration.ConfigurationRequest;
 import org.graylog2.plugin.configuration.fields.ConfigurationField;
@@ -56,8 +57,8 @@ public class RandomMessageTransport extends GeneratorTransport {
     private final ObjectMapper objectMapper;
 
     @AssistedInject
-    public RandomMessageTransport(@Assisted Configuration configuration, EventBus eventBus, ObjectMapper objectMapper) {
-        super(eventBus, configuration);
+    public RandomMessageTransport(@Assisted Configuration configuration, EventBus eventBus, ObjectMapper objectMapper, LocalMetricRegistry localMetricRegistry) {
+        super(eventBus, configuration, localMetricRegistry);
         this.objectMapper = objectMapper;
 
         generator = new FakeHttpRawMessageGenerator(configuration.getString(CK_SOURCE));
diff --git a/graylog2-server/src/main/java/org/graylog2/plugin/inputs/transports/GeneratorTransport.java b/graylog2-server/src/main/java/org/graylog2/plugin/inputs/transports/GeneratorTransport.java
index e3fd333c3..56e17488a 100644
--- a/graylog2-server/src/main/java/org/graylog2/plugin/inputs/transports/GeneratorTransport.java
+++ b/graylog2-server/src/main/java/org/graylog2/plugin/inputs/transports/GeneratorTransport.java
@@ -16,10 +16,12 @@
  */
 package org.graylog2.plugin.inputs.transports;
 
+import com.codahale.metrics.Gauge;
 import com.codahale.metrics.MetricSet;
 import com.google.common.eventbus.EventBus;
 import com.google.common.util.concurrent.AbstractExecutionThreadService;
 import com.google.common.util.concurrent.Service;
+import org.graylog2.plugin.LocalMetricRegistry;
 import org.graylog2.plugin.configuration.Configuration;
 import org.graylog2.plugin.inputs.MessageInput;
 import org.graylog2.plugin.inputs.MisfireException;
@@ -28,13 +30,29 @@ import org.graylog2.plugin.journal.RawMessage;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.security.NoSuchAlgorithmException;
+import java.security.SecureRandom;
+
 public abstract class GeneratorTransport extends ThrottleableTransport {
     private static final Logger log = LoggerFactory.getLogger(GeneratorTransport.class);
+    private final LocalMetricRegistry localMetricRegistry;
+    private final Gauge<Long> someGauge;
 
     private Service generatorService;
 
-    public GeneratorTransport(EventBus eventBus, Configuration configuration) {
+    public GeneratorTransport(EventBus eventBus, Configuration configuration, LocalMetricRegistry localMetricRegistry) {
         super(eventBus, configuration);
+        this.localMetricRegistry = localMetricRegistry;
+        someGauge = localMetricRegistry.register("someGauge", new Gauge<Long>() {
+            @Override
+            public Long getValue() {
+                try {
+                    return SecureRandom.getInstanceStrong().nextLong();
+                } catch (NoSuchAlgorithmException e) {
+                    return 0L;
+                }
+            }
+        });
     }
 
     @Override
@@ -78,7 +96,7 @@ public abstract class GeneratorTransport extends ThrottleableTransport {
 
     @Override
     public MetricSet getMetricSet() {
-        return null;
+        return localMetricRegistry;
     }
 

(Gianluca Valentini) #3

Hi @kay
thanks for your help.
My code is similar to yours.

The strange situation is that I still do not see my metric on the Graylog UI, but using the ReST API call (api/system/metrics) It compare correctly:

 "my.jdbc.JDBCInput.59ca5c1c618e400f08961596.jdbc_input_rows_1sec": {
      "value": 0
 },

It’s very strange…


(Kay Roepke) #4

How do you search for the metric in the UI?


(Jochen) #5

The metrics browser in the Graylog web interface only displays metrics in the org namespace:


(Kay Roepke) #6

Quite unfortunate, but at least that explains it.
Not sure why we don’t expose the other ones (there should be jvm specific metrics masked this way as well).


(Gianluca Valentini) #7

Thanks everybody for your explanation.

I thought I was wrong…somewhere
Thanks
Gianluca


(system) #8

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.