mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Live: Use pure WebSocket transport (#31630)
This commit is contained in:
@@ -215,7 +215,6 @@
|
||||
"@types/md5": "^2.1.33",
|
||||
"@types/react-loadable": "5.5.2",
|
||||
"@types/react-virtualized-auto-sizer": "1.0.0",
|
||||
"@types/sockjs-client": "^1.1.1",
|
||||
"@types/uuid": "8.3.0",
|
||||
"@welldone-software/why-did-you-render": "4.0.6",
|
||||
"abortcontroller-polyfill": "1.4.0",
|
||||
@@ -285,7 +284,6 @@
|
||||
"search-query-parser": "1.5.4",
|
||||
"slate": "0.47.8",
|
||||
"slate-plain-serializer": "0.7.10",
|
||||
"sockjs-client": "^1.4.0",
|
||||
"tether": "1.4.7",
|
||||
"tether-drop": "https://github.com/torkelo/drop/tarball/master",
|
||||
"tinycolor2": "1.4.1",
|
||||
|
||||
@@ -2,7 +2,6 @@ package live
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/centrifugal/centrifuge"
|
||||
@@ -60,7 +59,7 @@ type GrafanaLive struct {
|
||||
// Init initializes the instance.
|
||||
// Required to implement the registry.Service interface.
|
||||
func (g *GrafanaLive) Init() error {
|
||||
logger.Debug("GrafanaLive initing")
|
||||
logger.Debug("GrafanaLive initialization")
|
||||
|
||||
if !g.IsEnabled() {
|
||||
logger.Debug("GrafanaLive feature not enabled, skipping initialization")
|
||||
@@ -113,9 +112,9 @@ func (g *GrafanaLive) Init() error {
|
||||
}
|
||||
})
|
||||
|
||||
// Called when a client writes to the websocket channel.
|
||||
// Called when a client publishes to the websocket channel.
|
||||
// In general, we should prefer writing to the HTTP API, but this
|
||||
// allows some simple prototypes to work quickly
|
||||
// allows some simple prototypes to work quickly.
|
||||
client.OnPublish(func(e centrifuge.PublishEvent, cb centrifuge.PublishCallback) {
|
||||
handler, err := g.GetChannelHandler(e.Channel)
|
||||
if err != nil {
|
||||
@@ -131,15 +130,7 @@ func (g *GrafanaLive) Init() error {
|
||||
return err
|
||||
}
|
||||
|
||||
// SockJS will find the best protocol possible for the browser
|
||||
sockJsPrefix := "/live/sockjs"
|
||||
sockjsHandler := centrifuge.NewSockjsHandler(node, centrifuge.SockjsConfig{
|
||||
HandlerPrefix: sockJsPrefix,
|
||||
WebsocketReadBufferSize: 1024,
|
||||
WebsocketWriteBufferSize: 1024,
|
||||
})
|
||||
|
||||
// Use a direct websocket from go clients
|
||||
// Use a pure websocket transport.
|
||||
wsHandler := centrifuge.NewWebsocketHandler(node, centrifuge.WebsocketConfig{
|
||||
ReadBufferSize: 1024,
|
||||
WriteBufferSize: 1024,
|
||||
@@ -152,31 +143,19 @@ func (g *GrafanaLive) Init() error {
|
||||
return
|
||||
}
|
||||
|
||||
// Centrifuge expects Credentials in context with a current user ID.
|
||||
cred := ¢rifuge.Credentials{
|
||||
UserID: fmt.Sprintf("%d", user.UserId),
|
||||
}
|
||||
newCtx := centrifuge.SetCredentials(ctx.Req.Context(), cred)
|
||||
|
||||
r := ctx.Req.Request
|
||||
r = r.WithContext(newCtx) // Set a user ID
|
||||
r = r.WithContext(newCtx) // Set a user ID.
|
||||
|
||||
// Check if this is a direct websocket connection
|
||||
path := ctx.Req.URL.Path
|
||||
if strings.Contains(path, "live/ws") {
|
||||
wsHandler.ServeHTTP(ctx.Resp, r)
|
||||
return
|
||||
}
|
||||
|
||||
if strings.Contains(path, sockJsPrefix) {
|
||||
sockjsHandler.ServeHTTP(ctx.Resp, r)
|
||||
return
|
||||
}
|
||||
|
||||
// Unknown path
|
||||
ctx.Resp.WriteHeader(404)
|
||||
wsHandler.ServeHTTP(ctx.Resp, r)
|
||||
}
|
||||
|
||||
g.RouteRegister.Any("/live/*", g.WebsocketHandler)
|
||||
g.RouteRegister.Get("/live/ws", g.WebsocketHandler)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -286,7 +265,7 @@ func handleLog(msg centrifuge.LogEntry) {
|
||||
loggerCF.Error(msg.Message, arr...)
|
||||
case centrifuge.LogLevelInfo:
|
||||
loggerCF.Info(msg.Message, arr...)
|
||||
case centrifuge.LogLevelNone:
|
||||
default:
|
||||
loggerCF.Debug(msg.Message, arr...)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import Centrifuge from 'centrifuge/dist/centrifuge.protobuf';
|
||||
import SockJS from 'sockjs-client';
|
||||
import { GrafanaLiveSrv, setGrafanaLiveSrv, getGrafanaLiveSrv, config } from '@grafana/runtime';
|
||||
import { BehaviorSubject } from 'rxjs';
|
||||
import { LiveChannel, LiveChannelScope, LiveChannelAddress } from '@grafana/data';
|
||||
@@ -28,9 +27,10 @@ export class CentrifugeSrv implements GrafanaLiveSrv {
|
||||
readonly scopes: Record<LiveChannelScope, GrafanaLiveScope>;
|
||||
|
||||
constructor() {
|
||||
this.centrifuge = new Centrifuge(`${config.appUrl}live/sockjs`, {
|
||||
// build live url replacing scheme in appUrl.
|
||||
const liveUrl = `${config.appUrl}live/ws`.replace(/^(http)(s)?:\/\//, 'ws$2://');
|
||||
this.centrifuge = new Centrifuge(liveUrl, {
|
||||
debug: true,
|
||||
sockjs: SockJS,
|
||||
});
|
||||
this.centrifuge.setConnectData({
|
||||
sessionId,
|
||||
|
||||
23
yarn.lock
23
yarn.lock
@@ -6876,11 +6876,6 @@
|
||||
"@types/react" "*"
|
||||
immutable "^3.8.2"
|
||||
|
||||
"@types/sockjs-client@^1.1.1":
|
||||
version "1.1.1"
|
||||
resolved "https://registry.yarnpkg.com/@types/sockjs-client/-/sockjs-client-1.1.1.tgz#1ef133b5a79d51447a93ce16164706c0164b5548"
|
||||
integrity sha512-DaTdN4kfPNxu0otmQlxhmYeCjtY8cHmJsU6LqiFOrhytIkx8Txq06PwAWYzha7nMkEyju44a3NDpqCKiHn/NZQ==
|
||||
|
||||
"@types/source-list-map@*":
|
||||
version "0.1.2"
|
||||
resolved "https://registry.yarnpkg.com/@types/source-list-map/-/source-list-map-0.1.2.tgz#0078836063ffaf17412349bba364087e0ac02ec9"
|
||||
@@ -11527,7 +11522,7 @@ debug@3.1.0:
|
||||
dependencies:
|
||||
ms "2.0.0"
|
||||
|
||||
debug@3.2.6, debug@^3.1.0, debug@^3.1.1, debug@^3.2.5, debug@^3.2.6:
|
||||
debug@3.2.6, debug@^3.1.0, debug@^3.1.1, debug@^3.2.6:
|
||||
version "3.2.6"
|
||||
resolved "https://registry.yarnpkg.com/debug/-/debug-3.2.6.tgz#e83d17de16d8a7efb7717edbe5fb10135eee629b"
|
||||
integrity sha512-mel+jf7nrtEl5Pn1Qx46zARXKDpBbvzezse7p7LqINmdoIk8PYP5SySaxEmYv6TZ0JyEKA1hsCId6DIhgITtWQ==
|
||||
@@ -13418,7 +13413,7 @@ fault@^1.0.0:
|
||||
dependencies:
|
||||
format "^0.2.0"
|
||||
|
||||
faye-websocket@^0.11.3, faye-websocket@~0.11.1:
|
||||
faye-websocket@^0.11.3:
|
||||
version "0.11.3"
|
||||
resolved "https://registry.yarnpkg.com/faye-websocket/-/faye-websocket-0.11.3.tgz#5c0e9a8968e8912c286639fde977a8b209f2508e"
|
||||
integrity sha512-D2y4bovYpzziGgbHYtGCMjlJM36vAl/y+xUyn1C+FVx8szd1E+86KwVw6XvYSzOP8iMpm1X0I4xJD+QtUb36OA==
|
||||
@@ -16995,7 +16990,7 @@ json-stringify-safe@^5.0.1, json-stringify-safe@~5.0.1:
|
||||
resolved "https://registry.yarnpkg.com/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz#1296a2d58fd45f19a0f6ce01d65701e2c735b6eb"
|
||||
integrity sha1-Epai1Y/UXxmg9s4B1lcB4sc1tus=
|
||||
|
||||
json3@^3.3.2, json3@^3.3.3:
|
||||
json3@^3.3.3:
|
||||
version "3.3.3"
|
||||
resolved "https://registry.yarnpkg.com/json3/-/json3-3.3.3.tgz#7fc10e375fc5ae42c4705a5cc0aa6f62be305b81"
|
||||
integrity sha512-c7/8mbUsKigAbLkD5B010BK4D9LZm7A1pNItkEwiUZRpIN66exu/e7YQWysGun+TRKaJp8MhemM+VkfWv42aCA==
|
||||
@@ -23824,18 +23819,6 @@ snapdragon@^0.8.1:
|
||||
source-map-resolve "^0.5.0"
|
||||
use "^3.1.0"
|
||||
|
||||
sockjs-client@^1.4.0:
|
||||
version "1.4.0"
|
||||
resolved "https://registry.yarnpkg.com/sockjs-client/-/sockjs-client-1.4.0.tgz#c9f2568e19c8fd8173b4997ea3420e0bb306c7d5"
|
||||
integrity sha512-5zaLyO8/nri5cua0VtOrFXBPK1jbL4+1cebT/mmKA1E1ZXOvJrII75bPu0l0k843G/+iAbhEqzyKr0w/eCCj7g==
|
||||
dependencies:
|
||||
debug "^3.2.5"
|
||||
eventsource "^1.0.7"
|
||||
faye-websocket "~0.11.1"
|
||||
inherits "^2.0.3"
|
||||
json3 "^3.3.2"
|
||||
url-parse "^1.4.3"
|
||||
|
||||
sockjs-client@^1.5.0:
|
||||
version "1.5.0"
|
||||
resolved "https://registry.yarnpkg.com/sockjs-client/-/sockjs-client-1.5.0.tgz#2f8ff5d4b659e0d092f7aba0b7c386bd2aa20add"
|
||||
|
||||
Reference in New Issue
Block a user