...

Source file src/go.opencensus.io/examples/grpc/helloworld_server/main.go

Documentation: go.opencensus.io/examples/grpc/helloworld_server

     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  
    15  //go:generate protoc -I ../proto --go_out=plugins=grpc:../proto ../proto/helloworld.proto
    16  
    17  package main
    18  
    19  import (
    20  	"context"
    21  	"log"
    22  	"math/rand"
    23  	"net"
    24  	"net/http"
    25  	"time"
    26  
    27  	"go.opencensus.io/examples/exporter"
    28  	pb "go.opencensus.io/examples/grpc/proto"
    29  	"go.opencensus.io/plugin/ocgrpc"
    30  	"go.opencensus.io/stats/view"
    31  	"go.opencensus.io/trace"
    32  	"go.opencensus.io/zpages"
    33  	"google.golang.org/grpc"
    34  )
    35  
    36  const port = ":50051"
    37  
    38  // server is used to implement helloworld.GreeterServer.
    39  type server struct{}
    40  
    41  // SayHello implements helloworld.GreeterServer
    42  func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
    43  	ctx, span := trace.StartSpan(ctx, "sleep")
    44  	time.Sleep(time.Duration(rand.Float64() * float64(time.Second)))
    45  	span.End()
    46  	return &pb.HelloReply{Message: "Hello " + in.Name}, nil
    47  }
    48  
    49  func main() {
    50  	// Start z-Pages server.
    51  	go func() {
    52  		mux := http.NewServeMux()
    53  		zpages.Handle(mux, "/debug")
    54  		log.Fatal(http.ListenAndServe("127.0.0.1:8081", mux))
    55  	}()
    56  
    57  	// Register stats and trace exporters to export
    58  	// the collected data.
    59  	view.RegisterExporter(&exporter.PrintExporter{})
    60  
    61  	// Register the views to collect server request count.
    62  	if err := view.Register(ocgrpc.DefaultServerViews...); err != nil {
    63  		log.Fatal(err)
    64  	}
    65  
    66  	lis, err := net.Listen("tcp", port)
    67  	if err != nil {
    68  		log.Fatalf("Failed to listen: %v", err)
    69  	}
    70  
    71  	// Set up a new server with the OpenCensus
    72  	// stats handler to enable stats and tracing.
    73  	s := grpc.NewServer(grpc.StatsHandler(&ocgrpc.ServerHandler{}))
    74  	pb.RegisterGreeterServer(s, &server{})
    75  
    76  	if err := s.Serve(lis); err != nil {
    77  		log.Fatalf("Failed to serve: %v", err)
    78  	}
    79  }
    80  

View as plain text