Skip to content

Commit

Permalink
Merge pull request #38 from ruhan1/master-health
Browse files Browse the repository at this point in the history
Add health checks
  • Loading branch information
ruhan1 authored Jan 20, 2021
2 parents 18940df + 1022386 commit 1c0068f
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 4 deletions.
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@
<groupId>io.smallrye.reactive</groupId>
<artifactId>smallrye-mutiny-vertx-web-client</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-health</artifactId>
</dependency>

<!-- Test -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.commonjava.util.gateway.metrics.health;

import org.commonjava.util.gateway.metrics.honeycomb.JvmRootSpanFields;
import org.eclipse.microprofile.health.HealthCheck;
import org.eclipse.microprofile.health.HealthCheckResponse;
import org.eclipse.microprofile.health.Liveness;

import javax.enterprise.context.ApplicationScoped;
import java.util.Map;
import java.util.Optional;

import static org.eclipse.microprofile.health.HealthCheckResponse.State.UP;

@Liveness
@ApplicationScoped
public class HeapHealthCheck
implements HealthCheck
{
@Override
public HealthCheckResponse call()
{
return new HealthCheckResponse( "heap", UP, Optional.of( JvmRootSpanFields.getInstance().getHeapInfo() ) );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.commonjava.util.gateway.metrics.health;

import org.commonjava.util.gateway.metrics.honeycomb.JvmRootSpanFields;
import org.eclipse.microprofile.health.HealthCheck;
import org.eclipse.microprofile.health.HealthCheckResponse;
import org.eclipse.microprofile.health.Liveness;

import javax.enterprise.context.ApplicationScoped;
import java.util.Optional;

import static org.eclipse.microprofile.health.HealthCheckResponse.State.UP;

@Liveness
@ApplicationScoped
public class ThreadsHealthCheck
implements HealthCheck
{
@Override
public HealthCheckResponse call()
{
return new HealthCheckResponse( "threads", UP,
Optional.of( JvmRootSpanFields.getInstance().getThreadsInfo() ) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import org.commonjava.o11yphant.honeycomb.RootSpanFields;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -10,19 +13,62 @@ public class JvmRootSpanFields
{
private final static long MB = 1024 * 1024;

private static final JvmRootSpanFields INSTANCE = new JvmRootSpanFields();

public static JvmRootSpanFields getInstance()
{
return INSTANCE;
}

private final Runtime rt = Runtime.getRuntime();

private final ThreadMXBean mxBean = ManagementFactory.getThreadMXBean();

private JvmRootSpanFields()
{
}

@Override
public Map<String, Object> get()
{
Map<String, Object> ret = new HashMap<>();
getHeapInfo().forEach( ( k, v ) -> ret.put( "heap_" + k, v ) );
getThreadsInfo().forEach( ( k, v ) -> ret.put( "threads_" + k, v ) );
return ret;
}

public Map<String, Object> getHeapInfo()
{
long total = rt.totalMemory();
long free = rt.freeMemory();
long used = total - free;

Map<String, Object> ret = new HashMap<>();
ret.put( "heap_total_mb", total / MB );
ret.put( "heap_free_mb", free / MB );
ret.put( "heap_used_mb", used / MB );
ret.put( "total_mb", total / MB );
ret.put( "free_mb", free / MB );
ret.put( "used_mb", used / MB );
return ret;
}

public Map<String, Object> getThreadsInfo()
{
Map<String, Object> ret = new HashMap<>();
ThreadInfo[] threads = mxBean.getThreadInfo( mxBean.getAllThreadIds() );

for ( ThreadInfo info : threads )
{
String state = info.getThreadState().toString().toLowerCase();
Long count = (Long) ret.get( state );
if ( count == null )
{
count = 1L;
}
else
{
count += 1;
}
ret.put( state, count );
}
return ret;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public ProxyHoneycombManager( ProxyHoneycombConfiguration honeycombConfiguration
public void init()
{
super.init();
registerRootSpanFields( new JvmRootSpanFields() );
registerRootSpanFields( JvmRootSpanFields.getInstance() );
}

public void addFields( long elapse, HttpServerRequest request, Object item, Throwable err )
Expand Down

0 comments on commit 1c0068f

Please sign in to comment.