Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ docker run --net=host -it nethermindeth/nethermind:latest
```
3. Run the interceptor:
```bash
docker run --net="host" -e LISTEN_PORT=0.0.0.0:8081 -e SERVICE_TO_PROXY=0.0.0.0:8545 -e LOG_SERVER_URL=0.0.0.0:514 jrpc-interceptor
docker run --net="host" -e LISTEN_PORT=0.0.0.0:8081 -e SERVICE_TO_PROXY=0.0.0.0:8545 -e LOG_SERVER_URL=0.0.0.0:514 -e SERVER_NAME="" jrpc-interceptor
```

Where:
Expand All @@ -32,6 +32,7 @@ Where:
- `PROMETHEUS_URL` - the IP / port of the Prometheus server. Optional, "0.0.0.0:9100" by default.
- `USE_PROMETHEUS` - whether to publish metrics to Prometheus. Optional, "true" by default.
- `LOG_SERVER_DEBUG` - whether to print the logs to stdout. Optional, "true" by default.
- `SERVER_NAME` - name to use for the `server` tag in Prometheus metrics. Optional, empty by default (uses dynamic hostname from syslog messages).

4. Send requests to the interceptor:
```bash
Expand All @@ -54,9 +55,16 @@ go build .
```
3. Run the interceptor:
```bash
./jrpc-interceptor -debug=${LOG_SERVER_DEBUG:-true} -listenSyslog=${LOG_SERVER_URL:-"0.0.0.0:514"} -listenHTTP=${PROMETHEUS_URL:-"0.0.0.0:9100"} -usePrometheus=${USE_PROMETHEUS:-true}
./jrpc-interceptor -debug=${LOG_SERVER_DEBUG:-true} -listenSyslog=${LOG_SERVER_URL:-"0.0.0.0:514"} -listenHTTP=${PROMETHEUS_URL:-"0.0.0.0:9100"} -usePrometheus=${USE_PROMETHEUS:-true} -serverName=${SERVER_NAME:-""}
```

The following command-line flags are available:
- `-debug` - whether to print the logs to stdout. Optional, `false` by default.
- `-listenSyslog` - the IP / port of the syslog server. Optional, `0.0.0.0:514` by default.
- `-listenHTTP` - the IP / port of the Prometheus server. Optional, `0.0.0.0:9100` by default.
- `-usePrometheus` - whether to publish metrics to Prometheus. Optional, `true` by default.
- `-serverName` - name to use for the `server` tag in Prometheus metrics. When empty (default), the hostname from each syslog message is used. Set this when you want to override the default hostname with a custom name.

### Download docker image from github container registry

1. Create a [personal access token ](https://github.com/settings/tokens)(PAT) on github with repo access
Expand Down
4 changes: 2 additions & 2 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/sh
./jrpc-interceptor -debug=${LOG_SERVER_DEBUG:-true} -listenSyslog=${LOG_SERVER_URL:-"0.0.0.0:514"} -listenHTTP=${PROMETHEUS_URL:-"0.0.0.0:9100"} -usePrometheus=${USE_PROMETHEUS:-true} &
envsubst '${SERVICE_TO_PROXY} ${LOG_SERVER_URL} ${LISTEN_PORT}' < /etc/nginx/templates/nginx.conf.template > /etc/nginx/conf.d/default.conf && exec nginx -g 'daemon off;'
./jrpc-interceptor -debug=${LOG_SERVER_DEBUG:-true} -listenSyslog=${LOG_SERVER_URL:-"0.0.0.0:514"} -listenHTTP=${PROMETHEUS_URL:-"0.0.0.0:9100"} -serverName=${SERVER_NAME:-""} -usePrometheus=${USE_PROMETHEUS:-true} &
envsubst '${SERVICE_TO_PROXY} ${LOG_SERVER_URL} ${LISTEN_PORT} ${SERVER_NAME}' < /etc/nginx/templates/nginx.conf.template > /etc/nginx/conf.d/default.conf && exec nginx -g 'daemon off;'
12 changes: 7 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import (
)

var (
debug bool
debug bool
serverName string
)

func receiveSyslog(ch syslog.LogPartsChannel) {
var (
l *logEntry
err error
l *logEntry
err error
)

for msg := range ch {
Expand All @@ -40,13 +41,14 @@ func receiveSyslog(ch syslog.LogPartsChannel) {

func main() {
var (
listenSyslog string
listenHTTP string
listenSyslog string
listenHTTP string
usePrometheus bool
)

flag.StringVar(&listenSyslog, "listenSyslog", "0.0.0.0:514", "ip:port to listen for syslog messages")
flag.StringVar(&listenHTTP, "listenHTTP", "0.0.0.0:9100", "ip:port to listen for http requests")
flag.StringVar(&serverName, "serverName", "", "Server name to use in Prometheus metrics (leave empty for dynamic hostname)")
flag.BoolVar(&usePrometheus, "usePrometheus", true, "Enable posting metrics to Prometheus")
flag.BoolVar(&debug, "debug", false, "Enable debug")
flag.Parse()
Expand Down
51 changes: 28 additions & 23 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,23 @@ import (

type logEntry struct {
// Tags
server string
scheme string
method string
hostname string
status string
protocol string
uri string
jrpc_method string
server string
scheme string
method string
hostname string
status string
protocol string
uri string
jrpc_method string

// Fields
clientIP net.IP
duration float64
bytesSent uint64
bytesReceived uint64
response_duration float64
clientIP net.IP
duration float64
bytesSent uint64
bytesReceived uint64
response_duration float64
}


func parseSyslogMessage(msg format.LogParts) (l *logEntry, err error) {
content := msg["content"].(string)

Expand All @@ -37,15 +36,21 @@ func parseSyslogMessage(msg format.LogParts) (l *logEntry, err error) {
return nil, fmt.Errorf("wrong number of fields in message: %s", content)
}

// Use custom serverName if provided, otherwise use hostname from syslog message
server := msg["hostname"].(string)
if serverName != "" {
server = serverName
}

l = &logEntry{
server: msg["hostname"].(string),
scheme: chunks[1],
hostname: chunks[2],
method: chunks[3],
protocol: chunks[4],
uri: strings.Split(chunks[5], "?")[0],
status: chunks[6],
jrpc_method: chunks[11],
server: server,
scheme: chunks[1],
hostname: chunks[2],
method: chunks[3],
protocol: chunks[4],
uri: strings.Split(chunks[5], "?")[0],
status: chunks[6],
jrpc_method: chunks[11],
}

if l.clientIP = net.ParseIP(chunks[0]); l.clientIP == nil {
Expand All @@ -68,4 +73,4 @@ func parseSyslogMessage(msg format.LogParts) (l *logEntry, err error) {
return nil, fmt.Errorf("unable to parse response duration as float: %s", err)
}
return
}
}
Loading