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 #18 from microservices-demo/enhancement/opentracing
Browse files Browse the repository at this point in the history
added opentracing with a sample docker-compose
  • Loading branch information
jasonrichardsmith authored Jan 2, 2017
2 parents 5fb09ae + f6ca332 commit 12fc91f
Show file tree
Hide file tree
Showing 7 changed files with 336 additions and 26 deletions.
47 changes: 44 additions & 3 deletions cmd/paymentsvc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,67 @@ package main
import (
"flag"
"fmt"
"github.com/microservices-demo/payment"
"golang.org/x/net/context"
"net/http"
"os"
"os/signal"
"syscall"

"github.com/go-kit/kit/log"
"github.com/microservices-demo/payment"
stdopentracing "github.com/opentracing/opentracing-go"
zipkin "github.com/openzipkin/zipkin-go-opentracing"
"golang.org/x/net/context"
)

const (
ServiceName = "payment"
)

func main() {
var (
port = flag.String("port", "8080", "Port to bind HTTP listener")
zip = flag.String("zipkin", os.Getenv("ZIPKIN"), "Zipkin address")
declineAmount = flag.Float64("decline", 100, "Decline payments over certain amount")
)
flag.Parse()
var tracer stdopentracing.Tracer
{
// Log domain.
var logger log.Logger
{
logger = log.NewLogfmtLogger(os.Stderr)
logger = log.NewContext(logger).With("ts", log.DefaultTimestampUTC)
logger = log.NewContext(logger).With("caller", log.DefaultCaller)
}
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)

}
// Mechanical stuff.
errc := make(chan error)
ctx := context.Background()

handler, logger := payment.WireUp(ctx, float32(*declineAmount))
handler, logger := payment.WireUp(ctx, float32(*declineAmount), tracer)

// Create and launch the HTTP server.
go func() {
Expand Down
3 changes: 2 additions & 1 deletion component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (
"net/http/httptest"
"testing"

"github.com/opentracing/opentracing-go"
"golang.org/x/net/context"
)

func TestComponent(t *testing.T) {
// Mechanical stuff.
ctx := context.Background()

handler, logger := WireUp(ctx, float32(99.99))
handler, logger := WireUp(ctx, float32(99.99), opentracing.GlobalTracer())

ts := httptest.NewServer(handler)
defer ts.Close()
Expand Down
34 changes: 34 additions & 0 deletions docker-compose-zipkin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
version: '2'

services:
payment:
image: weaveworksdemos/payment
hostname: payment
restart: always
cap_drop:
- all
cap_add:
- NET_BIND_SERVICE
read_only: true
environment:
- reschedule=on-node-failure
- ZIPKIN=http://zipkin:9411/api/v1/spans
ports:
- "8082:80"
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"
16 changes: 13 additions & 3 deletions endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package payment

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 @@ -13,16 +15,20 @@ 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{
AuthoriseEndpoint: MakeAuthoriseEndpoint(s),
HealthEndpoint: MakeHealthEndpoint(s),
AuthoriseEndpoint: opentracing.TraceServer(tracer, "POST /paymentAuth")(MakeAuthoriseEndpoint(s)),
HealthEndpoint: opentracing.TraceServer(tracer, "GET /health")(MakeHealthEndpoint(s)),
}
}

// MakeListEndpoint returns an endpoint via the given service.
func MakeAuthoriseEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
var span stdopentracing.Span
span, ctx = stdopentracing.StartSpanFromContext(ctx, "authorize payment")
span.SetTag("service", "payment")
defer span.Finish()
req := request.(AuthoriseRequest)
authorisation, err := s.Authorise(req.Amount)
return AuthoriseResponse{Authorisation: authorisation, Err: err}, nil
Expand All @@ -32,6 +38,10 @@ func MakeAuthoriseEndpoint(s Service) endpoint.Endpoint {
// MakeHealthEndpoint returns current health of the given service.
func MakeHealthEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
var span stdopentracing.Span
span, ctx = stdopentracing.StartSpanFromContext(ctx, "health check")
span.SetTag("service", "payment")
defer span.Finish()
health := s.Health()
return healthResponse{Health: health}, nil
}
Expand Down
8 changes: 5 additions & 3 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ import (
"net/http"

"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, logger log.Logger) http.Handler {
func MakeHTTPHandler(ctx context.Context, e Endpoints, logger log.Logger, tracer stdopentracing.Tracer) http.Handler {
r := mux.NewRouter().StrictSlash(false)
options := []httptransport.ServerOption{
httptransport.ServerErrorLogger(logger),
Expand All @@ -27,14 +29,14 @@ func MakeHTTPHandler(ctx context.Context, e Endpoints, logger log.Logger) http.H
e.AuthoriseEndpoint,
decodeAuthoriseRequest,
encodeAuthoriseResponse,
options...,
append(options, httptransport.ServerBefore(opentracing.FromHTTPRequest(tracer, "POST /paymentAuth", logger)))...,
))
r.Methods("GET").Path("/health").Handler(httptransport.NewServer(
ctx,
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 12fc91f

Please sign in to comment.