-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
main.go
126 lines (89 loc) · 2.71 KB
/
main.go
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"context"
"fmt"
"log"
"net"
"net/http"
"github.com/go-redis/redis"
"github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"google.golang.org/grpc"
"github.com/lucperkins/colossus/proto/auth"
)
const (
PORT = 8888
PROMETHEUS_PORT = 9092
)
var (
metricsRegistry = prometheus.NewRegistry()
grpcMetrics = grpc_prometheus.NewServerMetrics()
authCounter = prometheus.NewCounter(prometheus.CounterOpts{
Name: "auth_svc_success",
Help: "Auth success counter",
})
failCounter = prometheus.NewCounter(prometheus.CounterOpts{
Name: "auth_svc_fail",
Help: "Auth fail counter",
})
)
type authHandler struct {
redisClient *redis.Client
}
func (h *authHandler) Authenticate(ctx context.Context, req *auth.AuthRequest) (*auth.AuthResponse, error) {
var authenticated bool
password := req.Password
log.Printf("Request received for the password %s", password)
value, err := h.redisClient.Get("password").Result()
if err != nil {
log.Fatalf("Could not fetch value from Redis: %v", err)
}
authenticated = password == value
if authenticated {
log.Printf("Password %s succeeded", password)
authCounter.Inc()
} else {
log.Printf("Password %s failed", password)
failCounter.Inc()
}
return &auth.AuthResponse{Authenticated: authenticated}, nil
}
func main() {
log.Printf("Starting up the gRPC auth server on localhost:%d", PORT)
log.Print("Attempting to connect to Redis")
redisClient := redis.NewClient(&redis.Options{
Addr: "colossus-redis-cluster:6379",
})
_, err := redisClient.Ping().Result()
if err != nil {
log.Fatalf("Could not connect to Redis cluster: %v", err)
}
log.Print("Successfully connected to Redis")
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", PORT))
if err != nil {
log.Fatalf("Failed to listen: %v", err)
}
log.Print("Successfully created TCP listener")
server := grpc.NewServer(
grpc.UnaryInterceptor(grpc_prometheus.UnaryServerInterceptor),
)
authServer := authHandler{
redisClient: redisClient,
}
httpServer := &http.Server{
Handler: promhttp.HandlerFor(metricsRegistry, promhttp.HandlerOpts{}),
Addr: fmt.Sprintf("0.0.0.0:%d", PROMETHEUS_PORT),
}
auth.RegisterAuthServiceServer(server, &authServer)
grpcMetrics.InitializeMetrics(server)
metricsRegistry.MustRegister(grpcMetrics, authCounter, failCounter)
log.Print("Successfully registered with Prometheus")
go func() {
log.Print("Starting up HTTP server for Prometheus metrics collection")
if err := httpServer.ListenAndServe(); err != nil {
log.Fatalf("Unable to start HTTP server for Prometheus metrics: %v", err)
}
}()
log.Fatal(server.Serve(listener))
}