Creating my own Glowroot collector.

156 views
Skip to first unread message

Anthony Roberts

unread,
Apr 6, 2018, 2:16:21 PM4/6/18
to Glowroot
Hi everyone.

I know this could be more of a Java programming exercise than anything specifically to do with Glowroot but I was wondering if there are any resources online that explain how to use the various Java packages to write my own agent based collector.

I want it to do nothing more than write the 5 minute aggregate gauge data to a log, eg; /opt/glowroot/collector/glowroot.collector.YYYY-MM-DD.log, where I can then get the have a non-glowroot agent deliver it to a central MariaDB data warehouse.

So far I've got the example collector compiled to a .jar thanks to the thread entitled 'What is about integration glowroot agent with other collectors?' but I don't really know it's doing and how best to write the type of collector I want.

Also, when I write a collector will it stop the UI and H2 from running as I'd like to keep them in place.

Any help would be greatly appreciated.

Anthony.

Trask Stalnaker

unread,
Apr 11, 2018, 2:54:10 AM4/11/18
to Glowroot
Hi Anthony,

Thanks for posting!

I fixed the Example Collector over at https://github.com/trask/glowroot-example-collector so it builds now against Glowroot 0.10.7.

The issue for you is that it will replace the embedded collector (H2/UI), which is not what you want.

There's a new feature in Glowroot 0.10.7 to support a "Collector Proxy" which allows you to write a proxy in front of the existing (e.g. embedded) collector.  There's a new Example Collector Proxy over at https://github.com/trask/glowroot-example-collector-proxy to demonstrate this.

Since you want to write out 5 minute aggregate gauge data, you will need to aggregate the 5 second gauge data in memory and write it out every 5 minutes.  Check out https://gist.github.com/trask/bcdec36392b15b068378c313497b33a5.

Thanks,
Trask

Anthony Roberts

unread,
Apr 11, 2018, 3:59:54 AM4/11/18
to Glowroot
Hi Trask.

Thanks for the update to Glowroot. It's very much appreciated. The URL https://gist.github.com/trask/bcdec36392b15b068378c313497b33a5 returns a 404 error.

Anthony.

Trask Stalnaker

unread,
Apr 11, 2018, 4:03:59 AM4/11/18
to Glowroot

Anthony Roberts

unread,
Apr 17, 2018, 2:58:49 AM4/17/18
to Glowroot
Hi Trask.

Thanks for your help so far with the example collector. Although I'm not a programmer I did manage to build it, although I'm not sure what it's doing or if I'm doing it right. I didn't use Apache Maven as it's too complicated for someone like me. :)

My next problem is that the example collector isn't being recognised when I put it in /opt/glowroot/services and restart Glowroot. There's no log entry that says that the file has been detected or being used and I don't see any output. I'm not sure if that's because there's a problem with Glowroot, if the example collector .jar file in /opt/glowroot/services is wrong, or if I've not done something else? Could you help me debug the problem?

I was also wondering if you could send me a built version of the example collector that you know is working? It'd only be running on my test server so it wouldn't break anything.

Anthony.

Trask Stalnaker

unread,
Apr 17, 2018, 8:29:13 PM4/17/18
to Glowroot
Hi Anthony,

Don't be intimidated by Maven.  You just need to run "mvn clean install" from inside the example collector folder, and it will build the jar file under a sub directory named "target".

Thanks,
Trask

Anthony Roberts

unread,
Apr 18, 2018, 2:57:07 AM4/18/18
to Glowroot
Hi Trask.

I'll try not to be intimidated but, since I'm not a developer, I'm going to running these commands blind. I copied the code for the example collector to a directory and run the mvn command. Here's the output.

[root ~]# cd /tmp/ant/ExampleCollector
[root ExampleCollector]# ls
ExampleCollector.java
[root ExampleCollector]# mvn clean install -X
.
.
.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.193 s
[INFO] Finished at: 2018-04-18T07:52:21+01:00
[INFO] ------------------------------------------------------------------------
[ERROR] The goal you specified requires a project to execute but there is no POM in this directory (/tmp/ant/ExampleCollector). Please verify you invoked Maven from the correct directory. -> [Help 1]
org.apache.maven.lifecycle.MissingProjectException: The goal you specified requires a project to execute but there is no POM in this directory (/tmp/ant/ExampleCollector). Please verify you invoked Maven from the correct directory.
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:84)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:290)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:194)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
[ERROR]
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:

Anthony.

Trask Stalnaker

unread,
Apr 18, 2018, 1:47:26 PM4/18/18
to Glowroot
Hi Anthony,

You're close, you want to use the directory from https://github.com/trask/glowroot-example-collector-proxy to run "mvn clean install", e.g.

Cloning into 'glowroot-example-collector-proxy'...
remote: Counting objects: 15, done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 15 (delta 0), reused 15 (delta 0), pack-reused 0
Unpacking objects: 100% (15/15), done.

$ cd glowroot-example-collector-proxy

$ mvn clean install
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building glowroot-example-collector-proxy 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ glowroot-example-collector-proxy ---
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ glowroot-example-collector-proxy ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ glowroot-example-collector-proxy ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to D:\git\glowroot-example-collector-proxy\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ glowroot-example-collector-proxy ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\git\glowroot-example-collector-proxy\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ glowroot-example-collector-proxy ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ glowroot-example-collector-proxy ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ glowroot-example-collector-proxy ---
[INFO] Building jar: D:\git\glowroot-example-collector-proxy\target\glowroot-example-collector-proxy-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ glowroot-example-collector-proxy ---
[INFO] Installing D:\git\glowroot-example-collector-proxy\target\glowroot-example-collector-proxy-1.0-SNAPSHOT.jar to C:\Users\trask.stalnaker\.m2\repository\org\example\glowroot-example-collector-proxy\1.0-SNAPSHOT\glowroot-example-collector-proxy-1.0-SNAPSHOT.jar
[INFO] Installing D:\git\glowroot-example-collector-proxy\pom.xml to C:\Users\trask.stalnaker\.m2\repository\org\example\glowroot-example-collector-proxy\1.0-SNAPSHOT\glowroot-example-collector-proxy-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.523 s
[INFO] Finished at: 2018-04-18T10:45:31-07:00
[INFO] Final Memory: 22M/479M
[INFO] ------------------------------------------------------------------------

