Skip to content
This repository has been archived by the owner on May 13, 2024. It is now read-only.

Commit

Permalink
fix: logging is broken (#252)
Browse files Browse the repository at this point in the history
* wip: [skip ci] add logging & metrics scripts and config

Signed-off-by: Lin Yang <reaver@flomesh.io>

* wip: [skip ci] logging

Signed-off-by: Lin Yang <reaver@flomesh.io>

* wip: [skip ci] refactoring config change listeners

Signed-off-by: Lin Yang <reaver@flomesh.io>

* wip: [skip ci] add missing config

Signed-off-by: Lin Yang <reaver@flomesh.io>

* chore: initialize version v0.2.4-alpha.2

Signed-off-by: Lin Yang <reaver@flomesh.io>

chore: initialize version v0.2.4-alpha.2

Signed-off-by: Lin Yang <reaver@flomesh.io>

* feat: update logging config

Signed-off-by: Lin Yang <reaver@flomesh.io>

* chore: regenerate deploy yaml

Signed-off-by: Lin Yang <reaver@flomesh.io>

* fix: make package-scripts

Signed-off-by: Lin Yang <reaver@flomesh.io>

* fix: broken values

Signed-off-by: Lin Yang <reaver@flomesh.io>

* build: bump pipy to 0.90.0-185

Signed-off-by: Lin Yang <reaver@flomesh.io>

* wip: [skip ci] update logging config

Signed-off-by: Lin Yang <reaver@flomesh.io>

* chore: regenerate deploy yaml

Signed-off-by: Lin Yang <reaver@flomesh.io>

* fix: make package-scripts

Signed-off-by: Lin Yang <reaver@flomesh.io>

* feat: implement logging & metrics scripts

Signed-off-by: Lin Yang <reaver@flomesh.io>

* fix: logging script

Signed-off-by: Lin Yang <reaver@flomesh.io>

* fix: logging script

Signed-off-by: Lin Yang <reaver@flomesh.io>

* fix: a typo in Helm chart

Signed-off-by: Lin Yang <reaver@flomesh.io>

---------

Signed-off-by: Lin Yang <reaver@flomesh.io>
  • Loading branch information
reaver-flomesh committed Apr 10, 2023
1 parent e6304e8 commit c4d1f1e
Show file tree
Hide file tree
Showing 35 changed files with 878 additions and 356 deletions.
Binary file modified charts/fsm/components/scripts.tar.gz
Binary file not shown.
11 changes: 10 additions & 1 deletion charts/fsm/components/scripts/ingress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
tlsDomains: [],
mapTLSWildcardDomain: {},
tlsWildcardDomains: [],
certificates: {}
certificates: {},
logLogger: null,
},

global.addIssuingCA = ca => (
Expand Down Expand Up @@ -150,6 +151,14 @@
)
),

global.logLogger = config?.logging?.enabled ? new logging.JSONLogger('http').toHTTP(
config.logging.url,
{
headers: config.logging.token === "[UNKNOWN]" ? config.logging.headers : Object.assign(config.logging.headers, {Authorization: config.logging.token}),
batch: config.logging.batch
}
) : null,

global.config = config,

global
Expand Down
15 changes: 14 additions & 1 deletion charts/fsm/components/scripts/ingress/config/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,20 @@
"plugins/reject-http.js",
"plugins/protocol.js",
"plugins/router.js",
"plugins/logging.js",
"plugins/metrics.js",
"plugins/balancer.js",
"plugins/default.js"
]
],

"logging": {
"enabled": false,
"url": "http://localhost:8123/ping",
"headers": {},
"token": "",
"batch": {
"size": 1000,
"separator": "\n"
}
}
}
92 changes: 92 additions & 0 deletions charts/fsm/components/scripts/ingress/plugins/logging.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* MIT License
*
* Copyright (c) since 2021, flomesh.io Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

