Skip to content

Commit 2eb2bdb

Browse files
committed
instrument api executor
1 parent fd302a1 commit 2eb2bdb

File tree

9 files changed

+52
-114
lines changed

9 files changed

+52
-114
lines changed

Makefile

+2-6
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,13 @@ DOCKER_RUN_DOC_PORT := 8000
77
DOCKER_RUN_DOC_MOUNT := -v $(CURDIR)/docs:/mkdocs
88
DOCKER_RUN_DOC_OPTS := --rm $(DOCKER_RUN_DOC_MOUNT) -p $(DOCKER_RUN_DOC_PORT):8000
99

10-
all: release
11-
12-
release:
13-
@read -p "Enter new release version: " version; \
14-
./misc/scripts/release.sh $$version
10+
all: test
1511

1612
prepare:
1713
go get github.com/mitchellh/gox
1814

1915
test:
20-
go test $(TESTFOLDERS) -cover
16+
go test $(TESTFOLDERS) -cover -race
2117

2218
web:
2319
./misc/scripts/update_web.sh

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Try our [demo instance](https://centrifugo.herokuapp.com/) on Heroku (password `
3232
### Highlights
3333

3434
* Fast server capable to serve thousands of simultaneous connections
35+
* Simple to install and cross platform – works on Linux, MacOS and Windows
3536
* Easily integrates with existing application – no need to rewrite your code base to introduce real-time events
3637
* HTTP and GRPC API to communicate from your application backend (publish messages in channels etc)
3738
* JSON and binary Protobuf Websocket protocol

docs/content/deploy/packages.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Currently we support versions of the following distributions:
1414
See [full list of available packages](https://packagecloud.io/FZambia/centrifugo) and
1515
[installation instructions](https://packagecloud.io/FZambia/centrifugo/install).
1616

17-
Also note that if your linux distro is not in list you can ask us to package
17+
Also note that if your Linux distro is not in list you can ask us to package
1818
for it or just download appropriate package from packagecloud that fits your
1919
distribution.
2020

docs/content/libraries/client.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
# Client connection libraries
1+
# Client libraries
22

3-
In progress:
3+
These libraries allow your users to connect to Centrifugo from application frontend.
44

5-
* [centrifuge-js](https://github.com/centrifugal/centrifuge-js/tree/c2) – for browser, NodeJS and React Native.
6-
* [centrifuge-go](https://github.com/centrifugal/centrifuge-go/tree/c2) - for Go language.
7-
* [centrifuge-mobile](https://github.com/centrifugal/centrifuge-mobile/c2) - for iOS and Android using `centrifuge-go` as basis and `gomobile` project to create bindings.
5+
* [centrifuge-js](https://github.com/centrifugal/centrifuge-js) – for browser, NodeJS and React Native.
6+
* [centrifuge-go](https://github.com/centrifugal/centrifuge-go) - for Go language.
7+
* [centrifuge-mobile](https://github.com/centrifugal/centrifuge-mobile) - for iOS and Android using `centrifuge-go` as basis and `gomobile` project to create bindings.
88

99
There are no native mobile clients at moment but hopefully this will change soon with open-source community help.

docs/content/server/channels.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Several symbols in channel names are reserved for Centrifugo internal needs:
1717
* `#` – for user channel boundary (see below)
1818
* `*` – for future Centrifugo needs
1919
* `&` – for future Centrifugo needs
20+
* `/` – for future Centrifugo needs
2021

2122
### namespace channel boundary (:)
2223

internal/api/api.go

+37-3
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,30 @@ package api
22

33
import (
44
"context"
5+
"time"
56

67
"github.com/centrifugal/centrifuge"
78
)
89

910
// apiExecutor can run API methods.
1011
type apiExecutor struct {
11-
node *centrifuge.Node
12+
node *centrifuge.Node
13+
protocol string
1214
}
1315

14-
func newAPIExecutor(n *centrifuge.Node) *apiExecutor {
16+
func newAPIExecutor(n *centrifuge.Node, protocol string) *apiExecutor {
1517
return &apiExecutor{
16-
node: n,
18+
node: n,
19+
protocol: protocol,
1720
}
1821
}
1922

2023
// Publish publishes data into channel.
2124
func (h *apiExecutor) Publish(ctx context.Context, cmd *PublishRequest) *PublishResponse {
25+
defer func(started time.Time) {
26+
apiCommandDurationSummary.WithLabelValues(h.protocol, "publish").Observe(time.Since(started).Seconds())
27+
}(time.Now())
28+
2229
ch := cmd.Channel
2330
data := cmd.Data
2431

@@ -60,6 +67,9 @@ func (h *apiExecutor) Publish(ctx context.Context, cmd *PublishRequest) *Publish
6067

6168
// Broadcast publishes the same data into many channels.
6269
func (h *apiExecutor) Broadcast(ctx context.Context, cmd *BroadcastRequest) *BroadcastResponse {
70+
defer func(started time.Time) {
71+
apiCommandDurationSummary.WithLabelValues(h.protocol, "broadcast").Observe(time.Since(started).Seconds())
72+
}(time.Now())
6373

6474
resp := &BroadcastResponse{}
6575

@@ -125,6 +135,9 @@ func (h *apiExecutor) Broadcast(ctx context.Context, cmd *BroadcastRequest) *Bro
125135
// Unsubscribe unsubscribes user from channel and sends unsubscribe
126136
// control message to other nodes so they could also unsubscribe user.
127137
func (h *apiExecutor) Unsubscribe(ctx context.Context, cmd *UnsubscribeRequest) *UnsubscribeResponse {
138+
defer func(started time.Time) {
139+
apiCommandDurationSummary.WithLabelValues(h.protocol, "unsibscribe").Observe(time.Since(started).Seconds())
140+
}(time.Now())
128141

129142
resp := &UnsubscribeResponse{}
130143

@@ -157,6 +170,9 @@ func (h *apiExecutor) Unsubscribe(ctx context.Context, cmd *UnsubscribeRequest)
157170
// Disconnect disconnects user by its ID and sends disconnect
158171
// control message to other nodes so they could also disconnect user.
159172
func (h *apiExecutor) Disconnect(ctx context.Context, cmd *DisconnectRequest) *DisconnectResponse {
173+
defer func(started time.Time) {
174+
apiCommandDurationSummary.WithLabelValues(h.protocol, "disconnect").Observe(time.Since(started).Seconds())
175+
}(time.Now())
160176

161177
resp := &DisconnectResponse{}
162178

@@ -178,6 +194,9 @@ func (h *apiExecutor) Disconnect(ctx context.Context, cmd *DisconnectRequest) *D
178194

179195
// Presence returns response with presence information for channel.
180196
func (h *apiExecutor) Presence(ctx context.Context, cmd *PresenceRequest) *PresenceResponse {
197+
defer func(started time.Time) {
198+
apiCommandDurationSummary.WithLabelValues(h.protocol, "presence").Observe(time.Since(started).Seconds())
199+
}(time.Now())
181200

182201
resp := &PresenceResponse{}
183202

@@ -224,6 +243,9 @@ func (h *apiExecutor) Presence(ctx context.Context, cmd *PresenceRequest) *Prese
224243

225244
// PresenceStats returns response with presence stats information for channel.
226245
func (h *apiExecutor) PresenceStats(ctx context.Context, cmd *PresenceStatsRequest) *PresenceStatsResponse {
246+
defer func(started time.Time) {
247+
apiCommandDurationSummary.WithLabelValues(h.protocol, "presence_stats").Observe(time.Since(started).Seconds())
248+
}(time.Now())
227249

228250
resp := &PresenceStatsResponse{}
229251

@@ -262,6 +284,9 @@ func (h *apiExecutor) PresenceStats(ctx context.Context, cmd *PresenceStatsReque
262284

263285
// History returns response with history information for channel.
264286
func (h *apiExecutor) History(ctx context.Context, cmd *HistoryRequest) *HistoryResponse {
287+
defer func(started time.Time) {
288+
apiCommandDurationSummary.WithLabelValues(h.protocol, "history").Observe(time.Since(started).Seconds())
289+
}(time.Now())
265290

266291
resp := &HistoryResponse{}
267292

@@ -316,6 +341,9 @@ func (h *apiExecutor) History(ctx context.Context, cmd *HistoryRequest) *History
316341

317342
// HistoryRemove removes all history information for channel.
318343
func (h *apiExecutor) HistoryRemove(ctx context.Context, cmd *HistoryRemoveRequest) *HistoryRemoveResponse {
344+
defer func(started time.Time) {
345+
apiCommandDurationSummary.WithLabelValues(h.protocol, "history_remove").Observe(time.Since(started).Seconds())
346+
}(time.Now())
319347

320348
resp := &HistoryRemoveResponse{}
321349

@@ -349,6 +377,9 @@ func (h *apiExecutor) HistoryRemove(ctx context.Context, cmd *HistoryRemoveReque
349377

350378
// Channels returns active channels.
351379
func (h *apiExecutor) Channels(ctx context.Context, cmd *ChannelsRequest) *ChannelsResponse {
380+
defer func(started time.Time) {
381+
apiCommandDurationSummary.WithLabelValues(h.protocol, "channels").Observe(time.Since(started).Seconds())
382+
}(time.Now())
352383

353384
resp := &ChannelsResponse{}
354385

@@ -367,6 +398,9 @@ func (h *apiExecutor) Channels(ctx context.Context, cmd *ChannelsRequest) *Chann
367398

368399
// Info returns information about running nodes.
369400
func (h *apiExecutor) Info(ctx context.Context, cmd *InfoRequest) *InfoResponse {
401+
defer func(started time.Time) {
402+
apiCommandDurationSummary.WithLabelValues(h.protocol, "info").Observe(time.Since(started).Seconds())
403+
}(time.Now())
370404

371405
resp := &InfoResponse{}
372406

internal/api/grpc.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type grpcAPIService struct {
2626
func newGRPCAPIService(n *centrifuge.Node, c GRPCAPIServiceConfig) *grpcAPIService {
2727
return &grpcAPIService{
2828
config: c,
29-
api: newAPIExecutor(n),
29+
api: newAPIExecutor(n, "grpc"),
3030
}
3131
}
3232

internal/api/handler.go

+1-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"io/ioutil"
77
"net/http"
88
"strings"
9-
"time"
109

1110
"github.com/centrifugal/centrifuge"
1211
)
@@ -26,7 +25,7 @@ func NewHandler(n *centrifuge.Node, c Config) *Handler {
2625
return &Handler{
2726
node: n,
2827
config: c,
29-
api: newAPIExecutor(n),
28+
api: newAPIExecutor(n, "http"),
3029
}
3130
}
3231

@@ -38,10 +37,6 @@ func (s *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
3837
default:
3938
}
4039

41-
defer func(started time.Time) {
42-
apiHandlerDurationSummary.Observe(time.Since(started).Seconds())
43-
}(time.Now())
44-
4540
var data []byte
4641
var err error
4742

@@ -83,9 +78,7 @@ func (s *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
8378
http.Error(w, "Bad Request", http.StatusBadRequest)
8479
return
8580
}
86-
now := time.Now()
8781
rep, err := s.handleAPICommand(r.Context(), enc, command)
88-
apiCommandDurationSummary.WithLabelValues(strings.ToLower(MethodType_name[int32(command.Method)])).Observe(time.Since(now).Seconds())
8982
if err != nil {
9083
s.node.Log(centrifuge.NewLogEntry(centrifuge.LogLevelError, "error handling API command", map[string]interface{}{"error": err.Error()}))
9184
http.Error(w, "Internal Server Error", http.StatusInternalServerError)

internal/api/metrics.go

+3-90
Original file line numberDiff line numberDiff line change
@@ -7,102 +7,15 @@ import (
77
var metricsNamespace = "centrifugo"
88

99
var (
10-
messagesSentCount = prometheus.NewCounterVec(prometheus.CounterOpts{
11-
Namespace: metricsNamespace,
12-
Subsystem: "node",
13-
Name: "messages_sent_count",
14-
Help: "Number of messages sent.",
15-
}, []string{"type"})
16-
17-
messagesReceivedCount = prometheus.NewCounterVec(prometheus.CounterOpts{
18-
Namespace: metricsNamespace,
19-
Subsystem: "node",
20-
Name: "messages_received_count",
21-
Help: "Number of messages received.",
22-
}, []string{"type"})
23-
24-
actionCount = prometheus.NewCounterVec(prometheus.CounterOpts{
25-
Namespace: metricsNamespace,
26-
Subsystem: "node",
27-
Name: "action_count",
28-
Help: "Number of node actions called.",
29-
}, []string{"action"})
30-
31-
numClientsGauge = prometheus.NewGauge(prometheus.GaugeOpts{
32-
Namespace: metricsNamespace,
33-
Subsystem: "node",
34-
Name: "num_clients",
35-
Help: "Number of clients connected.",
36-
})
37-
38-
numUsersGauge = prometheus.NewGauge(prometheus.GaugeOpts{
39-
Namespace: metricsNamespace,
40-
Subsystem: "node",
41-
Name: "num_users",
42-
Help: "Number of unique users connected.",
43-
})
44-
45-
buildInfoGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
46-
Namespace: metricsNamespace,
47-
Subsystem: "node",
48-
Name: "build",
49-
Help: "Node build info.",
50-
}, []string{"version"})
51-
52-
numChannelsGauge = prometheus.NewGauge(prometheus.GaugeOpts{
53-
Namespace: metricsNamespace,
54-
Subsystem: "node",
55-
Name: "num_channels",
56-
Help: "Number of channels with one or more subscribers.",
57-
})
58-
59-
replyErrorCount = prometheus.NewCounterVec(prometheus.CounterOpts{
60-
Namespace: metricsNamespace,
61-
Subsystem: "client",
62-
Name: "num_reply_errors",
63-
Help: "Number of errors in replies sent to clients.",
64-
}, []string{"method", "code"})
65-
66-
commandDurationSummary = prometheus.NewSummaryVec(prometheus.SummaryOpts{
67-
Namespace: metricsNamespace,
68-
Subsystem: "client",
69-
Name: "command_duration_seconds",
70-
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
71-
Help: "Client command duration summary.",
72-
}, []string{"method"})
73-
74-
apiHandlerDurationSummary = prometheus.NewSummary(prometheus.SummaryOpts{
75-
Namespace: metricsNamespace,
76-
Subsystem: "http",
77-
Name: "api_request_duration_seconds",
78-
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
79-
Help: "Duration of API handler in general.",
80-
})
81-
8210
apiCommandDurationSummary = prometheus.NewSummaryVec(prometheus.SummaryOpts{
8311
Namespace: metricsNamespace,
84-
Subsystem: "http",
85-
Name: "api_request_command_duration_seconds",
12+
Subsystem: "api",
13+
Name: "command_duration_seconds",
8614
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
8715
Help: "Duration of API per command.",
88-
}, []string{"method"})
89-
90-
transportConnectCount = prometheus.NewCounterVec(prometheus.CounterOpts{
91-
Namespace: metricsNamespace,
92-
Subsystem: "transport",
93-
Name: "connect_count",
94-
Help: "Number of connections to specific transport.",
95-
}, []string{"transport"})
96-
97-
transportMessagesSent = prometheus.NewCounterVec(prometheus.CounterOpts{
98-
Namespace: metricsNamespace,
99-
Subsystem: "transport",
100-
Name: "messages_sent",
101-
Help: "Number of messages sent over specific transport.",
102-
}, []string{"transport"})
16+
}, []string{"protocol", "method"})
10317
)
10418

10519
func init() {
106-
prometheus.MustRegister(apiHandlerDurationSummary)
10720
prometheus.MustRegister(apiCommandDurationSummary)
10821
}

0 commit comments

Comments
 (0)