forked from lsjostro/prometheus-plugin
-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Monotonic Counter and Add New Build Counters (#606)
* I think its working * Done * Removed dead code
- Loading branch information
1 parent
ce2e589
commit 73e8034
Showing
21 changed files
with
911 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,5 @@ work | |
.classpath | ||
.settings/ | ||
build/ | ||
*.DS_Store | ||
*.DS_Store | ||
.vscode/* |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/java/org/jenkinsci/plugins/prometheus/collectors/builds/BuildAbortedCounter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.jenkinsci.plugins.prometheus.collectors.builds; | ||
|
||
import hudson.model.Result; | ||
import hudson.model.Run; | ||
import io.prometheus.client.Counter; | ||
import io.prometheus.client.SimpleCollector; | ||
|
||
import org.jenkinsci.plugins.prometheus.collectors.CollectorType; | ||
|
||
public class BuildAbortedCounter extends BuildsMetricCollector<Run<?, ?>, Counter> { | ||
protected BuildAbortedCounter(String[] labelNames, String namespace, String subsystem) { | ||
super(labelNames, namespace, subsystem); | ||
} | ||
|
||
protected BuildAbortedCounter(String[] labelNames, String namespace, String subsystem, String prefix) { | ||
super(labelNames, namespace, subsystem, prefix); | ||
} | ||
|
||
@Override | ||
protected CollectorType getCollectorType() { | ||
return CollectorType.BUILD_ABORTED_COUNTER; | ||
} | ||
|
||
@Override | ||
protected String getHelpText() { | ||
return "aborted build count"; | ||
} | ||
|
||
@Override | ||
protected SimpleCollector.Builder<?, Counter> getCollectorBuilder() { | ||
return Counter.build(); | ||
} | ||
|
||
@Override | ||
public void calculateMetric(Run<?, ?> jenkinsObject, String[] labelValues) { | ||
// Increment counter if result was unstable. | ||
if(jenkinsObject.getResult() == Result.ABORTED){ | ||
this.collector.labels(labelValues).inc(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
...main/java/org/jenkinsci/plugins/prometheus/collectors/builds/BuildCompletionListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package org.jenkinsci.plugins.prometheus.collectors.builds; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.concurrent.locks.Lock; | ||
import java.util.concurrent.locks.ReentrantLock; | ||
|
||
import hudson.Extension; | ||
import hudson.model.Run; | ||
import hudson.model.TaskListener; | ||
import hudson.model.listeners.RunListener; | ||
|
||
/* | ||
* Listens to builds that have been completed and stores them in a list. | ||
* The JobCollector reads items in the list when it performs a scrape and | ||
* publishes the data. | ||
* Class extends https://javadoc.jenkins.io/hudson/model/listeners/RunListener.html | ||
*/ | ||
public class BuildCompletionListener extends RunListener<Run<?,?>> { | ||
// static instance of the class to use as a singleton. | ||
private static BuildCompletionListener _Listener; | ||
|
||
// Lock to synchronize iteration and adding to the collection | ||
private Lock lock; | ||
|
||
// Holds the list o runs in queue. | ||
private List<Run<?,?>> runStack; | ||
|
||
// Iterable that defines a close method (allows us to use try resource) block | ||
// in JobCollector.java | ||
public interface CloseableIterator<T> extends Iterator<T>, AutoCloseable { | ||
void close(); | ||
} | ||
|
||
// Protected so no one can create their own copy of the class. | ||
protected BuildCompletionListener(){ | ||
runStack = Collections.synchronizedList(new ArrayList<>()); | ||
lock = new ReentrantLock(); | ||
} | ||
|
||
/* | ||
* Extension tells Jenkins to register this class as a RunListener and to use | ||
* this method in order to retrieve an instance of the class. It is a singleton | ||
* so we can get the same reference registered in Jenkins in another class. | ||
*/ | ||
@Extension | ||
public synchronized static BuildCompletionListener getInstance(){ | ||
if(_Listener == null){ | ||
_Listener = new BuildCompletionListener(); | ||
} | ||
return _Listener; | ||
} | ||
|
||
/* | ||
* Fires on completion of a job. | ||
*/ | ||
public void onCompleted(Run<?,?> run, TaskListener listener){ | ||
push(run); | ||
} | ||
|
||
/* | ||
* Pushes a run onto the list | ||
*/ | ||
private synchronized void push(Run<?,?> run){ | ||
// Acquire lock | ||
lock.lock(); | ||
|
||
// Try to add the run to the list. If something goes wrong, make sure | ||
// we still unlock the lock! | ||
try{ | ||
runStack.add(run); | ||
} | ||
finally{ | ||
lock.unlock(); | ||
} | ||
} | ||
|
||
/* | ||
* Returns a closeable iterator | ||
*/ | ||
public synchronized CloseableIterator<Run<?,?>> iterator(){ | ||
// acquire lock before iterating | ||
lock.lock(); | ||
return new CloseableIterator<Run<?,?>>() { | ||
// Get iterator from the list | ||
private Iterator<Run<?,?>> iterator = runStack.iterator(); | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return iterator.hasNext(); | ||
} | ||
|
||
@Override | ||
public Run<?,?> next() { | ||
return iterator.next(); | ||
} | ||
|
||
@Override | ||
public void remove() { | ||
iterator.remove(); | ||
} | ||
|
||
// When we close the iterator, clear the list right before we unlock. | ||
// This ensures we don't see the same job twice if iterator is called again. | ||
public void close() { | ||
runStack.clear(); | ||
lock.unlock(); | ||
} | ||
}; | ||
} | ||
} |
Oops, something went wrong.