ad4d222daed42798dfbc023ad7baa8523c9f9483.svn-base 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "github.com/go-kit/kit/log"
  7. kitzipkin "github.com/go-kit/kit/tracing/zipkin"
  8. "github.com/longjoy/micro-go-course/section35/zipkin-kit/client"
  9. "github.com/openzipkin/zipkin-go"
  10. zipkinhttp "github.com/openzipkin/zipkin-go/reporter/http"
  11. "google.golang.org/grpc"
  12. "os"
  13. "testing"
  14. "time"
  15. )
  16. func TestMain(m *testing.M) {
  17. var (
  18. grpcAddr = flag.String("addr", ":9008", "gRPC address")
  19. serviceHost = flag.String("service.host", "localhost", "service ip address")
  20. servicePort = flag.String("service.port", "8009", "service port")
  21. zipkinURL = flag.String("zipkin.url", "http://114.67.98.210:9411/api/v2/spans", "Zipkin server url")
  22. )
  23. flag.Parse()
  24. var logger log.Logger
  25. {
  26. logger = log.NewLogfmtLogger(os.Stderr)
  27. logger = log.With(logger, "ts", log.DefaultTimestampUTC)
  28. logger = log.With(logger, "caller", log.DefaultCaller)
  29. }
  30. var zipkinTracer *zipkin.Tracer
  31. {
  32. var (
  33. err error
  34. hostPort = *serviceHost + ":" + *servicePort
  35. serviceName = "test-service"
  36. useNoopTracer = (*zipkinURL == "")
  37. reporter = zipkinhttp.NewReporter(*zipkinURL)
  38. )
  39. defer reporter.Close()
  40. zEP, _ := zipkin.NewEndpoint(serviceName, hostPort)
  41. zipkinTracer, err = zipkin.NewTracer(
  42. reporter, zipkin.WithLocalEndpoint(zEP), zipkin.WithNoopTracer(useNoopTracer),
  43. )
  44. if err != nil {
  45. logger.Log("err", err)
  46. os.Exit(1)
  47. }
  48. if !useNoopTracer {
  49. logger.Log("tracer", "Zipkin", "type", "Native", "URL", *zipkinURL)
  50. }
  51. }
  52. tr := zipkinTracer
  53. parentSpan := tr.StartSpan("test")
  54. defer parentSpan.Flush()
  55. ctx := zipkin.NewContext(context.Background(), parentSpan)
  56. clientTracer := kitzipkin.GRPCClientTrace(tr)
  57. conn, err := grpc.Dial(*grpcAddr, grpc.WithInsecure(), grpc.WithTimeout(1*time.Second))
  58. if err != nil {
  59. fmt.Println("gRPC dial err:", err)
  60. }
  61. defer conn.Close()
  62. svr := client.StringDiff(conn, clientTracer)
  63. result, err := svr.Diff(ctx, "Add", "ppsdd")
  64. if err != nil {
  65. fmt.Println("Diff error", err.Error())
  66. }
  67. fmt.Println("result =", result)
  68. }