Skip to content

Commit

Permalink
Initial commit of upgrade to metrics 3.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffellis authored and a-team+pairing committed Nov 6, 2013
1 parent b7389a5 commit ae98759
Show file tree
Hide file tree
Showing 17 changed files with 127 additions and 63 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -21,7 +21,7 @@ Annotations
-------
@Timed
This annotation can be added to any method you wish to be timed. @Timed uses sensible defaults to create an instance of
com.yammer.metrics.core.Timer and the associated code to update it from within your method body.
com.codahale.metrics.core.Timer and the associated code to update it from within your method body.

Before
```
Expand Down Expand Up @@ -59,7 +59,7 @@ class SomeService{

@Metered
This annotation can be added to any method you wish to be metered. @Metered uses sensible defaults to create an instance of
com.yammer.metrics.core.Meter and the associated code to update it from within your method body.
com.codahale.metrics.core.Meter and the associated code to update it from within your method body.


Before
Expand Down
17 changes: 15 additions & 2 deletions YammerMetricsGrailsPlugin.groovy
Expand Up @@ -6,7 +6,7 @@ import org.apache.commons.lang.StringUtils
class YammerMetricsGrailsPlugin {

// the plugin version
def version = "2.2.0-1"
def version = "3.0.1-1"
// the version or versions of Grails the plugin is designed for
def grailsVersion = "2.0.3 > *"
// the other plugins this plugin depends on
Expand Down Expand Up @@ -34,7 +34,20 @@ See the source code documentation on Github for more details.
def doWithWebDescriptor = { xml ->

if(application.config.metrics.servletEnabled!=false){
def count = xml.'servlet'.size()
def count = xml.'listener'.size()
def listenerElement = xml.'listener'[count - 1]
if(count > 0) {
listenerElement + {
'listener' {
'listener-class'('org.grails.plugins.yammermetrics.groovy.HealthCheckServletContextListener')
}
'listener' {
'listener-class'('org.grails.plugins.yammermetrics.groovy.MetricsServletContextListener')
}
}
}

count = xml.'servlet'.size()
if(count > 0) {

def servletElement = xml.'servlet'[count - 1]
Expand Down
7 changes: 7 additions & 0 deletions example-app/grails-app/conf/BootStrap.groovy
@@ -1,7 +1,14 @@
import com.codahale.metrics.health.HealthCheckRegistry
import metricstest.AlwaysHealthyCheck
import org.grails.plugins.yammermetrics.groovy.HealthCheckServletContextListener

class BootStrap {

def init = { servletContext ->
HealthCheckRegistry healthCheckRegistry = HealthCheckServletContextListener.HEALTH_CHECK_REGISTRY
healthCheckRegistry.register("AlwaysHealthyCheck", new AlwaysHealthyCheck())
}

def destroy = {
}
}
8 changes: 4 additions & 4 deletions example-app/grails-app/conf/BuildConfig.groovy
Expand Up @@ -12,8 +12,8 @@ grails.project.source.level = 1.6
//]

// Testing the plugin
grails.plugin.location.'yammer-metrics' = "/home/jeff/projects/grails-yammer-metrics"
yammermetrics.version = System.getProperty( "yammermetrics.version", "2.2.0" )
grails.plugin.location.'yammer-metrics' = "../."
yammermetrics.version = System.getProperty( "yammermetrics.version", "3.0.1" )

grails.project.dependency.resolution = {

Expand Down Expand Up @@ -49,8 +49,8 @@ grails.project.dependency.resolution = {
// runtime 'mysql:mysql-connector-java:5.1.20'

// Have to duplicate this here to use the plugin in-line.
runtime "com.yammer.metrics:metrics-core:${yammermetrics.version}"
runtime "com.yammer.metrics:metrics-servlet:${yammermetrics.version}"
runtime "com.codahale.metrics:metrics-core:${yammermetrics.version}"
runtime "com.codahale.metrics:metrics-servlets:${yammermetrics.version}"

}

Expand Down
11 changes: 11 additions & 0 deletions example-app/src/groovy/metricstest/AlwaysHealthyCheck.groovy
@@ -0,0 +1,11 @@
package metricstest

import com.codahale.metrics.health.HealthCheck

class AlwaysHealthyCheck extends HealthCheck {

@Override
protected HealthCheck.Result check() throws Exception {
return HealthCheck.Result.healthy()
}
}
10 changes: 5 additions & 5 deletions grails-app/conf/BuildConfig.groovy
@@ -1,12 +1,12 @@
/*
* Copyright 2012 Jeff Ellis
* Copyright 2013 Jeff Ellis
*/

grails.project.class.dir = "target/classes"
grails.project.test.class.dir = "target/test-classes"
grails.project.test.reports.dir = "target/test-reports"

yammermetrics.version = "2.2.0"
yammermetrics.version = "3.0.1"

grails.project.dependency.resolution = {
// inherit Grails' default dependencies
Expand Down Expand Up @@ -38,7 +38,7 @@ grails.project.dependency.resolution = {
export = false
}

build ':codenarc:0.17', {
build ":codenarc:0.19", {
export = false
}

Expand All @@ -48,8 +48,8 @@ grails.project.dependency.resolution = {
dependencies {
// specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes eg.

runtime "com.yammer.metrics:metrics-core:${yammermetrics.version}"
runtime "com.yammer.metrics:metrics-servlet:${yammermetrics.version}"
runtime "com.codahale.metrics:metrics-core:${yammermetrics.version}"
runtime "com.codahale.metrics:metrics-servlets:${yammermetrics.version}"

}
}
@@ -1,25 +1,25 @@
package org.grails.plugins.yammermetrics.groovy

import com.yammer.metrics.Metrics
import com.codahale.metrics.MetricRegistry
import org.codehaus.groovy.reflection.ReflectionUtils

import java.util.concurrent.TimeUnit
class GroovierMetrics {
static MetricRegistry DEFAULT_METRICS_REGISTRY = new MetricRegistry()

class GroovierMetrics extends Metrics {
// ignore org.springsource.loaded.ri.ReflectiveInterceptor when not running as a war, and ignore this class for convenience
static final List<String> extraIgnoredPackages = ["org.springsource.loaded.ri", "org.grails.plugins.yammermetrics.groovy"]

private GroovierMetrics() { super() }

static com.yammer.metrics.core.Meter newMeter(String meterName){
return newMeter(ReflectionUtils.getCallingClass(0, extraIgnoredPackages), meterName, meterName, TimeUnit.SECONDS)
static com.codahale.metrics.Meter newMeter(String meterName){
return DEFAULT_METRICS_REGISTRY.meter(MetricRegistry.name(ReflectionUtils.getCallingClass(0, extraIgnoredPackages), meterName))
}

static com.yammer.metrics.core.Timer newTimer(String timerName){
return newTimer(ReflectionUtils.getCallingClass(0, extraIgnoredPackages), timerName )
static com.codahale.metrics.Timer newTimer(String timerName){
return DEFAULT_METRICS_REGISTRY.timer(MetricRegistry.name(ReflectionUtils.getCallingClass(0, extraIgnoredPackages), timerName))
}

static com.yammer.metrics.core.Timer newTimer(String className, String timerName){
return newTimer(Class.forName(className), timerName)
static com.codahale.metrics.Timer newTimer(String className, String timerName){
return DEFAULT_METRICS_REGISTRY.timer(MetricRegistry.name(className, timerName))
}
}
@@ -0,0 +1,14 @@
package org.grails.plugins.yammermetrics.groovy

import com.codahale.metrics.health.HealthCheckRegistry
import com.codahale.metrics.servlets.HealthCheckServlet

class HealthCheckServletContextListener extends HealthCheckServlet.ContextListener {

public static final HealthCheckRegistry HEALTH_CHECK_REGISTRY = new HealthCheckRegistry()

@Override
protected HealthCheckRegistry getHealthCheckRegistry() {
return HealthCheckServletContextListener.HEALTH_CHECK_REGISTRY
}
}
@@ -0,0 +1,12 @@
package org.grails.plugins.yammermetrics.groovy

import com.codahale.metrics.MetricRegistry
import com.codahale.metrics.servlets.MetricsServlet

class MetricsServletContextListener extends MetricsServlet.ContextListener {

@Override
protected MetricRegistry getMetricRegistry() {
return GroovierMetrics.DEFAULT_METRICS_REGISTRY
}
}
10 changes: 3 additions & 7 deletions src/groovy/org/grails/plugins/yammermetrics/groovy/Timer.groovy
Expand Up @@ -3,14 +3,11 @@
*/
package org.grails.plugins.yammermetrics.groovy

import com.yammer.metrics.Metrics
import com.yammer.metrics.core.TimerContext
import java.util.concurrent.TimeUnit
import org.apache.commons.logging.LogFactory

class Timer {

@Delegate private com.yammer.metrics.core.Timer timerMetric
@Delegate private com.codahale.metrics.Timer timerMetric
String owner
String name

Expand All @@ -22,13 +19,12 @@ class Timer {
this.name = name

ownerLog = LogFactory.getLog( owner )
timerMetric = Metrics.newTimer( owner, name,
TimeUnit.MILLISECONDS, TimeUnit.SECONDS )
timerMetric = GroovierMetrics.newTimer( owner, name)
}

def time( Closure closure ) {
def oldMax = timerMetric.max()
TimerContext tc = timerMetric.time()
com.codahale.metrics.Timer.Context tc = timerMetric.time()
try {
closure.call()
} finally {
Expand Down
Expand Up @@ -54,7 +54,7 @@ public class YMeteredASTTransformation implements ASTTransformation {
FieldNode timerField = new FieldNode(
meterName,
Modifier.PRIVATE,
new ClassNode(com.yammer.metrics.core.Meter.class),
new ClassNode(com.codahale.metrics.Meter.class),
new ClassNode(classNode.getClass()),
new StaticMethodCallExpression(
new ClassNode(GroovierMetrics.class),
Expand Down
Expand Up @@ -89,7 +89,7 @@ public class YTimedASTTransformation implements ASTTransformation {
FieldNode timerField = new FieldNode(
timerName,
Modifier.PRIVATE,
new ClassNode(com.yammer.metrics.core.Timer.class),
new ClassNode(com.codahale.metrics.Timer.class),
new ClassNode(classNode.getClass()),
new StaticMethodCallExpression(
new ClassNode(GroovierMetrics.class),
Expand Down
@@ -1,15 +1,16 @@
package org.grails.plugins.yammermetrics.reporting

import com.yammer.metrics.reporting.AdminServlet
import com.yammer.metrics.reporting.MetricsServlet
import com.yammer.metrics.reporting.PingServlet
import com.yammer.metrics.reporting.ThreadDumpServlet
import com.codahale.metrics.servlets.AdminServlet
import com.codahale.metrics.servlets.MetricsServlet
import com.codahale.metrics.servlets.PingServlet
import com.codahale.metrics.servlets.ThreadDumpServlet

class GrailsAdminServlet extends AdminServlet {

GrailsAdminServlet() {
super(new GrailsHealthCheckServlet(), new MetricsServlet(), new PingServlet(),
new ThreadDumpServlet(), AdminServlet.DEFAULT_HEALTHCHECK_URI, AdminServlet.DEFAULT_METRICS_URI,
AdminServlet.DEFAULT_PING_URI, AdminServlet.DEFAULT_THREADS_URI);
super()
// super(new GrailsHealthCheckServlet(), new MetricsServlet(), new PingServlet(),
// new ThreadDumpServlet(), AdminServlet.DEFAULT_HEALTHCHECK_URI, AdminServlet.DEFAULT_METRICS_URI,
// AdminServlet.DEFAULT_PING_URI, AdminServlet.DEFAULT_THREADS_URI);
}
}
Expand Up @@ -3,7 +3,7 @@ package org.grails.plugins.yammermetrics.reporting
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import javax.servlet.ServletException
import com.yammer.metrics.reporting.HealthCheckServlet
import com.codahale.metrics.servlets.HealthCheckServlet

class GrailsHealthCheckServlet extends HealthCheckServlet {

Expand Down
@@ -1,6 +1,8 @@
package com.yamme.metrics.groovy
package com.codahale.metrics.groovy

import com.yammer.metrics.core.Meter
import com.codahale.metrics.Meter
import com.codahale.metrics.MetricFilter
import org.grails.plugins.yammermetrics.groovy.GroovierMetrics

/**
* User: GavinHogan@gmail.com
Expand All @@ -15,12 +17,15 @@ class MeterTest extends GroovyTestCase {
sample = new SampleObject()
}

void tearDown() {
GroovierMetrics.DEFAULT_METRICS_REGISTRY.removeMatching(MetricFilter.ALL)
}

void testAddingMeterField(){
def meter = sample.hasProperty('meteredMeter')?sample.meteredMeter : null
assertNotNull("We should generate 'meteredMeter' for metered() :> ${sample.dump()}", meter)
if(!meter instanceof Meter){
fail("We should generate a com.yammer.metrics.core.meter for timed() :> ${sample.dump()}")
fail("We should generate a com.codahale.metrics.meter for timed() :> ${sample.dump()}")
}

}
Expand All @@ -33,42 +38,42 @@ class MeterTest extends GroovyTestCase {
//Not going crazy testing the yammer code, just want to ensure that our AST is functioning.
void testCallingMeteredMethod(){

def init = sample.meteredMeter.count.longValue()
def init = sample.meteredMeter.count
def callCount = 10
callCount.times {
sample.metered()
}
Meter meter = sample.meteredMeter
assertEquals("We should see ${callCount+init} calls on our meter", callCount+init, meter.count() )
assertEquals("We should see ${callCount+init} calls on our meter", callCount+init, meter.count )
}

void testCountsSpanInstances(){
def one = new SampleObject()
def two = new SampleObject()
def meterOne = one.anotherMeteredMeter
def meterTwo = two.anotherMeteredMeter
def initOne = meterOne.count.longValue()
def initTwo = meterTwo.count.longValue()
def initOne = meterOne.count
def initTwo = meterTwo.count
def callCount = 10

10.times{
one.anotherMetered()
two.anotherMetered()
}

assertEquals("We should see ${callCount} calls on our meter", (callCount*2) + initOne, meterOne.count() )
assertEquals("We should see ${callCount} calls on our meter", (callCount*2) + initTwo, meterTwo.count() )
assertEquals("We should see ${callCount} calls on our meter", (callCount*2) + initOne, meterOne.count )
assertEquals("We should see ${callCount} calls on our meter", (callCount*2) + initTwo, meterTwo.count )

}

//Not going crazy testing the yammer code, just want to ensure that our AST is functioning.
void testCallingMultiMetricMethod(){
def init = sample.multiMetricMeter.count.longValue()
def init = sample.multiMetricMeter.count
def callCount = 10
callCount.times {
sample.multiMetric()
}
Meter meter = sample.multiMetricMeter
assertEquals("We should see ${callCount + init} calls on our meter", callCount + init, meter.count() )
assertEquals("We should see ${callCount + init} calls on our meter", callCount + init, meter.count )
}
}
@@ -1,4 +1,4 @@
package com.yamme.metrics.groovy
package com.codahale.metrics.groovy

import org.grails.plugins.yammermetrics.groovy.GroovierMetrics
import org.grails.plugins.yammermetrics.groovy.Metered
Expand Down

0 comments on commit ae98759

Please sign in to comment.