Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

70 wiersze
1.9 KiB

  1. // Copyright 2017, OpenCensus Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "log"
  17. "os"
  18. "time"
  19. "go.opencensus.io/examples/exporter"
  20. pb "go.opencensus.io/examples/grpc/proto"
  21. "go.opencensus.io/plugin/ocgrpc"
  22. "go.opencensus.io/stats/view"
  23. "golang.org/x/net/context"
  24. "google.golang.org/grpc"
  25. )
  26. const (
  27. address = "localhost:50051"
  28. defaultName = "world"
  29. )
  30. func main() {
  31. // Register stats and trace exporters to export
  32. // the collected data.
  33. view.RegisterExporter(&exporter.PrintExporter{})
  34. // Register the view to collect gRPC client stats.
  35. if err := view.Register(ocgrpc.DefaultClientViews...); err != nil {
  36. log.Fatal(err)
  37. }
  38. // Set up a connection to the server with the OpenCensus
  39. // stats handler to enable stats and tracing.
  40. conn, err := grpc.Dial(address, grpc.WithStatsHandler(&ocgrpc.ClientHandler{}), grpc.WithInsecure())
  41. if err != nil {
  42. log.Fatalf("Cannot connect: %v", err)
  43. }
  44. defer conn.Close()
  45. c := pb.NewGreeterClient(conn)
  46. // Contact the server and print out its response.
  47. name := defaultName
  48. if len(os.Args) > 1 {
  49. name = os.Args[1]
  50. }
  51. view.SetReportingPeriod(time.Second)
  52. for {
  53. r, err := c.SayHello(context.Background(), &pb.HelloRequest{Name: name})
  54. if err != nil {
  55. log.Printf("Could not greet: %v", err)
  56. } else {
  57. log.Printf("Greeting: %s", r.Message)
  58. }
  59. time.Sleep(2 * time.Second)
  60. }
  61. }