((
{config, logLogger} = pipy.solve('config.js'),
) => pipy({
_reqHead: null,
_reqBody: '',
_reqTime: 0,
_reqSize: 0,
_resHead: null,
_resTime: 0,
_resSize: 0,
_resBody: '',
_instanceName: os.env.HOSTNAME,

_CONTENT_TYPES: {
'': true,
'text/plain': true,
'application/json': true,
'application/xml': true,
'multipart/form-data': true,
},
})

.pipeline()
.handleMessageStart(
msg => (
_reqHead = msg.head,
_reqBody = msg?.body?.toString?.() || '',
_reqTime = Date.now()
)
)
.handleData(data => _reqSize += data.size)
.chain()
.handleMessageStart(
(msg, contentType) => (
_resHead = msg.head,
_resTime = Date.now(),
contentType = (msg.head.headers && msg.head.headers['content-type']) || '',
_resBody = Boolean(_CONTENT_TYPES[contentType]) ? msg?.body?.toString?.() || '' : ''
)
)
.handleData(data => _resSize += data.size)
.handleMessageEnd(
() => (
logLogger?.log({
req: {
..._reqHead,
body: _reqBody,
},
res: {
..._resHead,
body: _resBody,
},
x_parameters: { aid: '', igid: '', pid: '' },
instanceName: _instanceName,
reqTime: _reqTime,
resTime: _resTime,
endTime: Date.now(),
reqSize: _reqSize,
resSize: _resSize,
remoteAddr: __inbound?.remoteAddress,
remotePort: __inbound?.remotePort,
localAddr: __inbound?.localAddress,
localPort: __inbound?.localPort,
})
)
)

)()
57 changes: 57 additions & 0 deletions charts/fsm/components/scripts/ingress/plugins/metrics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* MIT License
*
* Copyright (c) since 2021, flomesh.io Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

((
metricRequestCount = new stats.Counter('request_count', ["host", "path", "method"]),
metricResponseStatus = new stats.Counter('response_status', ["host", "path", "method", "status"]),
metricResponseLatency = new stats.Histogram(
'request_latency',
new Array(26).fill().map((_,i) => Math.pow(1.5, i+1)|0).concat([Infinity]),
["host", "path", "method"],
),
) => pipy({
_requestTime: 0,
_reqHead: null,
})

.import({
__route: 'main',
})

.pipeline()
.handleMessageStart(
(msg) => (
_reqHead = msg.head,
_requestTime = Date.now(),
metricRequestCount.withLabels(_reqHead.headers.host, _reqHead.path, _reqHead.method).increase()
)
)
.chain()
.handleMessageStart(
msg => (
metricResponseLatency.withLabels(_reqHead.headers.host, _reqHead.path, _reqHead.method).observe(Date.now() - _requestTime),
metricResponseStatus.withLabels(_reqHead.headers.host, _reqHead.path, _reqHead.method, msg.head.status || 200).increase()
)
)
)()
11 changes: 11 additions & 0 deletions charts/fsm/templates/logging-secret.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{{- if .Values.fsm.logging.enabled }}
apiVersion: v1
kind: Secret
metadata:
namespace: {{ include "fsm.namespace" . }}
name: {{ .Values.fsm.logging.secretName }}
type: Opaque
data:
url: {{ .Values.fsm.logging.url | b64enc }}
token: {{ .Values.fsm.logging.token | b64enc }}
{{- end }}
5 changes: 5 additions & 0 deletions charts/fsm/templates/mesh-config-configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ data:
"enabled": {{ .Values.fsm.serviceLB.enabled }}
},
"logging": {
"enabled": {{ .Values.fsm.logging.enabled }},
"secretName": "{{ .Values.fsm.logging.secretName }}"
},
"flb": {
"enabled": {{ .Values.fsm.flb.enabled }},
"secretName": "{{ .Values.fsm.flb.secretName }}"
Expand Down
2 changes: 1 addition & 1 deletion charts/fsm/values.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@
},
"tag": {
"type": "string",
"default": "0.90.0-123",
"default": "0.90.0-185",
"title": "The tag Schema"
}
}
Expand Down
10 changes: 8 additions & 2 deletions charts/fsm/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ fsm:

pipy:
imageName: pipy
tag: 0.90.0-123-nonroot
tag: 0.90.0-185-nonroot

pipyRepo:
imageName: pipy-repo
tag: 0.90.0-123
tag: 0.90.0-185

waitForIt:
imageName: wait-for-it
Expand Down Expand Up @@ -295,6 +295,12 @@ fsm:
adminPort: 6060
logLevel: error

logging:
enabled: false
secretName: fsm-logging-secret
url: http://localhost:8123/ping
token: "[UNKNOWN]"

