예제 #1
0
파일: json_test.go 프로젝트: jllopis/kit
func TestEncode(t *testing.T) {
	buf := &bytes.Buffer{}
	if err := jsoncodec.New().Encode(buf, response{Values: []string{"a", "b", "c"}}); err != nil {
		t.Fatal(err)
	}
	if want, have := `{"values":["a","b","c"]}`+"\n", buf.String(); want != have {
		t.Errorf("want %q, have %q", want, have)
	}
}
예제 #2
0
파일: json_test.go 프로젝트: jllopis/kit
func TestDecode(t *testing.T) {
	buf := bytes.NewBufferString(`{"a":1,"b":"2"}`)
	var req request
	if _, err := jsoncodec.New().Decode(context.Background(), buf, &req); err != nil {
		t.Fatal(err)
	}
	if want, have := (request{A: 1, B: "2"}), req; want != have {
		t.Errorf("want %v, have %v", want, have)
	}
}
예제 #3
0
파일: binding_test.go 프로젝트: jllopis/kit
func TestBinding(t *testing.T) {
	type myRequest struct {
		In int `json:"in"`
	}

	type myResponse struct {
		Out int `json:"out"`
	}

	transform := func(i int) int {
		return 3 * i // doesn't matter, just do something
	}

	endpoint := func(_ context.Context, request interface{}) (interface{}, error) {
		r, ok := request.(*myRequest)
		if !ok {
			return nil, fmt.Errorf("not myRequest (%s)", reflect.TypeOf(request))
		}
		return myResponse{transform(r.In)}, nil
	}

	ctx := context.Background()
	makeRequest := func() interface{} { return &myRequest{} }
	codec := jsoncodec.New()
	binding := httptransport.NewBinding(ctx, makeRequest, codec, endpoint)
	server := httptest.NewServer(binding)
	defer server.Close()

	n := 123
	requestBody, err := json.Marshal(myRequest{n})
	if err != nil {
		t.Fatal(err)
	}

	resp, err := http.Post(server.URL, "application/json", bytes.NewBuffer(requestBody))
	if err != nil {
		t.Fatal(err)
	}
	defer resp.Body.Close()

	var r myResponse
	if _, err := codec.Decode(ctx, resp.Body, &r); err != nil {
		t.Fatal(err)
	}

	if want, have := transform(n), r.Out; want != have {
		t.Errorf("want %d, have %d", want, have)
	}
}
예제 #4
0
파일: client_test.go 프로젝트: jllopis/kit
func TestClient(t *testing.T) {
	type myResponse struct {
		V int `json:"v"`
	}
	const v = 123
	codec := jsoncodec.New()
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		codec.Encode(w, myResponse{v})
	}))
	defer server.Close()
	makeResponse := func() interface{} { return &myResponse{} }
	client := httptransport.NewClient(server.URL, codec, makeResponse)
	resp, err := client(context.Background(), struct{}{})
	if err != nil {
		t.Fatal(err)
	}
	response, ok := resp.(*myResponse)
	if !ok {
		t.Fatalf("not myResponse (%s)", reflect.TypeOf(response))
	}
	if want, have := v, response.V; want != have {
		t.Errorf("want %d, have %d", want, have)
	}
}
예제 #5
0
파일: main.go 프로젝트: jllopis/kit
func main() {
	// Flag domain. Note that gRPC transitively registers flags via its import
	// of glog. So, we define a new flag set, to keep those domains distinct.
	fs := flag.NewFlagSet("", flag.ExitOnError)
	var (
		debugAddr        = fs.String("debug.addr", ":8000", "Address for HTTP debug/instrumentation server")
		httpAddr         = fs.String("http.addr", ":8001", "Address for HTTP (JSON) server")
		grpcAddr         = fs.String("grpc.addr", ":8002", "Address for gRPC server")
		thriftAddr       = fs.String("thrift.addr", ":8003", "Address for Thrift server")
		thriftProtocol   = fs.String("thrift.protocol", "binary", "binary, compact, json, simplejson")
		thriftBufferSize = fs.Int("thrift.buffer.size", 0, "0 for unbuffered")
		thriftFramed     = fs.Bool("thrift.framed", false, "true to enable framing")

		proxyHTTPAddr = fs.String("proxy.http.url", "", "if set, proxy requests over HTTP to this addsvc")

		zipkinServiceName            = fs.String("zipkin.service.name", "addsvc", "Zipkin service name")
		zipkinCollectorAddr          = fs.String("zipkin.collector.addr", "", "Zipkin Scribe collector address (empty will log spans)")
		zipkinCollectorTimeout       = fs.Duration("zipkin.collector.timeout", time.Second, "Zipkin collector timeout")
		zipkinCollectorBatchSize     = fs.Int("zipkin.collector.batch.size", 100, "Zipkin collector batch size")
		zipkinCollectorBatchInterval = fs.Duration("zipkin.collector.batch.interval", time.Second, "Zipkin collector batch interval")
	)
	flag.Usage = fs.Usage // only show our flags
	fs.Parse(os.Args[1:])

	// `package log` domain
	var logger kitlog.Logger
	logger = kitlog.NewLogfmtLogger(os.Stderr)
	logger = kitlog.With(logger, "ts", kitlog.DefaultTimestampUTC, "caller", kitlog.DefaultCaller)
	stdlog.SetOutput(kitlog.NewStdlibAdapter(logger)) // redirect stdlib logging to us
	stdlog.SetFlags(0)                                // flags are handled in our logger

	// `package metrics` domain
	requests := metrics.NewMultiCounter(
		expvar.NewCounter("requests"),
		statsd.NewCounter(ioutil.Discard, "requests_total", time.Second),
		prometheus.NewCounter(stdprometheus.CounterOpts{
			Namespace: "addsvc",
			Subsystem: "add",
			Name:      "requests_total",
			Help:      "Total number of received requests.",
		}, []string{}),
	)
	duration := metrics.NewMultiHistogram(
		expvar.NewHistogram("duration_nanoseconds_total", 0, 100000000, 3),
		statsd.NewHistogram(ioutil.Discard, "duration_nanoseconds_total", time.Second),
		prometheus.NewSummary(stdprometheus.SummaryOpts{
			Namespace: "addsvc",
			Subsystem: "add",
			Name:      "duration_nanoseconds_total",
			Help:      "Total nanoseconds spend serving requests.",
		}, []string{}),
	)

	// `package tracing` domain
	zipkinHostPort := "localhost:1234" // TODO Zipkin makes overly simple assumptions about services
	var zipkinCollector zipkin.Collector = loggingCollector{logger}
	if *zipkinCollectorAddr != "" {
		var err error
		if zipkinCollector, err = zipkin.NewScribeCollector(
			*zipkinCollectorAddr,
			*zipkinCollectorTimeout,
			*zipkinCollectorBatchSize,
			*zipkinCollectorBatchInterval,
		); err != nil {
			logger.Log("err", err)
			os.Exit(1)
		}
	}
	zipkinMethodName := "add"
	zipkinSpanFunc := zipkin.MakeNewSpanFunc(zipkinHostPort, *zipkinServiceName, zipkinMethodName)
	zipkin.Log.Swap(logger) // log diagnostic/error details

	// Our business and operational domain
	var a Add = pureAdd
	if *proxyHTTPAddr != "" {
		codec := jsoncodec.New()
		makeResponse := func() interface{} { return &addResponse{} }

		var e endpoint.Endpoint
		e = httptransport.NewClient(*proxyHTTPAddr, codec, makeResponse, httptransport.ClientBefore(zipkin.ToRequest(zipkinSpanFunc)))
		e = zipkin.AnnotateClient(zipkinSpanFunc, zipkinCollector)(e)

		a = proxyAdd(e, logger)
	}
	a = logging(logger)(a)

	// Server domain
	var e endpoint.Endpoint
	e = makeEndpoint(a)
	e = zipkin.AnnotateServer(zipkinSpanFunc, zipkinCollector)(e)

	// Mechanical stuff
	rand.Seed(time.Now().UnixNano())
	root := context.Background()
	errc := make(chan error)

	go func() {
		errc <- interrupt()
	}()

	// Transport: HTTP (debug/instrumentation)
	go func() {
		logger.Log("addr", *debugAddr, "transport", "debug")
		errc <- http.ListenAndServe(*debugAddr, nil)
	}()

	// Transport: HTTP (JSON)
	go func() {
		ctx, cancel := context.WithCancel(root)
		defer cancel()

		field := metrics.Field{Key: "transport", Value: "http"}
		before := httptransport.BindingBefore(zipkin.ToContext(zipkinSpanFunc))
		after := httptransport.BindingAfter(httptransport.SetContentType("application/json"))
		makeRequest := func() interface{} { return &addRequest{} }

		var handler http.Handler
		handler = httptransport.NewBinding(ctx, makeRequest, jsoncodec.New(), e, before, after)
		handler = encoding.Gzip(handler)
		handler = cors.Middleware(cors.Config{})(handler)
		handler = httpInstrument(requests.With(field), duration.With(field))(handler)

		mux := http.NewServeMux()
		mux.Handle("/add", handler)
		logger.Log("addr", *httpAddr, "transport", "HTTP")
		errc <- http.ListenAndServe(*httpAddr, mux)
	}()

	// Transport: gRPC
	go func() {
		ln, err := net.Listen("tcp", *grpcAddr)
		if err != nil {
			errc <- err
			return
		}
		s := grpc.NewServer() // uses its own context?
		field := metrics.Field{Key: "transport", Value: "grpc"}

		var addServer pb.AddServer
		addServer = grpcBinding{e}
		addServer = grpcInstrument(requests.With(field), duration.With(field))(addServer)

		pb.RegisterAddServer(s, addServer)
		logger.Log("addr", *grpcAddr, "transport", "gRPC")
		errc <- s.Serve(ln)
	}()

	// Transport: Thrift
	go func() {
		ctx, cancel := context.WithCancel(root)
		defer cancel()

		var protocolFactory thrift.TProtocolFactory
		switch *thriftProtocol {
		case "binary":
			protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
		case "compact":
			protocolFactory = thrift.NewTCompactProtocolFactory()
		case "json":
			protocolFactory = thrift.NewTJSONProtocolFactory()
		case "simplejson":
			protocolFactory = thrift.NewTSimpleJSONProtocolFactory()
		default:
			errc <- fmt.Errorf("invalid Thrift protocol %q", *thriftProtocol)
			return
		}

		var transportFactory thrift.TTransportFactory
		if *thriftBufferSize > 0 {
			transportFactory = thrift.NewTBufferedTransportFactory(*thriftBufferSize)
		} else {
			transportFactory = thrift.NewTTransportFactory()
		}

		if *thriftFramed {
			transportFactory = thrift.NewTFramedTransportFactory(transportFactory)
		}

		transport, err := thrift.NewTServerSocket(*thriftAddr)
		if err != nil {
			errc <- err
			return
		}

		field := metrics.Field{Key: "transport", Value: "thrift"}

		var service thriftadd.AddService
		service = thriftBinding{ctx, e}
		service = thriftInstrument(requests.With(field), duration.With(field))(service)

		logger.Log("addr", *thriftAddr, "transport", "Thrift")
		errc <- thrift.NewTSimpleServer4(
			thriftadd.NewAddServiceProcessor(service),
			transport,
			transportFactory,
			protocolFactory,
		).Serve()
	}()

	logger.Log("fatal", <-errc)
}