Skip to content
This repository was archived by the owner on Jan 2, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
source 'https://rubygems.org/'

gemspec
gemspec name: 'multimeter'
gemspec name: 'multimeter-http'

group :examples do
gem 'rack'
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Rake::JavaExtensionTask.new('multimeter_metrics') do |ext|
end

namespace :bundler do
Bundler::GemHelper.install_tasks
Bundler::GemHelper.install_tasks(name: 'multimeter')
end

task :release => [:spec, :compile, 'bundler:release']
Expand Down
2 changes: 2 additions & 0 deletions ext/java/MultimeterMetricsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import multimeter.Histogram;
import multimeter.Snapshot;
import multimeter.Gauge;
import multimeter.JSONSerializer;

public class MultimeterMetricsService implements BasicLibraryService {
public boolean basicLoad(final Ruby runtime) throws IOException {
Expand All @@ -20,6 +21,7 @@ public boolean basicLoad(final Ruby runtime) throws IOException {
Histogram.setup(runtime);
Snapshot.setup(runtime);
Gauge.setup(runtime);
JSONSerializer.setup(runtime);
return true;
}
}
12 changes: 12 additions & 0 deletions ext/java/multimeter/Counter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
import org.jruby.RubyObject;
import org.jruby.RubyHash;
import org.jruby.RubyArray;
import org.jruby.RubyString;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.runtime.Block;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.javasupport.JavaUtil;

import static org.jruby.runtime.Visibility.PRIVATE;

Expand All @@ -30,6 +32,16 @@ public Counter(Ruby runtime, com.codahale.metrics.Counter counter) {
this.counter = counter;
}

@JRubyMethod(name="to_java")
public IRubyObject toJava(ThreadContext ctx) {
return JavaUtil.convertJavaToUsableRubyObject(ctx.runtime, counter);
}

@JRubyMethod(name="to_json")
public RubyString toJson(ThreadContext ctx) {
return JSONSerializer.getInstance().serialize(ctx, counter);
}

@JRubyMethod
public IRubyObject count(ThreadContext ctx) {
return ctx.runtime.newFixnum(counter.getCount());
Expand Down
11 changes: 11 additions & 0 deletions ext/java/multimeter/Gauge.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.jruby.RubyObject;
import org.jruby.RubyHash;
import org.jruby.RubyArray;
import org.jruby.RubyString;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.runtime.Block;
Expand All @@ -31,6 +32,16 @@ public Gauge(Ruby runtime, com.codahale.metrics.Gauge<? extends Object> gauge) {
this.gauge = gauge;
}

@JRubyMethod(name="to_java")
public IRubyObject toJava(ThreadContext ctx) {
return JavaUtil.convertJavaToUsableRubyObject(ctx.runtime, gauge);
}

@JRubyMethod(name="to_json")
public RubyString toJson(ThreadContext ctx) {
return JSONSerializer.getInstance().serialize(ctx, gauge);
}

@JRubyMethod
public IRubyObject value(ThreadContext ctx) {
Object value = gauge.getValue();
Expand Down
12 changes: 12 additions & 0 deletions ext/java/multimeter/Histogram.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
import org.jruby.RubyObject;
import org.jruby.RubyHash;
import org.jruby.RubyArray;
import org.jruby.RubyString;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.runtime.Block;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.javasupport.JavaUtil;

import static org.jruby.runtime.Visibility.PRIVATE;

Expand All @@ -30,6 +32,16 @@ public Histogram(Ruby runtime, com.codahale.metrics.Histogram histogram) {
this.histogram = histogram;
}

@JRubyMethod(name="to_java")
public IRubyObject toJava(ThreadContext ctx) {
return JavaUtil.convertJavaToUsableRubyObject(ctx.runtime, histogram);
}

@JRubyMethod(name="to_json")
public RubyString toJson(ThreadContext ctx) {
return JSONSerializer.getInstance().serialize(ctx, histogram);
}

@JRubyMethod
public IRubyObject count(ThreadContext ctx) {
return ctx.runtime.newFixnum(histogram.getCount());
Expand Down
55 changes: 55 additions & 0 deletions ext/java/multimeter/JSONSerializer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package multimeter;

import com.codahale.metrics.json.MetricsModule;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.jruby.Ruby;
import org.jruby.RubyClass;
import org.jruby.RubyException;
import org.jruby.exceptions.RaiseException;
import org.jruby.RubyModule;
import org.jruby.RubyString;
import org.jruby.anno.JRubyClass;
import org.jruby.runtime.ThreadContext;

public class JSONSerializer {
private ObjectMapper mapper;
private MetricsModule module;

public static void setup(Ruby runtime) {
RubyModule multimeterModule = runtime.defineModule("Multimeter");
RubyClass standardError = runtime.getStandardError();
multimeterModule.defineClassUnder("JSONError", standardError, standardError.getAllocator());
}

@JRubyClass(name="Multimeter::JSONError", parent="StandardError")
public static class JSONError {}

private static JSONSerializer instance;

public static JSONSerializer getInstance() {
if ( instance == null ) {
instance = new JSONSerializer();
}
return instance;
}

private JSONSerializer() {
this.mapper = new ObjectMapper();
this.module = new MetricsModule(TimeUnit.SECONDS, TimeUnit.MILLISECONDS, false);
mapper.registerModule(module);
}

public RubyString serialize(ThreadContext ctx, Object object) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
try {
mapper.writeValue(stream, object);
return RubyString.newString(ctx.runtime, stream.toByteArray());
} catch (IOException e) {
RubyClass jsonError = ctx.runtime.getModule("Multimeter").getClass("JSONError");
throw new RaiseException(RubyException.newException(ctx.runtime, jsonError, e.toString()), false);
}
}
}
12 changes: 12 additions & 0 deletions ext/java/multimeter/Meter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
import org.jruby.RubyObject;
import org.jruby.RubyHash;
import org.jruby.RubyArray;
import org.jruby.RubyString;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.runtime.Block;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.javasupport.JavaUtil;

import static org.jruby.runtime.Visibility.PRIVATE;

Expand All @@ -30,6 +32,16 @@ public Meter(Ruby runtime, com.codahale.metrics.Meter meter) {
this.meter = meter;
}

@JRubyMethod(name="to_java")
public IRubyObject toJava(ThreadContext ctx) {
return JavaUtil.convertJavaToUsableRubyObject(ctx.runtime, meter);
}

@JRubyMethod(name="to_json")
public RubyString toJson(ThreadContext ctx) {
return JSONSerializer.getInstance().serialize(ctx, meter);
}

@JRubyMethod
public IRubyObject count(ThreadContext ctx) {
return ctx.runtime.newFixnum(meter.getCount());
Expand Down
5 changes: 5 additions & 0 deletions ext/java/multimeter/MetricRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public IRubyObject toJava(ThreadContext ctx) {
return JavaUtil.convertJavaToUsableRubyObject(ctx.runtime, registry);
}

@JRubyMethod(name="to_json")
public RubyString toJson(ThreadContext ctx) {
return JSONSerializer.getInstance().serialize(ctx, registry);
}

@JRubyMethod
public IRubyObject metrics(ThreadContext ctx) {
return metrics.dup(ctx);
Expand Down
12 changes: 12 additions & 0 deletions ext/java/multimeter/Snapshot.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
import org.jruby.RubyObject;
import org.jruby.RubyHash;
import org.jruby.RubyArray;
import org.jruby.RubyString;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.runtime.Block;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.javasupport.JavaUtil;

import static org.jruby.runtime.Visibility.PRIVATE;

Expand All @@ -30,6 +32,16 @@ public Snapshot(Ruby runtime, com.codahale.metrics.Snapshot snapshot) {
this.snapshot = snapshot;
}

@JRubyMethod(name="to_java")
public IRubyObject toJava(ThreadContext ctx) {
return JavaUtil.convertJavaToUsableRubyObject(ctx.runtime, snapshot);
}

@JRubyMethod(name="to_json")
public RubyString toJson(ThreadContext ctx) {
return JSONSerializer.getInstance().serialize(ctx, snapshot);
}

@JRubyMethod
public IRubyObject size(ThreadContext ctx) {
return ctx.runtime.newFixnum(snapshot.size());
Expand Down
12 changes: 12 additions & 0 deletions ext/java/multimeter/Timer.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
import org.jruby.RubyObject;
import org.jruby.RubyHash;
import org.jruby.RubyArray;
import org.jruby.RubyString;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.runtime.Block;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.javasupport.JavaUtil;

import static org.jruby.runtime.Visibility.PRIVATE;

Expand All @@ -35,6 +37,16 @@ public Timer(Ruby runtime, com.codahale.metrics.Timer timer) {
this.timer = timer;
}

@JRubyMethod(name="to_java")
public IRubyObject toJava(ThreadContext ctx) {
return JavaUtil.convertJavaToUsableRubyObject(ctx.runtime, timer);
}

@JRubyMethod(name="to_json")
public RubyString toJson(ThreadContext ctx) {
return JSONSerializer.getInstance().serialize(ctx, timer);
}

@JRubyMethod
public IRubyObject count(ThreadContext ctx) {
return ctx.runtime.newFixnum(timer.getCount());
Expand Down
Loading