Skip to content
This repository has been archived by the owner on Mar 22, 2021. It is now read-only.

Commit

Permalink
elastic APM and opentracing
Browse files Browse the repository at this point in the history
  • Loading branch information
hb-chen committed May 28, 2019
1 parent ed7eb42 commit 0f5e7b1
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 36 deletions.
1 change: 1 addition & 0 deletions conf/conf.d/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ keys = [
"/session_store",
"/cache_store",
"/app/name",
"/app/version",
"/server/graceful",
"/server/addr",
"/server/domain_api",
Expand Down
5 changes: 3 additions & 2 deletions conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ type config struct {
}

type app struct {
Name string `toml:"name"`
Name string `toml:"name"`
Version string `toml:"version"`
}

type server struct {
Expand Down Expand Up @@ -141,7 +142,7 @@ func InitConfig(configFile string) error {
}

func GetLogLvl() log.Lvl {
//DEBUG INFO WARN ERROR OFF
// DEBUG INFO WARN ERROR OFF
switch Conf.LogLevel {
case "DEBUG":
return log.DEBUG
Expand Down
9 changes: 5 additions & 4 deletions conf/conf.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ cache_store = "REDIS"

[app]
name = "Echo Web"
version = "0.0.1"

[server]
graceful = true
Expand Down Expand Up @@ -51,12 +52,12 @@ pwd = "123456"
server = "localhost:11211"

[opentracing]
disable = true
disable = false

# jaeger or appdash
type = "jaeger"
# "jaeger" "appdash" or "elastic"
type = "elastic"

# jaeger serviceName
# jaeger or elastic serviceName
service_name = "echo-web"

# jaeger-agent 127.0.0.1:6831
Expand Down
1 change: 1 addition & 0 deletions conf/templates/config.conf.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ cache_store = "{{getv "/cache_store"}}"

[app]
name = "{{getv "/app/name"}}"
version = "{{getv "/app/version"}}"

[server]
graceful = {{getv "/server/graceful"}}
Expand Down
25 changes: 21 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ require (
github.com/dchest/captcha v0.0.0-20170622155422-6a29415a8364
github.com/denisenkom/go-mssqldb v0.0.0-20181014144952-4e0d7dc8888f // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/elastic/go-sysinfo v1.0.1 // indirect
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 // indirect
github.com/flosch/pongo2 v0.0.0-20180809100617-24195e6d38b0
github.com/go-sql-driver/mysql v1.4.1
github.com/gomodule/redigo v2.0.0+incompatible
github.com/google/go-cmp v0.3.0 // indirect
github.com/gorilla/context v1.1.1
github.com/gorilla/mux v1.6.2 // indirect
github.com/gorilla/sessions v1.1.3
Expand All @@ -25,23 +27,38 @@ require (
github.com/juju/errors v0.0.0-20181118221551-089d3ea4e4d5 // indirect
github.com/juju/loggo v0.0.0-20180524022052-584905176618 // indirect
github.com/juju/testing v0.0.0-20180920084828-472a3e8b2073 // indirect
github.com/labstack/echo v0.0.0-20181123063703-c7eb8da9ec73
github.com/labstack/gommon v0.2.7
github.com/kr/pty v1.1.4 // indirect
github.com/labstack/echo v3.3.10+incompatible
github.com/labstack/gommon v0.2.8
github.com/mattn/go-colorable v0.1.2 // indirect
github.com/mattn/go-sqlite3 v1.10.0 // indirect
github.com/opentracing/basictracer-go v1.0.0 // indirect
github.com/opentracing/opentracing-go v1.0.2
github.com/opentracing/opentracing-go v1.1.0
github.com/prometheus/client_golang v0.9.1
github.com/prometheus/common v0.0.0-20181120120127-aeab699e26f4 // indirect
github.com/prometheus/procfs v0.0.0-20190523193104-a7aeb8df3389 // indirect
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a
github.com/robfig/go-cache v0.0.0-20130306151617-9fc39e0dbf62
github.com/santhosh-tekuri/jsonschema v1.2.4 // indirect
github.com/shurcooL/httpfs v0.0.0-20171119174359-809beceb2371 // indirect
github.com/shurcooL/vfsgen v0.0.0-20181020040650-a97a25d856ca // indirect
github.com/stretchr/objx v0.2.0 // indirect
github.com/uber-go/atomic v1.3.2 // indirect
github.com/uber/jaeger-client-go v2.15.0+incompatible
github.com/uber/jaeger-lib v1.5.0
github.com/valyala/fasttemplate v1.0.1 // indirect
github.com/vrischmann/go-metrics-influxdb v0.0.0-20160917065939-43af8332c303
golang.org/x/net v0.0.0-20181023162649-9b4f9f5ad519
go.elastic.co/apm v1.3.0
go.elastic.co/apm/module/apmecho v1.3.0
go.elastic.co/apm/module/apmhttp v1.3.0
go.elastic.co/apm/module/apmot v1.3.0
golang.org/x/crypto v0.0.0-20190513172903-22d7a77e9e5f // indirect
golang.org/x/net v0.0.0-20190522155817-f3200d17e092
golang.org/x/sys v0.0.0-20190528012530-adf421d2caf4 // indirect
golang.org/x/text v0.3.2 // indirect
golang.org/x/tools v0.0.0-20190525145741-7be61e1b0e51 // indirect
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce // indirect
gopkg.in/yaml.v2 v2.2.2 // indirect
sourcegraph.com/sourcegraph/appdash v0.0.0-20180531100431-4c381bd170b4
sourcegraph.com/sourcegraph/appdash-data v0.0.0-20151005221446-73f23eafcf67 // indirect
)
91 changes: 82 additions & 9 deletions go.sum

Large diffs are not rendered by default.

23 changes: 17 additions & 6 deletions middleware/opentracing/opentracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package opentracing

import (
"fmt"
"go.elastic.co/apm"
"go.elastic.co/apm/module/apmot"
"io"
"net"
"net/http"
"net/url"
"io"

"github.com/labstack/echo"
"github.com/labstack/gommon/log"
Expand All @@ -18,17 +20,19 @@ import (
"sourcegraph.com/sourcegraph/appdash/traceapp"
)

type TracerType string

const (
DefaultKey = "github.com/hb-go/echo-web/middleware/opentracing"

TracerTypeJaeger = "jaeger"

TracerTypeAppdash = "appdash"
TracerTypeJaeger TracerType = "jaeger"
TracerTypeAppdash = "appdash"
TracerTypeElastic = "elastic"
)

type Configuration struct {
Disabled bool
Type string
Type TracerType
}

func (c Configuration) InitGlobalTracer(options ...Option) io.Closer {
Expand All @@ -43,6 +47,9 @@ func (c Configuration) InitGlobalTracer(options ...Option) io.Closer {
return nil
case TracerTypeJaeger:
return initGlobalTracer_Jaeger(opts.ServiceName, opts.Address)
case TracerTypeElastic:
initGlobalTracer_Elastic(opts.ServiceName)
return nil
default:
return nil
}
Expand Down Expand Up @@ -91,7 +98,6 @@ func initGlobalTracer_Jaeger(serviceName, addr string) io.Closer {
log.Printf("could not initialize jaeger tracer: %s", err.Error())
return nil
}
//defer closer.Close()
return closer
}

Expand Down Expand Up @@ -138,6 +144,11 @@ func initGlobalTracer_Appdash(addr string) {
opentracing.InitGlobalTracer(tracer)
}

func initGlobalTracer_Elastic(serviceName string) {
apm.DefaultTracer.Service.Name = serviceName
opentracing.InitGlobalTracer(apmot.New())
}

func OpenTracing(comp string) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
Expand Down
10 changes: 5 additions & 5 deletions middleware/opentracing/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ type Option func(c *Options)

type Options struct {
ServiceName string
Address string
Address string
}

func ServiceName(serviceName string) Option {
Expand All @@ -19,7 +19,7 @@ func Address(address string) Option {
}
}

func applyOptions(otTyue string, options ...Option) Options {
func applyOptions(otType TracerType, options ...Option) Options {
opts := Options{}
for _, option := range options {
option(&opts)
Expand All @@ -29,10 +29,10 @@ func applyOptions(otTyue string, options ...Option) Options {
opts.ServiceName = "echo-web"
}

//jaeger-agent 127.0.0.1:6831
//appdash http://localhost:8700
// jaeger-agent 127.0.0.1:6831
// appdash http://localhost:8700
if len(opts.Address) == 0 {
switch otTyue {
switch otType {
case TracerTypeAppdash:
opts.Address = "http://localhost:8700"
case TracerTypeJaeger:
Expand Down
15 changes: 14 additions & 1 deletion router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package router

import (
"context"
"net/http"
"net/url"
"os"
"os/signal"
"time"

"github.com/labstack/echo"
mw "github.com/labstack/echo/middleware"
"go.elastic.co/apm"
"go.elastic.co/apm/module/apmecho"

. "github.com/hb-go/echo-web/conf"
"github.com/hb-go/echo-web/middleware/metrics/prometheus"
Expand Down Expand Up @@ -55,10 +58,20 @@ func RunSubdomains(confFilePath string) {

e.Pre(mw.RemoveTrailingSlash())

// Elastic APM
// Requires APM Server 6.5.0 or newer
apm.DefaultTracer.Service.Name = Conf.Opentracing.ServiceName
apm.DefaultTracer.Service.Version = Conf.App.Version
e.Use(apmecho.Middleware(
apmecho.WithRequestIgnorer(func(request *http.Request) bool {
return false
}),
))

// OpenTracing
otCtf := opentracing.Configuration{
Disabled: Conf.Opentracing.Disable,
Type: Conf.Opentracing.Type,
Type: opentracing.TracerType(Conf.Opentracing.Type),
}
if closer := otCtf.InitGlobalTracer(
opentracing.ServiceName(Conf.Opentracing.ServiceName),
Expand Down
9 changes: 4 additions & 5 deletions router/web/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@ import (
"github.com/labstack/echo"
mw "github.com/labstack/echo/middleware"

"github.com/hb-go/echo-web/middleware/captcha"
"github.com/hb-go/echo-web/middleware/staticbin"

"github.com/hb-go/echo-web/assets"
. "github.com/hb-go/echo-web/conf"
"github.com/hb-go/echo-web/middleware/captcha"
"github.com/hb-go/echo-web/middleware/opentracing"
"github.com/hb-go/echo-web/middleware/staticbin"
"github.com/hb-go/echo-web/model"
"github.com/hb-go/echo-web/module/auth"
"github.com/hb-go/echo-web/module/cache"
"github.com/hb-go/echo-web/module/render"
"github.com/hb-go/echo-web/module/session"
)

//---------
// ---------
// Website Routers
//---------
// ---------
func Routers() *echo.Echo {
// Echo instance
e := echo.New()
Expand Down

0 comments on commit 0f5e7b1

Please sign in to comment.