services:
repo:
name: fsm-repo-service
Expand Down
27 changes: 20 additions & 7 deletions cmd/manager/eventhandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,18 @@ import (
"context"
"github.com/flomesh-io/fsm/pkg/certificate"
"github.com/flomesh-io/fsm/pkg/config"
"github.com/flomesh-io/fsm/pkg/config/listener"
lcfg "github.com/flomesh-io/fsm/pkg/config/listener/config"
"github.com/flomesh-io/fsm/pkg/kube"
"github.com/flomesh-io/fsm/pkg/repo"
corev1 "k8s.io/api/core/v1"
"k8s.io/klog/v2"
"os"
"sigs.k8s.io/controller-runtime/pkg/manager"
"time"
)

func registerEventHandler(mgr manager.Manager, api *kube.K8sAPI, controlPlaneConfigStore *config.Store, certMgr certificate.Manager) {
func registerEventHandler(mgr manager.Manager, api *kube.K8sAPI, configStore *config.Store, certMgr certificate.Manager, repoClient *repo.PipyRepoClient) {

// FIXME: make it configurable
resyncPeriod := 15 * time.Minute
Expand All @@ -48,13 +51,23 @@ func registerEventHandler(mgr manager.Manager, api *kube.K8sAPI, controlPlaneCon
os.Exit(1)
}

listenerConfig := &lcfg.ListenerConfig{
Client: mgr.GetClient(),
K8sApi: api,
ConfigStore: configStore,
CertificateManager: certMgr,
RepoClient: repoClient,
}

listeners := []config.MeshConfigChangeListener{
listener.NewBasicConfigListener(listenerConfig),
listener.NewIngressConfigListener(listenerConfig),
listener.NewProxyProfileConfigListener(listenerConfig),
listener.NewLoggingConfigListener(listenerConfig),
}

config.RegisterConfigurationHanlder(
config.NewFlomeshConfigurationHandler(
mgr.GetClient(),
api,
controlPlaneConfigStore,
certMgr,
),
config.NewFlomeshConfigurationHandler(configStore, listeners),
configmapInformer,
resyncPeriod,
)
Expand Down
3 changes: 2 additions & 1 deletion cmd/manager/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ package main
import (
"github.com/flomesh-io/fsm/pkg/commons"
"github.com/flomesh-io/fsm/pkg/config"
"github.com/flomesh-io/fsm/pkg/config/utils"
"github.com/flomesh-io/fsm/pkg/repo"
"os"
)

func setupHTTP(repoClient *repo.PipyRepoClient, mc *config.MeshConfig) {
if err := config.UpdateIngressHTTPConfig(commons.DefaultIngressBasePath, repoClient, mc); err != nil {
if err := utils.UpdateIngressHTTPConfig(commons.DefaultIngressBasePath, repoClient, mc); err != nil {
os.Exit(1)
}
}
40 changes: 40 additions & 0 deletions cmd/manager/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* MIT License
*
* Copyright (c) since 2021, flomesh.io Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package main

import (
"github.com/flomesh-io/fsm/pkg/commons"
"github.com/flomesh-io/fsm/pkg/config"
"github.com/flomesh-io/fsm/pkg/config/utils"
"github.com/flomesh-io/fsm/pkg/kube"
"github.com/flomesh-io/fsm/pkg/repo"
"os"
)

func setupLogging(api *kube.K8sAPI, repoClient *repo.PipyRepoClient, mc *config.MeshConfig) {
if err := utils.UpdateLoggingConfig(api, commons.DefaultIngressBasePath, repoClient, mc); err != nil {
os.Exit(1)
}
}
5 changes: 4 additions & 1 deletion cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ func main() {
// setup TLS config
setupTLS(certMgr, repoClient, mc)

// setup Logging
setupLogging(k8sApi, repoClient, mc)

// create a new manager for controllers
mgr := newManager(kubeconfig, options)

Expand All @@ -133,7 +136,7 @@ func main() {
// register webhooks
registerToWebhookServer(mgr, k8sApi, controlPlaneConfigStore)

registerEventHandler(mgr, k8sApi, controlPlaneConfigStore, certMgr)
registerEventHandler(mgr, k8sApi, controlPlaneConfigStore, certMgr, repoClient)

// add endpoints for Liveness and Readiness check
addLivenessAndReadinessCheck(mgr)
Expand Down
Loading

0 comments on commit c4d1f1e

Please sign in to comment.