1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
package main
import (
"bufio"
"context"
"fmt"
"log"
"net/http"
"os"
"strings"
"time"
"google.golang.org/grpc"
"github.com/grpc-ecosystem/go-grpc-prometheus"
pb "github.com/grpc-ecosystem/go-grpc-prometheus/examples/grpc-server-with-prometheus/protobuf"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func main() {
// Create a metrics registry.
reg := prometheus.NewRegistry()
// Create some standard client metrics.
grpcMetrics := grpc_prometheus.NewClientMetrics()
// Register client metrics to registry.
reg.MustRegister(grpcMetrics)
// Create a insecure gRPC channel to communicate with the server.
conn, err := grpc.Dial(
fmt.Sprintf("localhost:%v", 9093),
grpc.WithUnaryInterceptor(grpcMetrics.UnaryClientInterceptor()),
grpc.WithStreamInterceptor(grpcMetrics.StreamClientInterceptor()),
grpc.WithInsecure(),
)
if err != nil {
log.Fatal(err)
}
defer conn.Close()
// Create a HTTP server for prometheus.
httpServer := &http.Server{Handler: promhttp.HandlerFor(reg, promhttp.HandlerOpts{}), Addr: fmt.Sprintf("0.0.0.0:%d", 9094)}
// Start your http server for prometheus.
go func() {
if err := httpServer.ListenAndServe(); err != nil {
log.Fatal("Unable to start a http server.")
}
}()
// Create a gRPC server client.
client := pb.NewDemoServiceClient(conn)
fmt.Println("Start to call the method called SayHello every 3 seconds")
go func() {
for {
// Call “SayHello” method and wait for response from gRPC Server.
_, err := client.SayHello(context.Background(), &pb.HelloRequest{Name: "Test"})
if err != nil {
log.Printf("Calling the SayHello method unsuccessfully. ErrorInfo: %+v", err)
log.Printf("You should to stop the process")
return
}
time.Sleep(3 * time.Second)
}
}()
scanner := bufio.NewScanner(os.Stdin)
fmt.Println("You can press n or N to stop the process of client")
for scanner.Scan() {
if strings.ToLower(scanner.Text()) == "n" {
os.Exit(0)
}
}
}
|