Skip to content

Commit

Permalink
Adding zipkin integration
Browse files Browse the repository at this point in the history
  • Loading branch information
ctoestreich committed Jul 25, 2016
1 parent d70feb9 commit 8ab24b0
Show file tree
Hide file tree
Showing 17 changed files with 117 additions and 27 deletions.
7 changes: 5 additions & 2 deletions gr8-rest-consumer/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ ext {
repositories {
mavenLocal()
maven { url "https://repo.grails.org/grails/core" }
maven { url 'https://repo.spring.io/libs-snapshot' }
}

dependencyManagement {
Expand All @@ -49,9 +50,11 @@ dependencies {
compile "org.grails:grails-core"
compile "org.springframework.boot:spring-boot-starter-logging"
compile "org.springframework.boot:spring-boot-autoconfigure"
compile "org.springframework.boot:spring-boot-starter-amqp" //AMQP
compile "org.springframework.boot:spring-boot-starter-amqp"
compile "org.springframework.boot:spring-boot-starter-actuator"
compile "org.springframework.boot:spring-boot-starter-tomcat"
compile 'org.springframework.cloud:spring-cloud-sleuth-core:1.0.3.RELEASE'
compile 'org.springframework.cloud:spring-cloud-sleuth-zipkin:1.0.3.RELEASE'
compile "org.grails:grails-plugin-url-mappings"
compile "org.grails:grails-plugin-rest"
compile "org.grails:grails-plugin-codecs"
Expand All @@ -62,7 +65,7 @@ dependencies {
compile "org.grails:grails-plugin-async"
compile "org.grails:grails-web-boot"
compile "org.grails:grails-logging"
compile "org.grails.plugins:cache"
// compile "org.grails.plugins:cache"
compile "org.grails.plugins:hibernate4"
compile "org.hibernate:hibernate-ehcache"
compile "org.grails.plugins:views-json"
Expand Down
Binary file modified gr8-rest-consumer/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gr8-rest-consumer/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Mon Jan 11 13:11:34 CST 2016
#Mon Jul 25 10:23:01 CDT 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
Expand Down
10 changes: 3 additions & 7 deletions gr8-rest-consumer/gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ case "`uname`" in
;;
esac

# For Cygwin, ensure paths are in UNIX format before anything is touched.
if $cygwin ; then
[ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
fi

# Attempt to set APP_HOME
# Resolve links: $0 may be a link
PRG="$0"
Expand All @@ -61,9 +56,9 @@ while [ -h "$PRG" ] ; do
fi
done
SAVED="`pwd`"
cd "`dirname \"$PRG\"`/" >&-
cd "`dirname \"$PRG\"`/" >/dev/null
APP_HOME="`pwd -P`"
cd "$SAVED" >&-
cd "$SAVED" >/dev/null

CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar

Expand Down Expand Up @@ -114,6 +109,7 @@ fi
if $cygwin ; then
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
JAVACMD=`cygpath --unix "$JAVACMD"`

# We build the pattern for arguments to be converted via cygpath
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
Expand Down
14 changes: 14 additions & 0 deletions gr8-rest-consumer/grails-app/conf/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@ rabbit.host.name: localhost

spring.cloud.zookeeper.connect-string: localhost:2181

---
spring:
zipkin:
# Uncomment to send to zipkin, replacing 192.168.99.100 with your zipkin IP address
# baseUrl: http://192.168.99.100:9411/
sleuth:
sampler:
percentage: 1.0

sample:
zipkin:
# When enabled=false, traces log to the console. Comment to send to zipkin
enabled: false

logging.level.kafka: WARN

---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,24 @@ import gr8.rest.api.config.RabbitConfiguration
import grails.boot.GrailsApp
import grails.boot.config.GrailsAutoConfiguration
import grails.converters.JSON
import groovy.util.logging.Slf4j
import org.springframework.amqp.rabbit.annotation.EnableRabbit
import org.springframework.amqp.rabbit.annotation.RabbitListener
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.EnableAspectJAutoProxy
import org.springframework.context.annotation.Import
import org.springframework.cloud.sleuth.zipkin.ZipkinSpanReporter
import org.springframework.scheduling.annotation.EnableAsync
import zipkin.Span

import javax.annotation.PostConstruct

@Import([RabbitConfiguration, KafkaConfig])
@EnableRabbit
@EnableAspectJAutoProxy(proxyTargetClass = true)
@EnableAsync
@Slf4j
class Application extends GrailsAutoConfiguration {
static void main(String[] args) {
GrailsApp.run(Application, args)
Expand All @@ -23,6 +32,17 @@ class Application extends GrailsAutoConfiguration {
Receiver receiver() {
new Receiver()
}

@Bean
@ConditionalOnProperty(value = "sample.zipkin.enabled", havingValue = "false")
public ZipkinSpanReporter spanCollector() {
return new ZipkinSpanReporter() {
@Override
public void report(Span span) {
log.info(String.format("Consumer reporting span [%s]", span));
}
};
}
}

class Receiver {
Expand Down
8 changes: 6 additions & 2 deletions gr8-rest-producer/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ ext {
repositories {
mavenLocal()
maven { url "https://repo.grails.org/grails/core" }
maven { url 'https://repo.spring.io/libs-snapshot' }
}

dependencyManagement {
Expand All @@ -51,7 +52,10 @@ dependencies {
compile "org.grails:grails-core"
compile "org.springframework.boot:spring-boot-starter-actuator"
compile "org.springframework.boot:spring-boot-starter-tomcat"
compile "org.springframework.boot:spring-boot-starter-amqp" //AMQP
compile "org.springframework.boot:spring-boot-starter-amqp"
compile 'org.springframework.cloud:spring-cloud-sleuth-core:1.0.3.RELEASE'
compile 'org.springframework.cloud:spring-cloud-sleuth-zipkin:1.0.3.RELEASE'
// compile 'org.springframework.cloud:spring-cloud-sleuth:1.1.0.BUILD-SNAPSHOT'
compile "org.grails:grails-plugin-url-mappings"
compile "org.grails:grails-plugin-rest"
compile "org.grails:grails-plugin-codecs"
Expand All @@ -62,7 +66,7 @@ dependencies {
compile "org.grails:grails-plugin-async"
compile "org.grails:grails-web-boot"
compile "org.grails:grails-logging"
compile "org.grails.plugins:cache"
// compile "org.grails.plugins:cache"
compile "org.grails.plugins:hibernate4"
compile "org.hibernate:hibernate-ehcache"
compile "org.grails.plugins:views-json"
Expand Down
Binary file modified gr8-rest-producer/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gr8-rest-producer/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Mon Jan 11 13:11:34 CST 2016
#Mon Jul 25 10:23:01 CDT 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
Expand Down
10 changes: 3 additions & 7 deletions gr8-rest-producer/gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ case "`uname`" in
;;
esac

# For Cygwin, ensure paths are in UNIX format before anything is touched.
if $cygwin ; then
[ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
fi

# Attempt to set APP_HOME
# Resolve links: $0 may be a link
PRG="$0"
Expand All @@ -61,9 +56,9 @@ while [ -h "$PRG" ] ; do
fi
done
SAVED="`pwd`"
cd "`dirname \"$PRG\"`/" >&-
cd "`dirname \"$PRG\"`/" >/dev/null
APP_HOME="`pwd -P`"
cd "$SAVED" >&-
cd "$SAVED" >/dev/null

CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar

Expand Down Expand Up @@ -114,6 +109,7 @@ fi
if $cygwin ; then
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
JAVACMD=`cygpath --unix "$JAVACMD"`

# We build the pattern for arguments to be converted via cygpath
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
Expand Down
16 changes: 16 additions & 0 deletions gr8-rest-producer/grails-app/conf/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@ brokerList: localhost:9092
sync: async
topic: person

---
spring:
zipkin:
# Uncomment to send to zipkin, replacing 192.168.99.100 with your zipkin IP address
# baseUrl: http://192.168.99.100:9411/
sleuth:
sampler:
percentage: 1.0

sample:
zipkin:
# When enabled=false, traces log to the console. Comment to send to zipkin
enabled: false

logging.level.kafka: WARN

---
hibernate:
cache:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package gr8.rest.producer

import gr8.rest.api.config.RabbitConfiguration
import grails.converters.JSON
import grails.rest.RestfulController
import grails.transaction.Transactional
import org.springframework.beans.factory.annotation.Autowired
import reactor.spring.context.annotation.Consumer
import reactor.spring.context.annotation.Selector

import static org.springframework.http.HttpStatus.*
import static org.springframework.http.HttpStatus.CREATED
import static org.springframework.http.HttpStatus.NOT_FOUND

@Transactional(readOnly = false)
@Consumer
class PersonController extends RestfulController {

@Autowired
Expand Down Expand Up @@ -46,8 +49,15 @@ class PersonController extends RestfulController {

person.save(flush: true)

//fire some events
//notify "person:saved", (person as JSON).toString()
eventEmitterService.emitEvent((person as JSON).toString())

respond person, [status: CREATED, view: "show"]
}

@Selector("person:saved")
public consumePersonSavedEvent(String json) {
log.error "Internal person saved event with data $json"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,34 @@ import gr8.rest.api.config.KafkaConfiguration
import gr8.rest.api.config.RabbitConfiguration
import grails.boot.GrailsApp
import grails.boot.config.GrailsAutoConfiguration
import groovy.util.logging.Slf4j
import org.springframework.amqp.rabbit.annotation.EnableRabbit
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.EnableAspectJAutoProxy
import org.springframework.context.annotation.Import
import org.springframework.cloud.sleuth.zipkin.ZipkinSpanReporter
import org.springframework.scheduling.annotation.EnableAsync
import zipkin.Span;

@Import([RabbitConfiguration, KafkaConfiguration])
@EnableRabbit
@EnableAspectJAutoProxy(proxyTargetClass = true)
@EnableAsync
@Slf4j
class Application extends GrailsAutoConfiguration {
static void main(String[] args) {
GrailsApp.run(Application, args)
}

@Bean
@ConditionalOnProperty(value = "sample.zipkin.enabled", havingValue = "false")
public ZipkinSpanReporter spanCollector() {
return new ZipkinSpanReporter() {
@Override
public void report(Span span) {
log.info(String.format("Producer reporting span [%s]", span));
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class EventEmitterService {

public void emitEvent(String messageBody) {
//emit a kafka event to the producer
log.info("You are going to emit an event to Kafka!")
try {
if (kafkaEnabled) {
springBootKafkaProducer.send(messageBody);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,14 @@ private void sendAsync(String value) {
ProducerRecord<String, String> record = new ProducerRecord<>(topic, value);

producer.send(record, (RecordMetadata recordMetadata, Exception e) -> {
logger.info("Received callback data: " + recordMetadata.toString());
if (e != null) {
//This will trip on grails development mode due to spring reloader. Not sure why
logger.info("Received exception on callback: " + e.getMessage());
if(recordMetadata != null) {
logger.info("Received callback data: " + recordMetadata.toString());
if (e != null) {
//This will trip on grails development mode due to spring reloader. Not sure why
logger.info("Received exception on callback: " + e.getMessage());
}
} else {
logger.info("Received null record on callback");
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Fri Nov 27 23:09:32 CET 2015
#Mon Jul 25 10:23:01 CDT 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
Expand Down
5 changes: 5 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ To put a message onto the RabbitMQ and/or the Kafka topic simply use your favori
curl -X POST -H "Content-type: application/json" --data '{"name": "Christian"}' http://localhost:8080/person
```

Docker Machine
```bash
curl -X POST -H "Content-type: application/json" --data '{"name": "Christian"}' http://192.168.99.100:8080/person
```

Notes
---
You may see a `java.lang.RuntimeException: Reloading agent exited via exception, please raise a jira` exception during the first post to kafka when running in `run-app` or `bootRun` if using async. This can be ignored and appears to not cause any data issues.

0 comments on commit 8ab24b0

Please sign in to comment.