Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to Brave4 and use Tracing instead of Brave instance #30

Merged
merged 1 commit into from
Jun 10, 2017
Merged
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
32 changes: 16 additions & 16 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@ name := "resty"

organization := "com.github.takezoe"

version := "0.0.10"
version := "0.0.11-SNAPSHOT"

scalaVersion := "2.12.1"

libraryDependencies ++= Seq(
"org.json4s" %% "json4s-scalap" % "3.5.1",
"com.netflix.hystrix" % "hystrix-core" % "1.5.10",
"com.netflix.hystrix" % "hystrix-metrics-event-stream" % "1.5.10",
"com.fasterxml.jackson.module" %% "jackson-module-scala" % "2.8.8",
"io.swagger" % "swagger-models" % "1.5.13",
"io.zipkin.brave" % "brave-core" % "3.18.0",
"io.zipkin.brave" % "brave-apache-http-interceptors" % "3.18.0",
"io.zipkin.brave" % "brave-web-servlet-filter" % "3.18.0",
"io.zipkin.reporter" % "zipkin-sender-okhttp3" % "0.6.13",
"org.apache.httpcomponents" % "httpclient" % "4.5.3",
"commons-io" % "commons-io" % "2.5",
"org.webjars" % "webjars-locator" % "0.32-1",
"ch.qos.logback" % "logback-classic" % "1.2.2",
"javax.servlet" % "javax.servlet-api" % "3.1.0" % "provided",
"org.scalatest" %% "scalatest" % "3.0.1" % "test"
"org.json4s" %% "json4s-scalap" % "3.5.1",
"com.netflix.hystrix" % "hystrix-core" % "1.5.10",
"com.netflix.hystrix" % "hystrix-metrics-event-stream" % "1.5.10",
"com.fasterxml.jackson.module" %% "jackson-module-scala" % "2.8.8",
"io.swagger" % "swagger-models" % "1.5.13",
"io.zipkin.brave" % "brave" % "4.3.3",
"io.zipkin.brave" % "brave-instrumentation-httpclient" % "4.3.3",
"io.zipkin.brave" % "brave-instrumentation-servlet" % "4.3.3",
"io.zipkin.reporter" % "zipkin-sender-okhttp3" % "0.6.13",
"org.apache.httpcomponents" % "httpclient" % "4.5.3",
"commons-io" % "commons-io" % "2.5",
"org.webjars" % "webjars-locator" % "0.32-1",
"ch.qos.logback" % "logback-classic" % "1.2.2",
"javax.servlet" % "javax.servlet-api" % "3.1.0" % "provided",
"org.scalatest" %% "scalatest" % "3.0.1" % "test"
)

scalacOptions := Seq("-deprecation")
Expand Down
26 changes: 13 additions & 13 deletions src/main/scala/com/github/takezoe/resty/HttpClientSupport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package com.github.takezoe.resty
import java.util.concurrent.atomic.AtomicReference
import javax.servlet.ServletContextEvent

import com.github.kristofa.brave.{Brave, Sampler}
import com.github.kristofa.brave.httpclient.{BraveHttpRequestInterceptor, BraveHttpResponseInterceptor}
import com.github.takezoe.resty.servlet.ConfigKeys
import com.github.takezoe.resty.util.{JsonUtils, StringUtils}
import org.apache.commons.io.IOUtils
Expand All @@ -14,16 +12,19 @@ import org.apache.http.entity.ContentType
import org.apache.http.impl.client.{CloseableHttpClient, HttpClients}
import zipkin.reporter.AsyncReporter
import zipkin.reporter.okhttp3.OkHttpSender
import brave._
import brave.sampler._
import brave.httpclient._

import scala.reflect.ClassTag

object HttpClientSupport {

private val _brave = new AtomicReference[Brave](null)
private val _tracing = new AtomicReference[Tracing](null)
private val _httpClient = new AtomicReference[CloseableHttpClient](null)

def brave: Brave = {
val instance = _brave.get()
def tracing: Tracing = {
val instance = _tracing.get()
if(instance == null){
throw new IllegalStateException("HttpClientSupport has not been initialized or Zipkin support is disabled.")
}
Expand All @@ -47,22 +48,21 @@ object HttpClientSupport {
val name = sce.getServletContext.getServletContextName
val url = StringUtils.trim(sce.getServletContext.getInitParameter(ConfigKeys.ZipkinServerUrl))
val rate = StringUtils.trim(sce.getServletContext.getInitParameter(ConfigKeys.ZipkinSampleRate))
val builder = new Brave.Builder(name)
val builder = Tracing.newBuilder().localServiceName(name)

if(url.nonEmpty){
val reporter = AsyncReporter.builder(OkHttpSender.create(url.trim)).build()
builder.reporter(reporter)
}

if(rate.nonEmpty){
val sampler = Sampler.create(rate.toFloat)
builder.traceSampler(sampler)
builder.sampler(sampler)
}
_brave.set(builder.build())

_httpClient.set(HttpClients.custom()
.addInterceptorFirst(BraveHttpRequestInterceptor.create(HttpClientSupport.brave))
.addInterceptorFirst(BraveHttpResponseInterceptor.create(HttpClientSupport.brave))
.build())
_tracing.set(builder.build())
_httpClient.set(TracingHttpClientBuilder.create(_tracing.get()).build())

} else {
_httpClient.set(HttpClients.createDefault())
}
Expand All @@ -72,7 +72,7 @@ object HttpClientSupport {
if(_httpClient.get() == null){
throw new IllegalArgumentException("HttpClientSupport is inactive now.")
}
_brave.set(null)
_tracing.set(null)
_httpClient.get().close()
_httpClient.set(null)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package com.github.takezoe.resty.servlet

import javax.servlet._

import com.github.kristofa.brave.servlet.BraveServletFilter
import brave.servlet.TracingFilter
import com.github.takezoe.resty.HttpClientSupport

class ZipkinBraveFilter extends Filter {

protected val filter = BraveServletFilter.create(HttpClientSupport.brave)
protected val filter = TracingFilter.create(HttpClientSupport.tracing)

override def init(filterConfig: FilterConfig): Unit = filter.init(filterConfig)

Expand Down