Skip to content
This repository has been archived by the owner on Dec 5, 2023. It is now read-only.

Commit

Permalink
Merge pull request #22 from microservices-demo/enhancement/opentracing
Browse files Browse the repository at this point in the history
Enhancement/opentracing
  • Loading branch information
jasonrichardsmith authored Dec 23, 2016
2 parents f4901df + d0f0044 commit 9e4174b
Show file tree
Hide file tree
Showing 6 changed files with 335 additions and 15 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,23 @@ To run it use:

### Push the service to Docker Container Registry
`GROUP=weaveworksdemos COMMIT=test ./scripts/push.sh`

## Test Zipkin

To test with Zipkin

```
docker-compose -f docker-compose-zipkin.yml build
docker-compose -f docker-compose-zipkin.yml up
```
It takes about 10 seconds to seed data

you should see it at:
[http://localhost:9411/](http://localhost:9411)

be sure to hit the "Find Traces" button. You may need to reload the page.

when done you can run:
```
docker-compose -f docker-compose-zipkin.yml down
```
40 changes: 37 additions & 3 deletions cmd/cataloguesvc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,30 @@ import (

"github.com/go-kit/kit/log"
kitprometheus "github.com/go-kit/kit/metrics/prometheus"
stdopentracing "github.com/opentracing/opentracing-go"
zipkin "github.com/openzipkin/zipkin-go-opentracing"
stdprometheus "github.com/prometheus/client_golang/prometheus"

"net/http"

"path/filepath"

_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
"github.com/microservices-demo/catalogue"
"golang.org/x/net/context"
"path/filepath"
)

const (
ServiceName = "catalogue"
)

func main() {
var (
port = flag.String("port", "8081", "Port to bind HTTP listener") // TODO(pb): should be -addr, default ":8081"
images = flag.String("images", "./images/", "Image path")
dsn = flag.String("DSN", "catalogue_user:default_password@tcp(catalogue-db:3306)/socksdb", "Data Source Name: [username[:password]@][protocol[(address)]]/dbname")
zip = flag.String("zipkin", os.Getenv("ZIPKIN"), "Zipkin address")
)
flag.Parse()

Expand All @@ -48,6 +56,32 @@ func main() {
logger = log.NewContext(logger).With("caller", log.DefaultCaller)
}

var tracer stdopentracing.Tracer
{
if *zip == "" {
tracer = stdopentracing.NoopTracer{}
} else {
logger := log.NewContext(logger).With("tracer", "Zipkin")
logger.Log("addr", zip)
collector, err := zipkin.NewHTTPCollector(
*zip,
zipkin.HTTPLogger(logger),
)
if err != nil {
logger.Log("err", err)
os.Exit(1)
}
tracer, err = zipkin.NewTracer(
zipkin.NewRecorder(collector, false, fmt.Sprintf("localhost:%v", port), ServiceName),
)
if err != nil {
logger.Log("err", err)
os.Exit(1)
}
}
stdopentracing.InitGlobalTracer(tracer)
}

// Data domain.
db, err := sqlx.Open("mysql", *dsn)
if err != nil {
Expand Down Expand Up @@ -88,12 +122,12 @@ func main() {
}

// Endpoint domain.
endpoints := catalogue.MakeEndpoints(service)
endpoints := catalogue.MakeEndpoints(service, tracer)

// Create and launch the HTTP server.
go func() {
logger.Log("transport", "HTTP", "port", *port)
handler := catalogue.MakeHTTPHandler(ctx, endpoints, *images, logger)
handler := catalogue.MakeHTTPHandler(ctx, endpoints, *images, logger, tracer)
errc <- http.ListenAndServe(":"+*port, handler)
}()

Expand Down
52 changes: 52 additions & 0 deletions docker-compose-zipkin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
version: '2'

services:
catalogue:
build:
context: .
dockerfile: ./docker/catalogue/Dockerfile
image: weaveworksdemos/catalogue-dev
hostname: catalogue
restart: always
cap_drop:
- all
cap_add:
- NET_BIND_SERVICE
read_only: true
environment:
- ZIPKIN=http://zipkin:9411/api/v1/spans
- reschedule=on-node-failure
ports:
- "8080:80"
catalogue-db:
build:
context: ./docker/catalogue-db/
image: weaveworksdemos/catalogue-dev-db
hostname: catalogue-db
restart: always
environment:
- reschedule=on-node-failure
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
- MYSQL_ALLOW_EMPTY_PASSWORD=true
- MYSQL_DATABASE=socksdb
zipkin:
image: openzipkin/zipkin
hostname: zipkin
restart: always
cap_drop:
- all
cap_add:
- CHOWN
- SETGID
- SETUID
read_only: true
tmpfs:
- /tmp:rw,noexec,nosuid
environment:
- reschedule=on-node-failure
ports:
- "9411:9411"
zipkinseed:
image: alpine
command: /bin/sh -c 'sleep 10 ; wget http://catalogue/health ; wget http://catalogue/catalogue ; wget http://catalogue/catalogue/size ; wget http://catalogue/tags'

14 changes: 8 additions & 6 deletions endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package catalogue

import (
"github.com/go-kit/kit/endpoint"
"github.com/go-kit/kit/tracing/opentracing"
stdopentracing "github.com/opentracing/opentracing-go"
"golang.org/x/net/context"
)

Expand All @@ -20,13 +22,13 @@ type Endpoints struct {

// MakeEndpoints returns an Endpoints structure, where each endpoint is
// backed by the given service.
func MakeEndpoints(s Service) Endpoints {
func MakeEndpoints(s Service, tracer stdopentracing.Tracer) Endpoints {
return Endpoints{
ListEndpoint: MakeListEndpoint(s),
CountEndpoint: MakeCountEndpoint(s),
GetEndpoint: MakeGetEndpoint(s),
TagsEndpoint: MakeTagsEndpoint(s),
HealthEndpoint: MakeHealthEndpoint(s),
ListEndpoint: opentracing.TraceServer(tracer, "GET /catalogue")(MakeListEndpoint(s)),
CountEndpoint: opentracing.TraceServer(tracer, "GET /catalogue/size")(MakeCountEndpoint(s)),
GetEndpoint: opentracing.TraceServer(tracer, "GET /catalogue/{id}")(MakeGetEndpoint(s)),
TagsEndpoint: opentracing.TraceServer(tracer, "GET /tags")(MakeTagsEndpoint(s)),
HealthEndpoint: opentracing.TraceServer(tracer, "GET /health")(MakeHealthEndpoint(s)),
}
}

Expand Down
14 changes: 8 additions & 6 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ import (
"strings"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/tracing/opentracing"
httptransport "github.com/go-kit/kit/transport/http"
"github.com/gorilla/mux"
stdopentracing "github.com/opentracing/opentracing-go"
"github.com/prometheus/client_golang/prometheus/promhttp"
"golang.org/x/net/context"
)

// MakeHTTPHandler mounts the endpoints into a REST-y HTTP handler.
func MakeHTTPHandler(ctx context.Context, e Endpoints, imagePath string, logger log.Logger) http.Handler {
func MakeHTTPHandler(ctx context.Context, e Endpoints, imagePath string, logger log.Logger, tracer stdopentracing.Tracer) http.Handler {
r := mux.NewRouter().StrictSlash(false)
options := []httptransport.ServerOption{
httptransport.ServerErrorLogger(logger),
Expand All @@ -35,28 +37,28 @@ func MakeHTTPHandler(ctx context.Context, e Endpoints, imagePath string, logger
e.ListEndpoint,
decodeListRequest,
encodeListResponse,
options...,
append(options, httptransport.ServerBefore(opentracing.FromHTTPRequest(tracer, "GET /catalogue", logger)))...,
))
r.Methods("GET").Path("/catalogue/size").Handler(httptransport.NewServer(
ctx,
e.CountEndpoint,
decodeCountRequest,
encodeResponse,
options...,
append(options, httptransport.ServerBefore(opentracing.FromHTTPRequest(tracer, "GET /catalogue/size", logger)))...,
))
r.Methods("GET").Path("/catalogue/{id}").Handler(httptransport.NewServer(
ctx,
e.GetEndpoint,
decodeGetRequest,
encodeGetResponse, // special case, this one can have an error
options...,
append(options, httptransport.ServerBefore(opentracing.FromHTTPRequest(tracer, "GET /catalogue/{id}", logger)))...,
))
r.Methods("GET").Path("/tags").Handler(httptransport.NewServer(
ctx,
e.TagsEndpoint,
decodeTagsRequest,
encodeResponse,
options...,
append(options, httptransport.ServerBefore(opentracing.FromHTTPRequest(tracer, "GET /tags", logger)))...,
))
r.Methods("GET").PathPrefix("/catalogue/images/").Handler(http.StripPrefix(
"/catalogue/images/",
Expand All @@ -67,7 +69,7 @@ func MakeHTTPHandler(ctx context.Context, e Endpoints, imagePath string, logger
e.HealthEndpoint,
decodeHealthRequest,
encodeHealthResponse,
options...,
append(options, httptransport.ServerBefore(opentracing.FromHTTPRequest(tracer, "GET /health", logger)))...,
))
r.Handle("/metrics", promhttp.Handler())
return r
Expand Down
Loading

0 comments on commit 9e4174b

Please sign in to comment.