Exemplo n.º 1
0
func Test_Proto_Thrift(t *testing.T) {
	t.Skip()
	server := &TServer{}
	err := server.Start(thrift.NewTTransportFactory())
	assert.NoError(t, err)
	defer server.Server.Stop()

	var transport thrift.TTransport
	transport, err = thrift.NewTSocket(fmt.Sprintf("127.0.0.1:%d", server.Port))
	assert.NoError(t, err)

	transportFactory := thrift.NewTTransportFactory()
	transport = transportFactory.GetTransport(transport)

	err = transport.Open()
	assert.NoError(t, err)
	defer transport.Close()

	protoFactory := thrift.NewTBinaryProtocolFactoryDefault()
	client := srv.NewTSrvClientFactory(transport, protoFactory)

	req := srv.TestRep{
		ID:   "test",
		Data: []byte("mama myla ramy"),
	}
	res, err := client.Test(&req)
	assert.NoError(t, err)
	assert.EqualValues(t, req, *res)
}
Exemplo n.º 2
0
func (t *TRpcServer) GetClientTransport() (thrift.TTransport, error) {
	transport, err := thrift.NewTSocket(t.Addr().String())
	if err != nil {
		return nil, err
	}
	if err = transport.Open(); err != nil {
		return nil, err
	}

	return thrift.NewTFramedTransport(transport), nil
}
Exemplo n.º 3
0
func main() {

	transportFactory := thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())
	protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()

	transport, err := thrift.NewTSocket(net.JoinHostPort("218.244.131.175", "8900"))
	if err != nil {
		fmt.Fprintln(os.Stderr, "error resolving address:", err)
		os.Exit(1)
	}

	useTransport := transportFactory.GetTransport(transport)
	client := ui2bs.NewBSServiceClientFactory(useTransport, protocolFactory)
	if err := transport.Open(); err != nil {
		fmt.Fprintln(os.Stderr, "Error opening socket to 127.0.0.1:8084", " ", err)
		os.Exit(1)
	}
	defer transport.Close()

	bs_req := new(ui2bs.BSRequest)
	bs_req.Searchid = "123"
	bs_req.Media = new(ui2bs.Media)
	bs_req.Media.Appsid = "123"
	bs_req.Media.ChannelId = "123"
	bs_req.Adslot = new(ui2bs.AdSlot)
	bs_req.Device = new(ui2bs.Device)
	bs_req.Device.Os = ui2bs.OSType_ANDROID
	bs_resp := new(ui2bs.BSResponse)
	//	bs_resp.Ads = new([]ui2bs.Ad)
	bs_resp, err = client.Search(bs_req)
	if err != nil {
		fmt.Println("error occuse")
		fmt.Println(err)
	}
	//	for i:=0; i < 10; i++ {
	//		err := client.Search2()
	//		if err != nil {
	//			fmt.Println("serach fail")
	//			transport, err = thrift.NewTSocket(net.JoinHostPort("127.0.0.1", "8084"))
	//			transport.Open()
	//			useTransport = transportFactory.GetTransport(transport)
	//			client = bs.NewBSServiceClientFactory(useTransport, protocolFactory)
	//		}
	//		time.Sleep(1*time.Second)
	//	}
	//
	fmt.Println(bs_resp)
	fmt.Println("I am exit ")

}
Exemplo n.º 4
0
func (p *TPool) factory() (res pools.Resource, err error) {
	// peek endpoint
	var endpoint string
	if p.endpointsCap == 0 {
		endpoint = p.endpoints[0]
	} else {
		endpoint = p.endpoints[rand.Intn(p.endpointsCap)]
	}

	var transport thrift.TTransport
	if transport, err = thrift.NewTSocket(endpoint); err != nil {
		return
	}
	transport = p.transportFactory.GetTransport(transport)

	client := wire.NewBarClientFactory(transport, p.protoFactory)
	res = &TClient{client, p}
	return
}
func main() {
	transportFactory := thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())
	protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()

	transport, err := thrift.NewTSocket(net.JoinHostPort("127.0.0.1", "19090"))
	if err != nil {
		fmt.Fprintln(os.Stderr, "error resolving address:", err)
		os.Exit(1)
	}

	useTransport := transportFactory.GetTransport(transport)
	client := account.NewAccountClientFactory(useTransport, protocolFactory)
	if err := transport.Open(); err != nil {
		fmt.Fprintln(os.Stderr, "Error opening socket to 127.0.0.1:19090", "", err)
		os.Exit(1)
	}
	defer transport.Close()

	// 第一个请求,登录账号
	request := &account.Request{
		Name:     "super",
		Password: "******",
		Op:       account.Operation_LOGIN,
	}

	r, err := client.DoAction(request)
	fmt.Println(r)

	// 第二个请求,注册账号
	request.Op = account.Operation_REGISTER
	r, err = client.DoAction(request)
	fmt.Println(r)

	// 第三个请求,name为空的请求
	request.Name = ""
	r, err = client.DoAction(request)
	fmt.Println(r)

}
Exemplo n.º 6
0
Arquivo: client.go Projeto: qband/down
// ConcatEndpointFactory transforms host:port strings into Endpoints.
func (c client) ConcatEndpoint(instance string) (endpoint.Endpoint, io.Closer, error) {
	transportSocket, err := thrift.NewTSocket(instance)
	if err != nil {
		c.Logger.Log("during", "thrift.NewTSocket", "err", err)
		return nil, nil, err
	}
	trans := c.TTransportFactory.GetTransport(transportSocket)

	if err := trans.Open(); err != nil {
		c.Logger.Log("during", "thrift transport.Open", "err", err)
		return nil, nil, err
	}
	cli := thriftadd.NewAddServiceClientFactory(trans, c.TProtocolFactory)

	return func(ctx context.Context, request interface{}) (interface{}, error) {
		concatRequest := request.(server.ConcatRequest)
		reply, err := cli.Concat(concatRequest.A, concatRequest.B)
		if err != nil {
			return server.ConcatResponse{}, err
		}
		return server.ConcatResponse{V: reply.Value}, nil
	}, trans, nil
}
Exemplo n.º 7
0
func Benchmark_Proto_Thrift_Buffered(b *testing.B) {
	b.Skip()
	server := &TServer{}
	err := server.Start(thrift.NewTBufferedTransportFactory(thrift_buffer))
	assert.NoError(b, err)
	defer server.Server.Stop()

	var transport thrift.TTransport
	transport, err = thrift.NewTSocket(fmt.Sprintf("127.0.0.1:%d", server.Port))
	assert.NoError(b, err)

	transportFactory := thrift.NewTBufferedTransportFactory(thrift_buffer)
	transport = transportFactory.GetTransport(transport)

	err = transport.Open()
	assert.NoError(b, err)
	defer transport.Close()

	protoFactory := thrift.NewTBinaryProtocolFactoryDefault()
	client := srv.NewTSrvClientFactory(transport, protoFactory)

	b.Log(server.Port, b.N)

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		b.StartTimer()
		req := srv.TestRep{
			ID:   fmt.Sprintf("%d", i),
			Data: []byte("mama myla ramy"),
		}
		res, err := client.Test(&req)
		b.StopTimer()
		assert.NoError(b, err)
		assert.EqualValues(b, req, *res)
		b.SetBytes(int64(len(req.Data) * 2))
	}
}
Exemplo n.º 8
0
Arquivo: main.go Projeto: nimmen/kit
func main() {
	var (
		transport        = flag.String("transport", "httpjson", "httpjson, grpc, netrpc, thrift")
		httpAddr         = flag.String("http.addr", "localhost:8001", "Address for HTTP (JSON) server")
		grpcAddr         = flag.String("grpc.addr", "localhost:8002", "Address for gRPC server")
		netrpcAddr       = flag.String("netrpc.addr", "localhost:8003", "Address for net/rpc server")
		thriftAddr       = flag.String("thrift.addr", "localhost:8004", "Address for Thrift server")
		thriftProtocol   = flag.String("thrift.protocol", "binary", "binary, compact, json, simplejson")
		thriftBufferSize = flag.Int("thrift.buffer.size", 0, "0 for unbuffered")
		thriftFramed     = flag.Bool("thrift.framed", false, "true to enable framing")
	)
	flag.Parse()
	if len(os.Args) < 4 {
		fmt.Fprintf(os.Stderr, "\n%s [flags] method arg1 arg2\n\n", filepath.Base(os.Args[0]))
		flag.Usage()
		os.Exit(1)
	}

	root := context.Background()
	method, s1, s2 := flag.Arg(0), flag.Arg(1), flag.Arg(2)

	var logger log.Logger
	logger = log.NewLogfmtLogger(os.Stdout)
	logger = log.NewContext(logger).With("caller", log.DefaultCaller)
	logger = log.NewContext(logger).With("transport", *transport)

	var svc server.AddService
	switch *transport {
	case "grpc":
		cc, err := grpc.Dial(*grpcAddr)
		if err != nil {
			_ = logger.Log("err", err)
			os.Exit(1)
		}
		defer cc.Close()
		svc = grpcclient.New(root, cc, logger)

	case "httpjson":
		rawurl := *httpAddr
		if !strings.HasPrefix("http", rawurl) {
			rawurl = "http://" + rawurl
		}
		baseurl, err := url.Parse(rawurl)
		if err != nil {
			_ = logger.Log("err", err)
			os.Exit(1)
		}
		svc = httpjsonclient.New(root, baseurl, logger, nil)

	case "netrpc":
		cli, err := rpc.DialHTTP("tcp", *netrpcAddr)
		if err != nil {
			_ = logger.Log("err", err)
			os.Exit(1)
		}
		defer cli.Close()
		svc = netrpcclient.New(cli, logger)

	case "thrift":
		var protocolFactory thrift.TProtocolFactory
		switch *thriftProtocol {
		case "compact":
			protocolFactory = thrift.NewTCompactProtocolFactory()
		case "simplejson":
			protocolFactory = thrift.NewTSimpleJSONProtocolFactory()
		case "json":
			protocolFactory = thrift.NewTJSONProtocolFactory()
		case "binary", "":
			protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
		default:
			_ = logger.Log("protocol", *thriftProtocol, "err", "invalid protocol")
			os.Exit(1)
		}
		var transportFactory thrift.TTransportFactory
		if *thriftBufferSize > 0 {
			transportFactory = thrift.NewTBufferedTransportFactory(*thriftBufferSize)
		} else {
			transportFactory = thrift.NewTTransportFactory()
		}
		if *thriftFramed {
			transportFactory = thrift.NewTFramedTransportFactory(transportFactory)
		}
		transportSocket, err := thrift.NewTSocket(*thriftAddr)
		if err != nil {
			_ = logger.Log("during", "thrift.NewTSocket", "err", err)
			os.Exit(1)
		}
		trans := transportFactory.GetTransport(transportSocket)
		defer trans.Close()
		if err := trans.Open(); err != nil {
			_ = logger.Log("during", "thrift transport.Open", "err", err)
			os.Exit(1)
		}
		cli := thriftadd.NewAddServiceClientFactory(trans, protocolFactory)
		svc = thriftclient.New(cli, logger)

	default:
		_ = logger.Log("err", "invalid transport")
		os.Exit(1)
	}

	begin := time.Now()
	switch method {
	case "sum":
		a, _ := strconv.Atoi(s1)
		b, _ := strconv.Atoi(s2)
		v := svc.Sum(a, b)
		_ = logger.Log("method", "sum", "a", a, "b", b, "v", v, "took", time.Since(begin))

	case "concat":
		a, b := s1, s2
		v := svc.Concat(a, b)
		_ = logger.Log("method", "concat", "a", a, "b", b, "v", v, "took", time.Since(begin))

	default:
		_ = logger.Log("err", "invalid method "+method)
		os.Exit(1)
	}
}
Exemplo n.º 9
0
func main() {
	flag.Usage = Usage
	var host string
	var port int
	var protocol string
	var urlString string
	var framed bool
	var useHttp bool
	var parsedUrl url.URL
	var trans thrift.TTransport
	_ = strconv.Atoi
	_ = math.Abs
	flag.Usage = Usage
	flag.StringVar(&host, "h", "localhost", "Specify host and port")
	flag.IntVar(&port, "p", 9090, "Specify port")
	flag.StringVar(&protocol, "P", "binary", "Specify the protocol (binary, compact, simplejson, json)")
	flag.StringVar(&urlString, "u", "", "Specify the url")
	flag.BoolVar(&framed, "framed", false, "Use framed transport")
	flag.BoolVar(&useHttp, "http", false, "Use http")
	flag.Parse()

	if len(urlString) > 0 {
		parsedUrl, err := url.Parse(urlString)
		if err != nil {
			fmt.Fprintln(os.Stderr, "Error parsing URL: ", err)
			flag.Usage()
		}
		host = parsedUrl.Host
		useHttp = len(parsedUrl.Scheme) <= 0 || parsedUrl.Scheme == "http"
	} else if useHttp {
		_, err := url.Parse(fmt.Sprint("http://", host, ":", port))
		if err != nil {
			fmt.Fprintln(os.Stderr, "Error parsing URL: ", err)
			flag.Usage()
		}
	}

	cmd := flag.Arg(0)
	var err error
	if useHttp {
		trans, err = thrift.NewTHttpClient(parsedUrl.String())
	} else {
		portStr := fmt.Sprint(port)
		if strings.Contains(host, ":") {
			host, portStr, err = net.SplitHostPort(host)
			if err != nil {
				fmt.Fprintln(os.Stderr, "error with host:", err)
				os.Exit(1)
			}
		}
		trans, err = thrift.NewTSocket(net.JoinHostPort(host, portStr))
		if err != nil {
			fmt.Fprintln(os.Stderr, "error resolving address:", err)
			os.Exit(1)
		}
		if framed {
			trans = thrift.NewTFramedTransport(trans)
		}
	}
	if err != nil {
		fmt.Fprintln(os.Stderr, "Error creating transport", err)
		os.Exit(1)
	}
	defer trans.Close()
	var protocolFactory thrift.TProtocolFactory
	switch protocol {
	case "compact":
		protocolFactory = thrift.NewTCompactProtocolFactory()
		break
	case "simplejson":
		protocolFactory = thrift.NewTSimpleJSONProtocolFactory()
		break
	case "json":
		protocolFactory = thrift.NewTJSONProtocolFactory()
		break
	case "binary", "":
		protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
		break
	default:
		fmt.Fprintln(os.Stderr, "Invalid protocol specified: ", protocol)
		Usage()
		os.Exit(1)
	}
	client := add.NewAddServiceClientFactory(trans, protocolFactory)
	if err := trans.Open(); err != nil {
		fmt.Fprintln(os.Stderr, "Error opening socket to ", host, ":", port, " ", err)
		os.Exit(1)
	}

	switch cmd {
	case "Add":
		if flag.NArg()-1 != 2 {
			fmt.Fprintln(os.Stderr, "Add requires 2 args")
			flag.Usage()
		}
		argvalue0, err4 := (strconv.ParseInt(flag.Arg(1), 10, 64))
		if err4 != nil {
			Usage()
			return
		}
		value0 := argvalue0
		argvalue1, err5 := (strconv.ParseInt(flag.Arg(2), 10, 64))
		if err5 != nil {
			Usage()
			return
		}
		value1 := argvalue1
		fmt.Print(client.Add(value0, value1))
		fmt.Print("\n")
		break
	case "":
		Usage()
		break
	default:
		fmt.Fprintln(os.Stderr, "Invalid function ", cmd)
	}
}
Exemplo n.º 10
0
Arquivo: main.go Projeto: EthanK28/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 (
		transport        = fs.String("transport", "grpc", "http, grpc, netrpc, thrift")
		httpAddr         = fs.String("http.addr", "localhost:8001", "HTTP (JSON) address")
		grpcAddr         = fs.String("grpc.addr", "localhost:8002", "gRPC address")
		netrpcAddr       = fs.String("netrpc.addr", "localhost:8003", "net/rpc address")
		thriftAddr       = fs.String("thrift.addr", "localhost:8004", "Thrift address")
		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")
		a                = fs.Int64("a", 1, "a value")
		b                = fs.Int64("b", 2, "b value")
	)
	flag.Usage = fs.Usage // only show our flags
	fs.Parse(os.Args[1:])
	log.SetFlags(0)
	log.SetOutput(os.Stdout)

	var e endpoint.Endpoint
	switch *transport {
	case "http":
		if !strings.HasPrefix(*httpAddr, "http") {
			*httpAddr = "http://" + *httpAddr
		}
		u, err := url.Parse(*httpAddr)
		if err != nil {
			log.Fatalf("url.Parse: %v", err)
		}
		if u.Path == "" {
			u.Path = "/add"
		}
		e = httpclient.NewClient("GET", u.String())

	case "grpc":
		cc, err := grpc.Dial(*grpcAddr)
		if err != nil {
			log.Fatalf("grpc.Dial: %v", err)
		}
		e = grpcclient.NewClient(cc)

	case "netrpc":
		client, err := rpc.DialHTTP("tcp", *netrpcAddr)
		if err != nil {
			log.Fatalf("rpc.DialHTTP: %v", err)
		}
		e = netrpcclient.NewClient(client)

	case "thrift":
		var protocolFactory thrift.TProtocolFactory
		switch *thriftProtocol {
		case "compact":
			protocolFactory = thrift.NewTCompactProtocolFactory()
		case "simplejson":
			protocolFactory = thrift.NewTSimpleJSONProtocolFactory()
		case "json":
			protocolFactory = thrift.NewTJSONProtocolFactory()
		case "binary", "":
			protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
		default:
			log.Fatalf("invalid protocol %q", *thriftProtocol)
		}

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

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

		transportSocket, err := thrift.NewTSocket(*thriftAddr)
		if err != nil {
			log.Fatalf("thrift.NewTSocket: %v", err)
		}

		transport := transportFactory.GetTransport(transportSocket)
		defer transport.Close()
		if err := transport.Open(); err != nil {
			log.Fatalf("Thrift transport.Open: %v", err)
		}

		e = thriftclient.NewClient(thriftadd.NewAddServiceClientFactory(transport, protocolFactory))

	default:
		log.Fatalf("unsupported transport %q", *transport)
	}

	response, err := e(context.Background(), reqrep.AddRequest{A: *a, B: *b})
	if err != nil {
		log.Fatalf("when invoking request: %v", err)
	}
	addResponse, ok := response.(reqrep.AddResponse)
	if !ok {
		log.Fatalf("when type-asserting response: %v", endpoint.ErrBadCast)
	}
	log.Print(addResponse.V)
}
Exemplo n.º 11
0
func main() {
	flag.Usage = Usage
	var host string
	var port int
	var protocol string
	var urlString string
	var framed bool
	var useHttp bool
	var parsedUrl url.URL
	var trans thrift.TTransport
	_ = strconv.Atoi
	_ = math.Abs
	flag.Usage = Usage
	flag.StringVar(&host, "h", "localhost", "Specify host and port")
	flag.IntVar(&port, "p", 9090, "Specify port")
	flag.StringVar(&protocol, "P", "binary", "Specify the protocol (binary, compact, simplejson, json)")
	flag.StringVar(&urlString, "u", "", "Specify the url")
	flag.BoolVar(&framed, "framed", false, "Use framed transport")
	flag.BoolVar(&useHttp, "http", false, "Use http")
	flag.Parse()

	if len(urlString) > 0 {
		parsedUrl, err := url.Parse(urlString)
		if err != nil {
			fmt.Fprintln(os.Stderr, "Error parsing URL: ", err)
			flag.Usage()
		}
		host = parsedUrl.Host
		useHttp = len(parsedUrl.Scheme) <= 0 || parsedUrl.Scheme == "http"
	} else if useHttp {
		_, err := url.Parse(fmt.Sprint("http://", host, ":", port))
		if err != nil {
			fmt.Fprintln(os.Stderr, "Error parsing URL: ", err)
			flag.Usage()
		}
	}

	cmd := flag.Arg(0)
	var err error
	if useHttp {
		trans, err = thrift.NewTHttpClient(parsedUrl.String())
	} else {
		portStr := fmt.Sprint(port)
		if strings.Contains(host, ":") {
			host, portStr, err = net.SplitHostPort(host)
			if err != nil {
				fmt.Fprintln(os.Stderr, "error with host:", err)
				os.Exit(1)
			}
		}
		trans, err = thrift.NewTSocket(net.JoinHostPort(host, portStr))
		if err != nil {
			fmt.Fprintln(os.Stderr, "error resolving address:", err)
			os.Exit(1)
		}
		if framed {
			trans = thrift.NewTFramedTransport(trans)
		}
	}
	if err != nil {
		fmt.Fprintln(os.Stderr, "Error creating transport", err)
		os.Exit(1)
	}
	defer trans.Close()
	var protocolFactory thrift.TProtocolFactory
	switch protocol {
	case "compact":
		protocolFactory = thrift.NewTCompactProtocolFactory()
		break
	case "simplejson":
		protocolFactory = thrift.NewTSimpleJSONProtocolFactory()
		break
	case "json":
		protocolFactory = thrift.NewTJSONProtocolFactory()
		break
	case "binary", "":
		protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
		break
	default:
		fmt.Fprintln(os.Stderr, "Invalid protocol specified: ", protocol)
		Usage()
		os.Exit(1)
	}
	client := scribe.NewScribeClientFactory(trans, protocolFactory)
	if err := trans.Open(); err != nil {
		fmt.Fprintln(os.Stderr, "Error opening socket to ", host, ":", port, " ", err)
		os.Exit(1)
	}

	switch cmd {
	case "Log":
		if flag.NArg()-1 != 1 {
			fmt.Fprintln(os.Stderr, "Log requires 1 args")
			flag.Usage()
		}
		arg5 := flag.Arg(1)
		mbTrans6 := thrift.NewTMemoryBufferLen(len(arg5))
		defer mbTrans6.Close()
		_, err7 := mbTrans6.WriteString(arg5)
		if err7 != nil {
			Usage()
			return
		}
		factory8 := thrift.NewTSimpleJSONProtocolFactory()
		jsProt9 := factory8.GetProtocol(mbTrans6)
		containerStruct0 := scribe.NewLogArgs()
		err10 := containerStruct0.ReadField1(jsProt9)
		if err10 != nil {
			Usage()
			return
		}
		argvalue0 := containerStruct0.Messages
		value0 := argvalue0
		fmt.Print(client.Log(value0))
		fmt.Print("\n")
		break
	case "":
		Usage()
		break
	default:
		fmt.Fprintln(os.Stderr, "Invalid function ", cmd)
	}
}
Exemplo n.º 12
0
func main() {
	flag.Usage = Usage
	var host string
	var port int
	var protocol string
	var urlString string
	var framed bool
	var useHttp bool
	var parsedUrl url.URL
	var trans thrift.TTransport
	_ = strconv.Atoi
	_ = math.Abs
	flag.Usage = Usage
	flag.StringVar(&host, "h", "localhost", "Specify host and port")
	flag.IntVar(&port, "p", 9090, "Specify port")
	flag.StringVar(&protocol, "P", "binary", "Specify the protocol (binary, compact, simplejson, json)")
	flag.StringVar(&urlString, "u", "", "Specify the url")
	flag.BoolVar(&framed, "framed", false, "Use framed transport")
	flag.BoolVar(&useHttp, "http", false, "Use http")
	flag.Parse()

	if len(urlString) > 0 {
		parsedUrl, err := url.Parse(urlString)
		if err != nil {
			fmt.Fprintln(os.Stderr, "Error parsing URL: ", err)
			flag.Usage()
		}
		host = parsedUrl.Host
		useHttp = len(parsedUrl.Scheme) <= 0 || parsedUrl.Scheme == "http"
	} else if useHttp {
		_, err := url.Parse(fmt.Sprint("http://", host, ":", port))
		if err != nil {
			fmt.Fprintln(os.Stderr, "Error parsing URL: ", err)
			flag.Usage()
		}
	}

	cmd := flag.Arg(0)
	var err error
	if useHttp {
		trans, err = thrift.NewTHttpClient(parsedUrl.String())
	} else {
		portStr := fmt.Sprint(port)
		if strings.Contains(host, ":") {
			host, portStr, err = net.SplitHostPort(host)
			if err != nil {
				fmt.Fprintln(os.Stderr, "error with host:", err)
				os.Exit(1)
			}
		}
		trans, err = thrift.NewTSocket(net.JoinHostPort(host, portStr))
		if err != nil {
			fmt.Fprintln(os.Stderr, "error resolving address:", err)
			os.Exit(1)
		}
		if framed {
			trans = thrift.NewTFramedTransport(trans)
		}
	}
	if err != nil {
		fmt.Fprintln(os.Stderr, "Error creating transport", err)
		os.Exit(1)
	}
	defer trans.Close()
	var protocolFactory thrift.TProtocolFactory
	switch protocol {
	case "compact":
		protocolFactory = thrift.NewTCompactProtocolFactory()
		break
	case "simplejson":
		protocolFactory = thrift.NewTSimpleJSONProtocolFactory()
		break
	case "json":
		protocolFactory = thrift.NewTJSONProtocolFactory()
		break
	case "binary", "":
		protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
		break
	default:
		fmt.Fprintln(os.Stderr, "Invalid protocol specified: ", protocol)
		Usage()
		os.Exit(1)
	}
	client := zipkincollector.NewZipkinCollectorClientFactory(trans, protocolFactory)
	if err := trans.Open(); err != nil {
		fmt.Fprintln(os.Stderr, "Error opening socket to ", host, ":", port, " ", err)
		os.Exit(1)
	}

	switch cmd {
	case "storeTopAnnotations":
		if flag.NArg()-1 != 2 {
			fmt.Fprintln(os.Stderr, "StoreTopAnnotations requires 2 args")
			flag.Usage()
		}
		argvalue0 := flag.Arg(1)
		value0 := argvalue0
		arg10 := flag.Arg(2)
		mbTrans11 := thrift.NewTMemoryBufferLen(len(arg10))
		defer mbTrans11.Close()
		_, err12 := mbTrans11.WriteString(arg10)
		if err12 != nil {
			Usage()
			return
		}
		factory13 := thrift.NewTSimpleJSONProtocolFactory()
		jsProt14 := factory13.GetProtocol(mbTrans11)
		containerStruct1 := zipkincollector.NewStoreTopAnnotationsArgs()
		err15 := containerStruct1.ReadField2(jsProt14)
		if err15 != nil {
			Usage()
			return
		}
		argvalue1 := containerStruct1.Annotations
		value1 := argvalue1
		fmt.Print(client.StoreTopAnnotations(value0, value1))
		fmt.Print("\n")
		break
	case "storeTopKeyValueAnnotations":
		if flag.NArg()-1 != 2 {
			fmt.Fprintln(os.Stderr, "StoreTopKeyValueAnnotations requires 2 args")
			flag.Usage()
		}
		argvalue0 := flag.Arg(1)
		value0 := argvalue0
		arg17 := flag.Arg(2)
		mbTrans18 := thrift.NewTMemoryBufferLen(len(arg17))
		defer mbTrans18.Close()
		_, err19 := mbTrans18.WriteString(arg17)
		if err19 != nil {
			Usage()
			return
		}
		factory20 := thrift.NewTSimpleJSONProtocolFactory()
		jsProt21 := factory20.GetProtocol(mbTrans18)
		containerStruct1 := zipkincollector.NewStoreTopKeyValueAnnotationsArgs()
		err22 := containerStruct1.ReadField2(jsProt21)
		if err22 != nil {
			Usage()
			return
		}
		argvalue1 := containerStruct1.Annotations
		value1 := argvalue1
		fmt.Print(client.StoreTopKeyValueAnnotations(value0, value1))
		fmt.Print("\n")
		break
	case "storeDependencies":
		if flag.NArg()-1 != 1 {
			fmt.Fprintln(os.Stderr, "StoreDependencies requires 1 args")
			flag.Usage()
		}
		arg23 := flag.Arg(1)
		mbTrans24 := thrift.NewTMemoryBufferLen(len(arg23))
		defer mbTrans24.Close()
		_, err25 := mbTrans24.WriteString(arg23)
		if err25 != nil {
			Usage()
			return
		}
		factory26 := thrift.NewTSimpleJSONProtocolFactory()
		jsProt27 := factory26.GetProtocol(mbTrans24)
		argvalue0 := zipkincollector.NewDependencies()
		err28 := argvalue0.Read(jsProt27)
		if err28 != nil {
			Usage()
			return
		}
		value0 := argvalue0
		fmt.Print(client.StoreDependencies(value0))
		fmt.Print("\n")
		break
	case "Log":
		if flag.NArg()-1 != 1 {
			fmt.Fprintln(os.Stderr, "Log requires 1 args")
			flag.Usage()
		}
		arg29 := flag.Arg(1)
		mbTrans30 := thrift.NewTMemoryBufferLen(len(arg29))
		defer mbTrans30.Close()
		_, err31 := mbTrans30.WriteString(arg29)
		if err31 != nil {
			Usage()
			return
		}
		factory32 := thrift.NewTSimpleJSONProtocolFactory()
		jsProt33 := factory32.GetProtocol(mbTrans30)
		containerStruct0 := zipkincollector.NewLogArgs()
		err34 := containerStruct0.ReadField1(jsProt33)
		if err34 != nil {
			Usage()
			return
		}
		argvalue0 := containerStruct0.Messages
		value0 := argvalue0
		fmt.Print(client.Log(value0))
		fmt.Print("\n")
		break
	case "":
		Usage()
		break
	default:
		fmt.Fprintln(os.Stderr, "Invalid function ", cmd)
	}
}
Exemplo n.º 13
0
func main() {
	flag.Usage = Usage
	var host string
	var port int
	var protocol string
	var urlString string
	var framed bool
	var useHttp bool
	var parsedUrl url.URL
	var trans thrift.TTransport
	_ = strconv.Atoi
	_ = math.Abs
	flag.Usage = Usage
	flag.StringVar(&host, "h", "localhost", "Specify host and port")
	flag.IntVar(&port, "p", 9090, "Specify port")
	flag.StringVar(&protocol, "P", "binary", "Specify the protocol (binary, compact, simplejson, json)")
	flag.StringVar(&urlString, "u", "", "Specify the url")
	flag.BoolVar(&framed, "framed", false, "Use framed transport")
	flag.BoolVar(&useHttp, "http", false, "Use http")
	flag.Parse()

	if len(urlString) > 0 {
		parsedUrl, err := url.Parse(urlString)
		if err != nil {
			fmt.Fprintln(os.Stderr, "Error parsing URL: ", err)
			flag.Usage()
		}
		host = parsedUrl.Host
		useHttp = len(parsedUrl.Scheme) <= 0 || parsedUrl.Scheme == "http"
	} else if useHttp {
		_, err := url.Parse(fmt.Sprint("http://", host, ":", port))
		if err != nil {
			fmt.Fprintln(os.Stderr, "Error parsing URL: ", err)
			flag.Usage()
		}
	}

	cmd := flag.Arg(0)
	var err error
	if useHttp {
		trans, err = thrift.NewTHttpClient(parsedUrl.String())
	} else {
		portStr := fmt.Sprint(port)
		if strings.Contains(host, ":") {
			host, portStr, err = net.SplitHostPort(host)
			if err != nil {
				fmt.Fprintln(os.Stderr, "error with host:", err)
				os.Exit(1)
			}
		}
		trans, err = thrift.NewTSocket(net.JoinHostPort(host, portStr))
		if err != nil {
			fmt.Fprintln(os.Stderr, "error resolving address:", err)
			os.Exit(1)
		}
		if framed {
			trans = thrift.NewTFramedTransport(trans)
		}
	}
	if err != nil {
		fmt.Fprintln(os.Stderr, "Error creating transport", err)
		os.Exit(1)
	}
	defer trans.Close()
	var protocolFactory thrift.TProtocolFactory
	switch protocol {
	case "compact":
		protocolFactory = thrift.NewTCompactProtocolFactory()
		break
	case "simplejson":
		protocolFactory = thrift.NewTSimpleJSONProtocolFactory()
		break
	case "json":
		protocolFactory = thrift.NewTJSONProtocolFactory()
		break
	case "binary", "":
		protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
		break
	default:
		fmt.Fprintln(os.Stderr, "Invalid protocol specified: ", protocol)
		Usage()
		os.Exit(1)
	}
	client := zipkinquery.NewZipkinQueryClientFactory(trans, protocolFactory)
	if err := trans.Open(); err != nil {
		fmt.Fprintln(os.Stderr, "Error opening socket to ", host, ":", port, " ", err)
		os.Exit(1)
	}

	switch cmd {
	case "getTraceIds":
		if flag.NArg()-1 != 1 {
			fmt.Fprintln(os.Stderr, "GetTraceIds requires 1 args")
			flag.Usage()
		}
		arg77 := flag.Arg(1)
		mbTrans78 := thrift.NewTMemoryBufferLen(len(arg77))
		defer mbTrans78.Close()
		_, err79 := mbTrans78.WriteString(arg77)
		if err79 != nil {
			Usage()
			return
		}
		factory80 := thrift.NewTSimpleJSONProtocolFactory()
		jsProt81 := factory80.GetProtocol(mbTrans78)
		argvalue0 := zipkinquery.NewQueryRequest()
		err82 := argvalue0.Read(jsProt81)
		if err82 != nil {
			Usage()
			return
		}
		value0 := argvalue0
		fmt.Print(client.GetTraceIds(value0))
		fmt.Print("\n")
		break
	case "getTraceIdsBySpanName":
		if flag.NArg()-1 != 5 {
			fmt.Fprintln(os.Stderr, "GetTraceIdsBySpanName requires 5 args")
			flag.Usage()
		}
		argvalue0 := flag.Arg(1)
		value0 := argvalue0
		argvalue1 := flag.Arg(2)
		value1 := argvalue1
		argvalue2, err85 := (strconv.ParseInt(flag.Arg(3), 10, 64))
		if err85 != nil {
			Usage()
			return
		}
		value2 := argvalue2
		tmp3, err86 := (strconv.Atoi(flag.Arg(4)))
		if err86 != nil {
			Usage()
			return
		}
		argvalue3 := int32(tmp3)
		value3 := argvalue3
		tmp4, err := (strconv.Atoi(flag.Arg(5)))
		if err != nil {
			Usage()
			return
		}
		argvalue4 := zipkinquery.Order(tmp4)
		value4 := argvalue4
		fmt.Print(client.GetTraceIdsBySpanName(value0, value1, value2, value3, value4))
		fmt.Print("\n")
		break
	case "getTraceIdsByServiceName":
		if flag.NArg()-1 != 4 {
			fmt.Fprintln(os.Stderr, "GetTraceIdsByServiceName requires 4 args")
			flag.Usage()
		}
		argvalue0 := flag.Arg(1)
		value0 := argvalue0
		argvalue1, err88 := (strconv.ParseInt(flag.Arg(2), 10, 64))
		if err88 != nil {
			Usage()
			return
		}
		value1 := argvalue1
		tmp2, err89 := (strconv.Atoi(flag.Arg(3)))
		if err89 != nil {
			Usage()
			return
		}
		argvalue2 := int32(tmp2)
		value2 := argvalue2
		tmp3, err := (strconv.Atoi(flag.Arg(4)))
		if err != nil {
			Usage()
			return
		}
		argvalue3 := zipkinquery.Order(tmp3)
		value3 := argvalue3
		fmt.Print(client.GetTraceIdsByServiceName(value0, value1, value2, value3))
		fmt.Print("\n")
		break
	case "getTraceIdsByAnnotation":
		if flag.NArg()-1 != 6 {
			fmt.Fprintln(os.Stderr, "GetTraceIdsByAnnotation requires 6 args")
			flag.Usage()
		}
		argvalue0 := flag.Arg(1)
		value0 := argvalue0
		argvalue1 := flag.Arg(2)
		value1 := argvalue1
		argvalue2 := []byte(flag.Arg(3))
		value2 := argvalue2
		argvalue3, err93 := (strconv.ParseInt(flag.Arg(4), 10, 64))
		if err93 != nil {
			Usage()
			return
		}
		value3 := argvalue3
		tmp4, err94 := (strconv.Atoi(flag.Arg(5)))
		if err94 != nil {
			Usage()
			return
		}
		argvalue4 := int32(tmp4)
		value4 := argvalue4
		tmp5, err := (strconv.Atoi(flag.Arg(6)))
		if err != nil {
			Usage()
			return
		}
		argvalue5 := zipkinquery.Order(tmp5)
		value5 := argvalue5
		fmt.Print(client.GetTraceIdsByAnnotation(value0, value1, value2, value3, value4, value5))
		fmt.Print("\n")
		break
	case "tracesExist":
		if flag.NArg()-1 != 1 {
			fmt.Fprintln(os.Stderr, "TracesExist requires 1 args")
			flag.Usage()
		}
		arg95 := flag.Arg(1)
		mbTrans96 := thrift.NewTMemoryBufferLen(len(arg95))
		defer mbTrans96.Close()
		_, err97 := mbTrans96.WriteString(arg95)
		if err97 != nil {
			Usage()
			return
		}
		factory98 := thrift.NewTSimpleJSONProtocolFactory()
		jsProt99 := factory98.GetProtocol(mbTrans96)
		containerStruct0 := zipkinquery.NewTracesExistArgs()
		err100 := containerStruct0.ReadField1(jsProt99)
		if err100 != nil {
			Usage()
			return
		}
		argvalue0 := containerStruct0.TraceIds
		value0 := argvalue0
		fmt.Print(client.TracesExist(value0))
		fmt.Print("\n")
		break
	case "getTracesByIds":
		if flag.NArg()-1 != 2 {
			fmt.Fprintln(os.Stderr, "GetTracesByIds requires 2 args")
			flag.Usage()
		}
		arg101 := flag.Arg(1)
		mbTrans102 := thrift.NewTMemoryBufferLen(len(arg101))
		defer mbTrans102.Close()
		_, err103 := mbTrans102.WriteString(arg101)
		if err103 != nil {
			Usage()
			return
		}
		factory104 := thrift.NewTSimpleJSONProtocolFactory()
		jsProt105 := factory104.GetProtocol(mbTrans102)
		containerStruct0 := zipkinquery.NewGetTracesByIdsArgs()
		err106 := containerStruct0.ReadField1(jsProt105)
		if err106 != nil {
			Usage()
			return
		}
		argvalue0 := containerStruct0.TraceIds
		value0 := argvalue0
		arg107 := flag.Arg(2)
		mbTrans108 := thrift.NewTMemoryBufferLen(len(arg107))
		defer mbTrans108.Close()
		_, err109 := mbTrans108.WriteString(arg107)
		if err109 != nil {
			Usage()
			return
		}
		factory110 := thrift.NewTSimpleJSONProtocolFactory()
		jsProt111 := factory110.GetProtocol(mbTrans108)
		containerStruct1 := zipkinquery.NewGetTracesByIdsArgs()
		err112 := containerStruct1.ReadField2(jsProt111)
		if err112 != nil {
			Usage()
			return
		}
		argvalue1 := containerStruct1.Adjust
		value1 := argvalue1
		fmt.Print(client.GetTracesByIds(value0, value1))
		fmt.Print("\n")
		break
	case "getTraceTimelinesByIds":
		if flag.NArg()-1 != 2 {
			fmt.Fprintln(os.Stderr, "GetTraceTimelinesByIds requires 2 args")
			flag.Usage()
		}
		arg113 := flag.Arg(1)
		mbTrans114 := thrift.NewTMemoryBufferLen(len(arg113))
		defer mbTrans114.Close()
		_, err115 := mbTrans114.WriteString(arg113)
		if err115 != nil {
			Usage()
			return
		}
		factory116 := thrift.NewTSimpleJSONProtocolFactory()
		jsProt117 := factory116.GetProtocol(mbTrans114)
		containerStruct0 := zipkinquery.NewGetTraceTimelinesByIdsArgs()
		err118 := containerStruct0.ReadField1(jsProt117)
		if err118 != nil {
			Usage()
			return
		}
		argvalue0 := containerStruct0.TraceIds
		value0 := argvalue0
		arg119 := flag.Arg(2)
		mbTrans120 := thrift.NewTMemoryBufferLen(len(arg119))
		defer mbTrans120.Close()
		_, err121 := mbTrans120.WriteString(arg119)
		if err121 != nil {
			Usage()
			return
		}
		factory122 := thrift.NewTSimpleJSONProtocolFactory()
		jsProt123 := factory122.GetProtocol(mbTrans120)
		containerStruct1 := zipkinquery.NewGetTraceTimelinesByIdsArgs()
		err124 := containerStruct1.ReadField2(jsProt123)
		if err124 != nil {
			Usage()
			return
		}
		argvalue1 := containerStruct1.Adjust
		value1 := argvalue1
		fmt.Print(client.GetTraceTimelinesByIds(value0, value1))
		fmt.Print("\n")
		break
	case "getTraceSummariesByIds":
		if flag.NArg()-1 != 2 {
			fmt.Fprintln(os.Stderr, "GetTraceSummariesByIds requires 2 args")
			flag.Usage()
		}
		arg125 := flag.Arg(1)
		mbTrans126 := thrift.NewTMemoryBufferLen(len(arg125))
		defer mbTrans126.Close()
		_, err127 := mbTrans126.WriteString(arg125)
		if err127 != nil {
			Usage()
			return
		}
		factory128 := thrift.NewTSimpleJSONProtocolFactory()
		jsProt129 := factory128.GetProtocol(mbTrans126)
		containerStruct0 := zipkinquery.NewGetTraceSummariesByIdsArgs()
		err130 := containerStruct0.ReadField1(jsProt129)
		if err130 != nil {
			Usage()
			return
		}
		argvalue0 := containerStruct0.TraceIds
		value0 := argvalue0
		arg131 := flag.Arg(2)
		mbTrans132 := thrift.NewTMemoryBufferLen(len(arg131))
		defer mbTrans132.Close()
		_, err133 := mbTrans132.WriteString(arg131)
		if err133 != nil {
			Usage()
			return
		}
		factory134 := thrift.NewTSimpleJSONProtocolFactory()
		jsProt135 := factory134.GetProtocol(mbTrans132)
		containerStruct1 := zipkinquery.NewGetTraceSummariesByIdsArgs()
		err136 := containerStruct1.ReadField2(jsProt135)
		if err136 != nil {
			Usage()
			return
		}
		argvalue1 := containerStruct1.Adjust
		value1 := argvalue1
		fmt.Print(client.GetTraceSummariesByIds(value0, value1))
		fmt.Print("\n")
		break
	case "getTraceCombosByIds":
		if flag.NArg()-1 != 2 {
			fmt.Fprintln(os.Stderr, "GetTraceCombosByIds requires 2 args")
			flag.Usage()
		}
		arg137 := flag.Arg(1)
		mbTrans138 := thrift.NewTMemoryBufferLen(len(arg137))
		defer mbTrans138.Close()
		_, err139 := mbTrans138.WriteString(arg137)
		if err139 != nil {
			Usage()
			return
		}
		factory140 := thrift.NewTSimpleJSONProtocolFactory()
		jsProt141 := factory140.GetProtocol(mbTrans138)
		containerStruct0 := zipkinquery.NewGetTraceCombosByIdsArgs()
		err142 := containerStruct0.ReadField1(jsProt141)
		if err142 != nil {
			Usage()
			return
		}
		argvalue0 := containerStruct0.TraceIds
		value0 := argvalue0
		arg143 := flag.Arg(2)
		mbTrans144 := thrift.NewTMemoryBufferLen(len(arg143))
		defer mbTrans144.Close()
		_, err145 := mbTrans144.WriteString(arg143)
		if err145 != nil {
			Usage()
			return
		}
		factory146 := thrift.NewTSimpleJSONProtocolFactory()
		jsProt147 := factory146.GetProtocol(mbTrans144)
		containerStruct1 := zipkinquery.NewGetTraceCombosByIdsArgs()
		err148 := containerStruct1.ReadField2(jsProt147)
		if err148 != nil {
			Usage()
			return
		}
		argvalue1 := containerStruct1.Adjust
		value1 := argvalue1
		fmt.Print(client.GetTraceCombosByIds(value0, value1))
		fmt.Print("\n")
		break
	case "getServiceNames":
		if flag.NArg()-1 != 0 {
			fmt.Fprintln(os.Stderr, "GetServiceNames requires 0 args")
			flag.Usage()
		}
		fmt.Print(client.GetServiceNames())
		fmt.Print("\n")
		break
	case "getSpanNames":
		if flag.NArg()-1 != 1 {
			fmt.Fprintln(os.Stderr, "GetSpanNames requires 1 args")
			flag.Usage()
		}
		argvalue0 := flag.Arg(1)
		value0 := argvalue0
		fmt.Print(client.GetSpanNames(value0))
		fmt.Print("\n")
		break
	case "setTraceTimeToLive":
		if flag.NArg()-1 != 2 {
			fmt.Fprintln(os.Stderr, "SetTraceTimeToLive requires 2 args")
			flag.Usage()
		}
		argvalue0, err150 := (strconv.ParseInt(flag.Arg(1), 10, 64))
		if err150 != nil {
			Usage()
			return
		}
		value0 := argvalue0
		tmp1, err151 := (strconv.Atoi(flag.Arg(2)))
		if err151 != nil {
			Usage()
			return
		}
		argvalue1 := int32(tmp1)
		value1 := argvalue1
		fmt.Print(client.SetTraceTimeToLive(value0, value1))
		fmt.Print("\n")
		break
	case "getTraceTimeToLive":
		if flag.NArg()-1 != 1 {
			fmt.Fprintln(os.Stderr, "GetTraceTimeToLive requires 1 args")
			flag.Usage()
		}
		argvalue0, err152 := (strconv.ParseInt(flag.Arg(1), 10, 64))
		if err152 != nil {
			Usage()
			return
		}
		value0 := argvalue0
		fmt.Print(client.GetTraceTimeToLive(value0))
		fmt.Print("\n")
		break
	case "getDataTimeToLive":
		if flag.NArg()-1 != 0 {
			fmt.Fprintln(os.Stderr, "GetDataTimeToLive requires 0 args")
			flag.Usage()
		}
		fmt.Print(client.GetDataTimeToLive())
		fmt.Print("\n")
		break
	case "getDependencies":
		if flag.NArg()-1 != 2 {
			fmt.Fprintln(os.Stderr, "GetDependencies requires 2 args")
			flag.Usage()
		}
		argvalue0, err153 := (strconv.ParseInt(flag.Arg(1), 10, 64))
		if err153 != nil {
			Usage()
			return
		}
		value0 := argvalue0
		argvalue1, err154 := (strconv.ParseInt(flag.Arg(2), 10, 64))
		if err154 != nil {
			Usage()
			return
		}
		value1 := argvalue1
		fmt.Print(client.GetDependencies(value0, value1))
		fmt.Print("\n")
		break
	case "getTopAnnotations":
		if flag.NArg()-1 != 1 {
			fmt.Fprintln(os.Stderr, "GetTopAnnotations requires 1 args")
			flag.Usage()
		}
		argvalue0 := flag.Arg(1)
		value0 := argvalue0
		fmt.Print(client.GetTopAnnotations(value0))
		fmt.Print("\n")
		break
	case "getTopKeyValueAnnotations":
		if flag.NArg()-1 != 1 {
			fmt.Fprintln(os.Stderr, "GetTopKeyValueAnnotations requires 1 args")
			flag.Usage()
		}
		argvalue0 := flag.Arg(1)
		value0 := argvalue0
		fmt.Print(client.GetTopKeyValueAnnotations(value0))
		fmt.Print("\n")
		break
	case "getSpanDurations":
		if flag.NArg()-1 != 3 {
			fmt.Fprintln(os.Stderr, "GetSpanDurations requires 3 args")
			flag.Usage()
		}
		argvalue0, err157 := (strconv.ParseInt(flag.Arg(1), 10, 64))
		if err157 != nil {
			Usage()
			return
		}
		value0 := argvalue0
		argvalue1 := flag.Arg(2)
		value1 := argvalue1
		argvalue2 := flag.Arg(3)
		value2 := argvalue2
		fmt.Print(client.GetSpanDurations(value0, value1, value2))
		fmt.Print("\n")
		break
	case "getServiceNamesToTraceIds":
		if flag.NArg()-1 != 3 {
			fmt.Fprintln(os.Stderr, "GetServiceNamesToTraceIds requires 3 args")
			flag.Usage()
		}
		argvalue0, err160 := (strconv.ParseInt(flag.Arg(1), 10, 64))
		if err160 != nil {
			Usage()
			return
		}
		value0 := argvalue0
		argvalue1 := flag.Arg(2)
		value1 := argvalue1
		argvalue2 := flag.Arg(3)
		value2 := argvalue2
		fmt.Print(client.GetServiceNamesToTraceIds(value0, value1, value2))
		fmt.Print("\n")
		break
	case "":
		Usage()
		break
	default:
		fmt.Fprintln(os.Stderr, "Invalid function ", cmd)
	}
}
Exemplo n.º 14
0
Arquivo: main.go Projeto: crezam/kit
func main() {
	// The addcli presumes no service discovery system, and expects users to
	// provide the direct address of an addsvc. This presumption is reflected in
	// the addcli binary and the the client packages: the -transport.addr flags
	// and various client constructors both expect host:port strings. For an
	// example service with a client built on top of a service discovery system,
	// see profilesvc.

	var (
		httpAddr         = flag.String("http.addr", "", "HTTP address of addsvc")
		grpcAddr         = flag.String("grpc.addr", "", "gRPC (HTTP) address of addsvc")
		thriftAddr       = flag.String("thrift.addr", "", "Thrift address of addsvc")
		thriftProtocol   = flag.String("thrift.protocol", "binary", "binary, compact, json, simplejson")
		thriftBufferSize = flag.Int("thrift.buffer.size", 0, "0 for unbuffered")
		thriftFramed     = flag.Bool("thrift.framed", false, "true to enable framing")
		zipkinAddr       = flag.String("zipkin.addr", "", "Enable Zipkin tracing via a Kafka Collector host:port")
		appdashAddr      = flag.String("appdash.addr", "", "Enable Appdash tracing via an Appdash server host:port")
		lightstepToken   = flag.String("lightstep.token", "", "Enable LightStep tracing via a LightStep access token")
		method           = flag.String("method", "sum", "sum, concat")
	)
	flag.Parse()

	if len(flag.Args()) != 2 {
		fmt.Fprintf(os.Stderr, "usage: addcli [flags] <a> <b>\n")
		os.Exit(1)
	}

	// This is a demonstration client, which supports multiple tracers.
	// Your clients will probably just use one tracer.
	var tracer stdopentracing.Tracer
	{
		if *zipkinAddr != "" {
			collector, err := zipkin.NewKafkaCollector(
				strings.Split(*zipkinAddr, ","),
				zipkin.KafkaLogger(log.NewNopLogger()),
			)
			if err != nil {
				fmt.Fprintf(os.Stderr, "%v\n", err)
				os.Exit(1)
			}
			tracer, err = zipkin.NewTracer(
				zipkin.NewRecorder(collector, false, "localhost:8000", "addcli"),
			)
			if err != nil {
				fmt.Fprintf(os.Stderr, "%v\n", err)
				os.Exit(1)
			}
		} else if *appdashAddr != "" {
			tracer = appdashot.NewTracer(appdash.NewRemoteCollector(*appdashAddr))
		} else if *lightstepToken != "" {
			tracer = lightstep.NewTracer(lightstep.Options{
				AccessToken: *lightstepToken,
			})
			defer lightstep.FlushLightStepTracer(tracer)
		} else {
			tracer = stdopentracing.GlobalTracer() // no-op
		}
	}

	// This is a demonstration client, which supports multiple transports.
	// Your clients will probably just define and stick with 1 transport.

	var (
		service addsvc.Service
		err     error
	)
	if *httpAddr != "" {
		service, err = httpclient.New(*httpAddr, tracer, log.NewNopLogger())
	} else if *grpcAddr != "" {
		conn, err := grpc.Dial(*grpcAddr, grpc.WithInsecure(), grpc.WithTimeout(time.Second))
		if err != nil {
			fmt.Fprintf(os.Stderr, "error: %v", err)
			os.Exit(1)
		}
		defer conn.Close()
		service = grpcclient.New(conn, tracer, log.NewNopLogger())
	} else if *thriftAddr != "" {
		// It's necessary to do all of this construction in the func main,
		// because (among other reasons) we need to control the lifecycle of the
		// Thrift transport, i.e. close it eventually.
		var protocolFactory thrift.TProtocolFactory
		switch *thriftProtocol {
		case "compact":
			protocolFactory = thrift.NewTCompactProtocolFactory()
		case "simplejson":
			protocolFactory = thrift.NewTSimpleJSONProtocolFactory()
		case "json":
			protocolFactory = thrift.NewTJSONProtocolFactory()
		case "binary", "":
			protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
		default:
			fmt.Fprintf(os.Stderr, "error: invalid protocol %q\n", *thriftProtocol)
			os.Exit(1)
		}
		var transportFactory thrift.TTransportFactory
		if *thriftBufferSize > 0 {
			transportFactory = thrift.NewTBufferedTransportFactory(*thriftBufferSize)
		} else {
			transportFactory = thrift.NewTTransportFactory()
		}
		if *thriftFramed {
			transportFactory = thrift.NewTFramedTransportFactory(transportFactory)
		}
		transportSocket, err := thrift.NewTSocket(*thriftAddr)
		if err != nil {
			fmt.Fprintf(os.Stderr, "error: %v\n", err)
			os.Exit(1)
		}
		transport := transportFactory.GetTransport(transportSocket)
		if err := transport.Open(); err != nil {
			fmt.Fprintf(os.Stderr, "error: %v\n", err)
			os.Exit(1)
		}
		defer transport.Close()
		client := thriftadd.NewAddServiceClientFactory(transport, protocolFactory)
		service = thriftclient.New(client)
	} else {
		fmt.Fprintf(os.Stderr, "error: no remote address specified\n")
		os.Exit(1)
	}
	if err != nil {
		fmt.Fprintf(os.Stderr, "error: %v\n", err)
		os.Exit(1)
	}

	switch *method {
	case "sum":
		a, _ := strconv.ParseInt(flag.Args()[0], 10, 64)
		b, _ := strconv.ParseInt(flag.Args()[1], 10, 64)
		v, err := service.Sum(context.Background(), int(a), int(b))
		if err != nil {
			fmt.Fprintf(os.Stderr, "error: %v\n", err)
			os.Exit(1)
		}
		fmt.Fprintf(os.Stdout, "%d + %d = %d\n", a, b, v)

	case "concat":
		a := flag.Args()[0]
		b := flag.Args()[1]
		v, err := service.Concat(context.Background(), a, b)
		if err != nil {
			fmt.Fprintf(os.Stderr, "error: %v\n", err)
			os.Exit(1)
		}
		fmt.Fprintf(os.Stdout, "%q + %q = %q\n", a, b, v)

	default:
		fmt.Fprintf(os.Stderr, "error: invalid method %q\n", method)
		os.Exit(1)
	}
}