$ ls -lt target/
total 4
-rw-r--r-- 1 trask.stalnaker 1049089 3742 Apr 18 10:45 glowroot-example-collector-proxy-1.0-SNAPSHOT.jar
drwxr-xr-x 1 trask.stalnaker 1049089    0 Apr 18 10:45 maven-archiver/
drwxr-xr-x 1 trask.stalnaker 1049089    0 Apr 18 10:45 classes/
drwxr-xr-x 1 trask.stalnaker 1049089    0 Apr 18 10:45 generated-sources/
drwxr-xr-x 1 trask.stalnaker 1049089    0 Apr 18 10:45 maven-status/

Thanks,
Trask

Anthony Roberts

unread,
Apr 18, 2018, 3:24:21 PM4/18/18
to Glowroot
Hi Trask.

Brilliant! Thank you.

I spent today working out that I need to download the Git repository but I ended up downloading and trying to build everything. It's good to know that I was on the right track, sort of!

I've built and installed the example collector. It got picked up, which is great progress.

2018-04-18 20:06:50.824 INFO  org.glowroot - using collector proxy: org.example.ExampleCollector

Looking at the example Java code, where should the collector be writing to? I can see the gauges working in the GUI but I don't see any data being written to a file. Am I missing something?

Anthony.

Trask Stalnaker

unread,
Apr 18, 2018, 3:26:43 PM4/18/18
to Glowroot
Great to hear.

Did you overlay https://gist.github.com/trask/bdbe7ec4d7f7010b2fa41007422c48b2 before building?  The git repo doesn't have that code.

Trask

Anthony Roberts

unread,
Apr 18, 2018, 3:40:11 PM4/18/18
to Glowroot
Hi Trask.

I hadn't before but I have now. I still don't get any data.

Anthony.

Trask Stalnaker

unread,
Apr 18, 2018, 3:42:54 PM4/18/18
to Glowroot

Anthony Roberts

unread,
Apr 18, 2018, 3:53:08 PM4/18/18
to Glowroot
Where should the data be writing to? It's been a good 20 minutes and I've not seen any obvious output.

Anthony Roberts

unread,
Apr 19, 2018, 10:33:26 AM4/19/18
to Glowroot
Hi Trask.

Any idea why the example collector isn't writing output? I've spent a day getting nowhere trying to figure it out, with no success.

On a plus note, I started learning Java from scratch. :)

Anthony.

On Wednesday, 18 April 2018 20:42:54 UTC+1, Trask Stalnaker wrote:

Trask Stalnaker

unread,
Apr 19, 2018, 10:49:48 AM4/19/18
to Glowroot
It should be writing to stdout.  That's usually the console where you are running your application from, but it can go other places depending on how you are running your application.  You can write to a file instead with a couple of changes:

    private static void write(Map<String, List<GaugeValue>> collectedGaugeValuesMap,
            long outputTime) throws FileNotFoundException {
        PrintStream out = new PrintStream(new FileOutputStream(new File("gaugevalues.txt"), true));
        try {
            for (Map.Entry<String, List<GaugeValue>> entry : collectedGaugeValuesMap.entrySet()) {
                double totalWeightedValue = 0;
                long totalWeight = 0;
                for (GaugeValue gaugeValue : entry.getValue()) {
                    totalWeightedValue += gaugeValue.getValue() * gaugeValue.getWeight();
                    totalWeight += gaugeValue.getWeight();
                }
                out.format("%tF %tT\t%s\t%f%n", outputTime, outputTime, entry.getKey(),
                        totalWeightedValue / totalWeight);
            }
        } finally {
            out.close();
        }
    }

Great to hear about Java!

Trask

Anthony Roberts

unread,
Apr 19, 2018, 4:06:53 PM4/19/18
to Glowroot
Hi Trask.

I've built the collector, deployed it, and it's working perfectly. Thank you so much for your help.

Anthony.

Trask Stalnaker

unread,
Apr 19, 2018, 6:26:47 PM4/19/18
to Glowroot
That's great to hear!

Trask
Message has been deleted

anirudh...@saividya.ac.in

unread,
Jan 15, 2019, 5:15:53 AM1/15/19
to Glowroot
Hey
Can you tell me what you mean by overlaying?

Anirudh

anirudh...@saividya.ac.in

unread,
Jan 15, 2019, 6:47:35 AM1/15/19
to Glowroot
Hey Trask,
Can you also let me know if this works with the latest version of glowroot?

Anirudh

Trask Stalnaker

unread,
Jan 15, 2019, 2:27:29 PM1/15/19
to Glowroot
Hi Anirudh,

I just updated both repos to work with the latest version of glowroot:


The "overlaying" comment above is obsolete, as that code is not in the example collector.

Let me know if you have any trouble.

Thanks,
Trask

anirudh...@saividya.ac.in

unread,
Jan 16, 2019, 12:10:20 AM1/16/19
to Glowroot
Hi Trask,

It is working fine after I used the updated collector proxy. Thank you for helping me out.

Anirudh

Trask Stalnaker

unread,
Jan 16, 2019, 12:20:53 AM1/16/19
to Glowroot
👍
Reply all
Reply to author
Forward
0 new messages