Skip to content

Commit

Permalink
refactor (gql-middleware): Code improvements and new Prometheus metri…
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavotrott authored Aug 19, 2024
1 parent af644be commit 09cc37f
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 425 deletions.
23 changes: 0 additions & 23 deletions bbb-graphql-middleware/cmd/bbb-graphql-middleware/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"bbb-graphql-middleware/internal/common"
"bbb-graphql-middleware/internal/msgpatch"
"bbb-graphql-middleware/internal/websrv"
"context"
"errors"
Expand All @@ -26,36 +25,18 @@ func main() {
log.SetFormatter(&log.JSONFormatter{})
log := log.WithField("_routine", "main")

if activitiesOverviewEnabled := os.Getenv("BBB_GRAPHQL_MIDDLEWARE_ACTIVITIES_OVERVIEW_ENABLED"); activitiesOverviewEnabled == "true" {
go common.ActivitiesOverviewLogRoutine()
//go common.JsonPatchBenchmarkingLogRoutine()
}

common.InitUniqueID()
log = log.WithField("graphql-middleware-uid", common.GetUniqueID())

log.Infof("Logger level=%v", log.Logger.Level)

//Clear cache from last exec
msgpatch.ClearAllCaches()

// Listen msgs from akka (for example to invalidate connection)
go websrv.StartRedisListener()

if jsonPatchDisabled := os.Getenv("BBB_GRAPHQL_MIDDLEWARE_JSON_PATCH_DISABLED"); jsonPatchDisabled != "" {
log.Infof("Json Patch Disabled!")
}

//if rawDataCacheStorageMode := os.Getenv("BBB_GRAPHQL_MIDDLEWARE_RAW_DATA_CACHE_STORAGE_MODE"); rawDataCacheStorageMode == "file" {
// msgpatch.RawDataCacheStorageMode = "file"
//} else {
// msgpatch.RawDataCacheStorageMode = "memory"
//}
//Force memory cache for now
msgpatch.RawDataCacheStorageMode = "memory"

log.Infof("Raw Data Cache Storage Mode: %s", msgpatch.RawDataCacheStorageMode)

// Websocket listener

//Define IP to listen
Expand Down Expand Up @@ -85,9 +66,6 @@ func main() {
ctx, cancel := context.WithTimeout(r.Context(), 120*time.Second)
defer cancel()

common.ActivitiesOverviewStarted("__WebsocketConnection")
defer common.ActivitiesOverviewCompleted("__WebsocketConnection")

common.HttpConnectionGauge.Inc()
common.HttpConnectionCounter.Inc()
defer common.HttpConnectionGauge.Dec()
Expand All @@ -108,5 +86,4 @@ func main() {

log.Infof("listening on %v:%v", listenIp, listenPort)
log.Fatal(http.ListenAndServe(fmt.Sprintf("%v:%v", listenIp, listenPort), nil))

}
155 changes: 0 additions & 155 deletions bbb-graphql-middleware/internal/common/ActivitiesOverview.go

This file was deleted.

35 changes: 13 additions & 22 deletions bbb-graphql-middleware/internal/common/GlobalState.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,37 +109,28 @@ func RemoveStreamCursorValueCache(cacheKey uint32, delayInSecs time.Duration) {
delete(StreamCursorValueCache, cacheKey)
}

var MaxConnPerSessionToken = -1
var MaxConnPerSessionToken = 3
var MaxConnGlobal = 500

func GetMaxConnectionsPerSessionToken() int {
if MaxConnPerSessionToken == -1 {
maxConnPerSessionToken := 3
if envMaxConnPerSessionToken := os.Getenv("BBB_GRAPHQL_MIDDLEWARE_MAX_CONN_PER_SESSION_TOKEN"); envMaxConnPerSessionToken != "" {
if envMaxConnPerSessionTokenAsInt, err := strconv.Atoi(envMaxConnPerSessionToken); err == nil {
maxConnPerSessionToken = envMaxConnPerSessionTokenAsInt
}
func init() {
if envMaxConnPerSessionToken := os.Getenv("BBB_GRAPHQL_MIDDLEWARE_MAX_CONN_PER_SESSION_TOKEN"); envMaxConnPerSessionToken != "" {
if envMaxConnPerSessionTokenAsInt, err := strconv.Atoi(envMaxConnPerSessionToken); err == nil {
MaxConnPerSessionToken = envMaxConnPerSessionTokenAsInt
}
}

MaxConnPerSessionToken = maxConnPerSessionToken
if envMaxConnGlobal := os.Getenv("BBB_GRAPHQL_MIDDLEWARE_MAX_CONN"); envMaxConnGlobal != "" {
if envMaxConnGlobalAsInt, err := strconv.Atoi(envMaxConnGlobal); err == nil {
MaxConnGlobal = envMaxConnGlobalAsInt
}
}
}

func GetMaxConnectionsPerSessionToken() int {
return MaxConnPerSessionToken
}

var MaxConnGlobal = -1

func GetMaxConnectionsGlobal() int {
if MaxConnGlobal == -1 {
maxConnGlobal := 500
if envMaxConnGlobal := os.Getenv("BBB_GRAPHQL_MIDDLEWARE_MAX_CONN"); envMaxConnGlobal != "" {
if envMaxConnGlobalAsInt, err := strconv.Atoi(envMaxConnGlobal); err == nil {
maxConnGlobal = envMaxConnGlobalAsInt
}
}

MaxConnGlobal = maxConnGlobal
}

return MaxConnGlobal
}

Expand Down
74 changes: 55 additions & 19 deletions bbb-graphql-middleware/internal/common/PrometheusMetrics.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
package common

import "github.com/prometheus/client_golang/prometheus"
import (
"github.com/prometheus/client_golang/prometheus"
"os"
)

var PrometheusAdvancedMetricsEnabled = false

func init() {
if prometheusAdvancedMetricsEnabled := os.Getenv("BBB_GRAPHQL_MIDDLEWARE_PROMETHEUS_ADVANCED_METRICS_ENABLED"); prometheusAdvancedMetricsEnabled == "true" {
PrometheusAdvancedMetricsEnabled = true
}
}

var (
HttpConnectionGauge = prometheus.NewGauge(prometheus.GaugeOpts{
Expand All @@ -22,33 +33,56 @@ var (
},
[]string{"reason"},
)
GqlSubscriptionCounter = prometheus.NewCounterVec(
GqlSubscribeCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "gql_subscription_total",
Help: "Total number of Graphql subscriptions",
},
[]string{"operationName"},
[]string{"type", "operationName"},
)
GqlSubscriptionStreamingCounter = prometheus.NewCounterVec(
GqlMutationsCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "gql_subscription_streaming_total",
Help: "Total number of Graphql subscriptions streaming",
Name: "gql_mutation_total",
Help: "Total number of Graphql mutations",
},
[]string{"operationName"},
)
GqlQueriesCounter = prometheus.NewCounterVec(
GqlReceivedDataCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "gql_query_total",
Help: "Total number of Graphql queries",
Name: "gql_received_data_total",
Help: "Frequency of updates of a given data",
},
[]string{"operationName"},
[]string{"type", "operationName"},
)
GqlMutationsCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "gql_mutation_total",
Help: "Total number of Graphql mutations",
GqlReceivedDataPayloadLength = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "gql_received_data_payload_length",
Help: "Length (number of positions) of received data payload",
Buckets: []float64{
1,
10,
50,
100,
300,
600,
},
},
[]string{"operationName"},
[]string{"type", "operationName"},
)
GqlReceivedDataPayloadSize = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "gql_received_data_payload_size",
Help: "Size (in bytes) of received data payload",
Buckets: []float64{
200,
600,
1200,
5000,
10000,
30000,
},
},
[]string{"type", "operationName"},
)
)

Expand All @@ -57,9 +91,11 @@ func init() {
prometheus.MustRegister(HttpConnectionCounter)
prometheus.MustRegister(WsConnectionAcceptedCounter)
prometheus.MustRegister(WsConnectionRejectedCounter)
prometheus.MustRegister(GqlSubscriptionCounter)
prometheus.MustRegister(GqlSubscriptionStreamingCounter)
prometheus.MustRegister(GqlQueriesCounter)
prometheus.MustRegister(GqlSubscribeCounter)
prometheus.MustRegister(GqlReceivedDataCounter)
prometheus.MustRegister(GqlMutationsCounter)

prometheus.MustRegister(GqlReceivedDataPayloadSize)
if PrometheusAdvancedMetricsEnabled {
prometheus.MustRegister(GqlReceivedDataPayloadLength)
}
}
5 changes: 0 additions & 5 deletions bbb-graphql-middleware/internal/gql_actions/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ import (
"time"
)

var ()

func init() {
}

var graphqlActionsUrl = os.Getenv("BBB_GRAPHQL_MIDDLEWARE_GRAPHQL_ACTIONS_URL")

func GraphqlActionsClient(
Expand Down
Loading

0 comments on commit 09cc37f

Please sign in to comment.