From 4546ec1a0a77a0305fd6b980581102e5e1711854 Mon Sep 17 00:00:00 2001 From: Minh Vuong <38932626+vuongxuongminh@users.noreply.github.com> Date: Wed, 11 May 2022 16:02:05 +0700 Subject: [PATCH 1/3] feat: bump versions (#35) --- charts/gbox/Chart.yaml | 4 ++-- charts/gbox/README.md | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/charts/gbox/Chart.yaml b/charts/gbox/Chart.yaml index 6fe8acb..ffd89e9 100644 --- a/charts/gbox/Chart.yaml +++ b/charts/gbox/Chart.yaml @@ -2,8 +2,8 @@ apiVersion: v2 name: gbox description: GBox Helm chart for Kubernetes. GBox is a reverse proxy in front of any GraphQL server for caching, securing and monitoring. type: application -version: 1.0.2 -appVersion: "v1.0.4" +version: 1.0.3 +appVersion: "v1.0.5" sources: - https://github.com/gbox-proxy/gbox dependencies: diff --git a/charts/gbox/README.md b/charts/gbox/README.md index 3fd8713..3611486 100644 --- a/charts/gbox/README.md +++ b/charts/gbox/README.md @@ -1,6 +1,6 @@ # GBox Chart for Kubernetes -![Version: 1.0.2](https://img.shields.io/badge/Version-1.0.2-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square) ![AppVersion: v1.0.4](https://img.shields.io/badge/AppVersion-v1.0.4-informational?style=flat-square) +![Version: 1.0.3](https://img.shields.io/badge/Version-1.0.3-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square) ![AppVersion: v1.0.5](https://img.shields.io/badge/AppVersion-v1.0.5-informational?style=flat-square) GBox Helm chart for Kubernetes. GBox is a reverse proxy in front of any GraphQL server for caching, securing and monitoring. From a03217e863f75165bf003ea6d7e77943bf5af71a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tr=E1=BA=A7n=20=C4=90=E1=BB=A9c=20Ph=C3=BA?= <30786617+Phu96@users.noreply.github.com> Date: Thu, 12 May 2022 14:21:45 +0700 Subject: [PATCH 2/3] feat: support scraping metrics by prometheus(#36) (#37) --- charts/gbox/Chart.yaml | 2 +- charts/gbox/README.md | 16 +++++++--- charts/gbox/templates/deployment.yaml | 4 +-- charts/gbox/templates/servicemonitor.yaml | 39 +++++++++++++++++++++++ charts/gbox/values.yaml | 32 ++++++++++++++++++- 5 files changed, 84 insertions(+), 9 deletions(-) create mode 100644 charts/gbox/templates/servicemonitor.yaml diff --git a/charts/gbox/Chart.yaml b/charts/gbox/Chart.yaml index ffd89e9..5e9abba 100644 --- a/charts/gbox/Chart.yaml +++ b/charts/gbox/Chart.yaml @@ -2,7 +2,7 @@ apiVersion: v2 name: gbox description: GBox Helm chart for Kubernetes. GBox is a reverse proxy in front of any GraphQL server for caching, securing and monitoring. type: application -version: 1.0.3 +version: 1.0.4 appVersion: "v1.0.5" sources: - https://github.com/gbox-proxy/gbox diff --git a/charts/gbox/README.md b/charts/gbox/README.md index 3611486..f175f1b 100644 --- a/charts/gbox/README.md +++ b/charts/gbox/README.md @@ -1,6 +1,6 @@ # GBox Chart for Kubernetes -![Version: 1.0.3](https://img.shields.io/badge/Version-1.0.3-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square) ![AppVersion: v1.0.5](https://img.shields.io/badge/AppVersion-v1.0.5-informational?style=flat-square) +![Version: 1.0.4](https://img.shields.io/badge/Version-1.0.4-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square) ![AppVersion: v1.0.5](https://img.shields.io/badge/AppVersion-v1.0.5-informational?style=flat-square) GBox Helm chart for Kubernetes. GBox is a reverse proxy in front of any GraphQL server for caching, securing and monitoring. @@ -50,15 +50,21 @@ To install the chart with the release name `my-release`, run the following comma | ingress.annotations | object | `{}` | Annotations to be added to the ingress. | | ingress.className | string | `""` | Ingress [class name](https://kubernetes.io/docs/concepts/services-networking/ingress/#ingress-class). | | ingress.enabled | bool | `false` | Enable [ingress](https://kubernetes.io/docs/concepts/services-networking/ingress/). | -| ingress.hosts[0].host | string | `"chart-example.local"` | | -| ingress.hosts[0].paths[0].path | string | `"/"` | | -| ingress.hosts[0].paths[0].pathType | string | `"ImplementationSpecific"` | | +| ingress.hosts | list | See [values.yaml](values.yaml). | Ingress host configuration. | | ingress.tls | list | See [values.yaml](values.yaml). | Ingress TLS configuration. | | metrics.enabled | bool | `true` | Whether enable Prometheus metric endpoint or not | | metrics.path | string | `"/metrics"` | Url path of metric endpoint. | +| metrics.serviceMonitor.additionalLabels | object | `{}` | Additional labels that can be used so ServiceMonitor resource(s) can be discovered by Prometheus | +| metrics.serviceMonitor.enabled | bool | `false` | Create ServiceMonitor resource(s) for scraping metrics using PrometheusOperator. When enabled @param metrics.enabled must be set to true | +| metrics.serviceMonitor.honorLabels | bool | `false` | Specify honorLabels parameter to add the scrape endpoint | +| metrics.serviceMonitor.interval | string | `"30s"` | The interval at which metrics should be scraped | +| metrics.serviceMonitor.metricRelabelings | list | `[]` | Metrics RelabelConfigs to apply to samples before ingestion. | +| metrics.serviceMonitor.namespace | string | `""` | The namespace in which the ServiceMonitor will be created | +| metrics.serviceMonitor.relabellings | list | `[]` | | +| metrics.serviceMonitor.scrapeTimeout | string | `""` | The timeout after which the scrape is ended | | nameOverride | string | `""` | A name in place of the chart name for `app:` labels. | | nodeSelector | object | `{}` | [Node selector](https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/#nodeselector) configuration. | -| podAnnotations | object | `{}` | Annotations to be added to pods. | +| podAnnotations | object | See [values.yaml](values.yaml). | Annotations to be added to pods. | | podSecurityContext | object | `{}` | Pod [security context](https://kubernetes.io/docs/tasks/configure-pod-container/security-context/#set-the-security-context-for-a-pod). See the [API reference](https://kubernetes.io/docs/reference/kubernetes-api/workload-resources/pod-v1/#security-context) for details. | | redis.architecture | string | `"standalone"` | Set Redis architecture standalone or replication. | | redis.auth.password | string | `"!ChangeMe!"` | | diff --git a/charts/gbox/templates/deployment.yaml b/charts/gbox/templates/deployment.yaml index ac3a79d..7cb5d95 100644 --- a/charts/gbox/templates/deployment.yaml +++ b/charts/gbox/templates/deployment.yaml @@ -13,9 +13,9 @@ spec: {{- include "gbox.selectorLabels" . | nindent 6 }} template: metadata: - {{- with .Values.podAnnotations }} + {{- if .Values.podAnnotations }} annotations: - {{- toYaml . | nindent 8 }} + {{- tpl (toYaml .Values.podAnnotations) . | nindent 8 }} {{- end }} labels: {{- include "gbox.selectorLabels" . | nindent 8 }} diff --git a/charts/gbox/templates/servicemonitor.yaml b/charts/gbox/templates/servicemonitor.yaml new file mode 100644 index 0000000..ff793ef --- /dev/null +++ b/charts/gbox/templates/servicemonitor.yaml @@ -0,0 +1,39 @@ +{{- if and .Values.metrics.enabled .Values.metrics.serviceMonitor.enabled }} +apiVersion: monitoring.coreos.com/v1 +kind: ServiceMonitor +metadata: + name: {{ include "gbox.fullname" . }} + {{- if .Values.metrics.serviceMonitor.namespace }} + namespace: {{ .Values.metrics.serviceMonitor.namespace }} + {{- else }} + namespace: {{ .Release.Namespace | quote }} + {{- end }} + labels: {{- include "gbox.labels" . | nindent 4 }} + {{- if .Values.metrics.serviceMonitor.additionalLabels }} + {{- include .Values.metrics.serviceMonitor.additionalLabels . | nindent 4 }} + {{- end }} +spec: + endpoints: + - port: http + path: {{ .Values.metrics.path }} + {{- if .Values.metrics.serviceMonitor.interval }} + interval: {{ .Values.metrics.serviceMonitor.interval }} + {{- end }} + {{- if .Values.metrics.serviceMonitor.scrapeTimeout }} + scrapeTimeout: {{ .Values.metrics.serviceMonitor.scrapeTimeout }} + {{- end }} + {{- if .Values.metrics.serviceMonitor.honorLabels }} + honorLabels: {{ .Values.metrics.serviceMonitor.honorLabels }} + {{- end }} + {{- if .Values.metrics.serviceMonitor.relabellings }} + relabelings: {{- toYaml .Values.metrics.serviceMonitor.relabellings | nindent 6 }} + {{- end }} + {{- if .Values.metrics.serviceMonitor.metricRelabelings }} + metricRelabelings: {{- toYaml .Values.metrics.serviceMonitor.metricRelabelings | nindent 6 }} + {{- end }} + namespaceSelector: + matchNames: + - {{ .Release.Namespace }} + selector: + matchLabels: {{- include "gbox.selectorLabels" . | nindent 6 }} +{{- end }} \ No newline at end of file diff --git a/charts/gbox/values.yaml b/charts/gbox/values.yaml index df3950f..bfba699 100644 --- a/charts/gbox/values.yaml +++ b/charts/gbox/values.yaml @@ -25,7 +25,11 @@ serviceAccount: name: "" # -- Annotations to be added to pods. -podAnnotations: {} +# @default -- See [values.yaml](values.yaml). +podAnnotations: + prometheus.io/scrape: "true" + prometheus.io/port: "80" + prometheus.io/path: "{{ .Values.metrics.path }}" # -- Pod [security context](https://kubernetes.io/docs/tasks/configure-pod-container/security-context/#set-the-security-context-for-a-pod). # See the [API reference](https://kubernetes.io/docs/reference/kubernetes-api/workload-resources/pod-v1/#security-context) for details. @@ -195,3 +199,29 @@ metrics: # -- Url path of metric endpoint. path: /metrics + + # Prometheus Service Monitor + serviceMonitor: + # -- Create ServiceMonitor resource(s) for scraping metrics using PrometheusOperator. When enabled @param metrics.enabled must be set to true + enabled: false + + # -- The namespace in which the ServiceMonitor will be created + namespace: "" + + # -- The interval at which metrics should be scraped + interval: 30s + + # -- The timeout after which the scrape is ended + scrapeTimeout: "" + + # -- Metrics RelabelConfigs to apply to samples before scraping. + relabellings: [] + + # -- Metrics RelabelConfigs to apply to samples before ingestion. + metricRelabelings: [] + + # -- Specify honorLabels parameter to add the scrape endpoint + honorLabels: false + + # -- Additional labels that can be used so ServiceMonitor resource(s) can be discovered by Prometheus + additionalLabels: {} From e5adb7126b728ffd2894ad45523df4a27dcfb91f Mon Sep 17 00:00:00 2001 From: Minh Vuong <38932626+vuongxuongminh@users.noreply.github.com> Date: Sat, 14 May 2022 22:05:29 +0700 Subject: [PATCH 3/3] fix: missing ws message (#38) --- router.go | 10 +++++----- ws.go | 35 ++++++++++++++++++----------------- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/router.go b/router.go index 566b1f8..e592a98 100644 --- a/router.go +++ b/router.go @@ -99,17 +99,17 @@ func (h *Handler) GraphQLHandle(w http.ResponseWriter, r *http.Request) { return } - h.addMetricsBeginRequest(gqlRequest) - defer func(startedAt time.Time) { - h.addMetricsEndRequest(gqlRequest, time.Since(startedAt)) - }(time.Now()) - if err = h.validateGraphqlRequest(gqlRequest); err != nil { reporter.error = writeResponseErrors(err, w) return } + h.addMetricsBeginRequest(gqlRequest) + defer func(startedAt time.Time) { + h.addMetricsEndRequest(gqlRequest, time.Since(startedAt)) + }(time.Now()) + n := r.Context().Value(nextHandlerCtxKey).(caddyhttp.Handler) if h.Caching != nil { diff --git a/ws.go b/ws.go index a3e08ef..70431d2 100644 --- a/ws.go +++ b/ws.go @@ -95,27 +95,28 @@ func (c *wsConn) Read(b []byte) (n int, err error) { buff.Reset() buff.Write(b[:n]) - r := wsutil.NewServerSideReader(buff) - - if _, e := r.NextFrame(); e != nil { - return n, err - } + for { + msg := new(wsMessage) + request := new(graphql.Request) + data, _, e := wsutil.ReadClientData(buff) - decoder := json.NewDecoder(r) - msg := &wsMessage{} + if e != nil { + return n, err + } - if e := decoder.Decode(msg); e != nil { - return n, err - } + if e = json.Unmarshal(data, msg); e != nil { + continue + } - if msg.Type == "subscribe" || msg.Type == "start" { - request := new(graphql.Request) + if msg.Type != "subscribe" && msg.Type != "start" { + continue + } - if e := json.Unmarshal(msg.Payload, request); e != nil { - return n, err + if e = json.Unmarshal(msg.Payload, request); e != nil { + continue } - if e := c.onWsSubscribe(request); e != nil { + if e = c.onWsSubscribe(request); e != nil { c.writeErrorMessage(msg.ID, e) c.writeCompleteMessage(msg.ID) @@ -124,9 +125,9 @@ func (c *wsConn) Read(b []byte) (n int, err error) { c.request = request c.subscribeAt = time.Now() - } - return n, err + return n, err + } } func (c *wsConn) writeErrorMessage(id interface{}, errMsg error) error {