Skip to content

Commit 377309e

Browse files
Merge pull request #234 from EUMETNET/issue_233_avoid_excessive_request_logging
Avoid excessive request logging
2 parents f7720d9 + 1819925 commit 377309e

File tree

4 files changed

+47
-28
lines changed

4 files changed

+47
-28
lines changed

datastore/datastore/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,10 @@ The following environment variables are supported:
153153
| `ENABLE_SSL` | No | `disable` | Weather to use ssl for connection |
154154
| `DYNAMICTIME` | No | `true` | Whether the valid time range is _dynamic_ or _static_ (defined below). |
155155
| `LOTIME` | No | `86400` | The _earliest_ valid time as seconds to be either [1] subtracted from the current time (if the valid time range is _dynamic_) or [2] added to UNIX epoch (1970-01-01T00:00:00Z) (if the valid time range is _static_). In the case of a _static_ valid time range, the `LOTIME` can optionally be specified as an ISO-8601 datetime of the exact form `2023-10-10T00:00:00Z`. |
156-
| `HITIME` | No | `-600` | Same as `LOTIME`, but for the _latest_ valid time. Note a default leeway of 10 minutes into the future to reduce risk of missing visual observations. |
156+
| `HITIME` | No | `-600` | Same as `LOTIME`, but for the _latest_ valid time. Note a default leeway of 10 minutes into the future to reduce risk of missing visual observations. |
157157
| `CLEANUPINTERVAL` | No | `86400` | The minimum time duration in seconds between automatic cleanups (like removing obsolete observations from the physical store). |
158158
| `PUTOBSLIMIT` | No | `100000` | Maximum number of observations allowed in a single call to `PutObservations`. |
159+
| `LOGREQSTATS` | No | `false` | Whether request stats (like client IP and processing time) will be logged using log.Printf(). |
159160

160161
**TODO:** Ensure that these variables are [passed properly](https://docs.docker.com/compose/environment-variables/set-environment-variables/) to the relevant `docker compose`
161162
commands. Any secrets should be passed using a [special mechanism](https://docs.docker.com/compose/use-secrets/), etc.

datastore/datastore/main/main.go

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,24 @@ import (
88
"datastore/storagebackend"
99
"datastore/storagebackend/postgresql"
1010
"fmt"
11-
"github.com/prometheus/client_golang/prometheus"
12-
"github.com/prometheus/client_golang/prometheus/collectors"
13-
"github.com/prometheus/client_golang/prometheus/promhttp"
1411
"log"
1512
"net"
13+
"strings"
1614
"time"
1715

16+
"github.com/prometheus/client_golang/prometheus"
17+
"github.com/prometheus/client_golang/prometheus/collectors"
18+
"github.com/prometheus/client_golang/prometheus/promhttp"
19+
1820
// gRPC
1921
"google.golang.org/grpc"
2022
"google.golang.org/grpc/health"
2123
"google.golang.org/grpc/health/grpc_health_v1"
2224
"google.golang.org/grpc/peer"
2325

2426
// Monitoring
25-
"datastore/metrics"
27+
promservermetrics "datastore/metrics"
28+
2629
grpcprometheus "github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus"
2730

2831
_ "expvar"
@@ -44,22 +47,6 @@ func createStorageBackend() (storagebackend.StorageBackend, error) {
4447

4548
func main() {
4649

47-
reqTimeLogger := func(
48-
ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
49-
handler grpc.UnaryHandler) (interface{}, error) {
50-
start := time.Now()
51-
resp, err := handler(ctx, req)
52-
reqTime := time.Since(start)
53-
if info.FullMethod != "/grpc.health.v1.Health/Check" {
54-
var clientIp = "unknown"
55-
if p, ok := peer.FromContext(ctx); ok {
56-
clientIp = p.Addr.String()
57-
}
58-
log.Printf("time for method %q: %d ms. Client ip: %s", info.FullMethod, reqTime.Milliseconds(), clientIp)
59-
}
60-
return resp, err
61-
}
62-
6350
grpcMetrics := grpcprometheus.NewServerMetrics(
6451
grpcprometheus.WithServerHandlingTimeHistogram(
6552
grpcprometheus.WithHistogramBuckets([]float64{0.001, 0.01, 0.1, 0.3, 0.6, 1, 3, 6, 9, 20, 30, 60, 90, 120}),
@@ -77,15 +64,45 @@ func main() {
7764

7865
go promservermetrics.TrackUptime()
7966

80-
// create gRPC server with middleware
81-
server := grpc.NewServer(
82-
grpc.ChainUnaryInterceptor(
83-
reqTimeLogger,
67+
// define RPC interceptor to log request time and client IP
68+
reqStatsLogger := func(
69+
ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
70+
handler grpc.UnaryHandler) (interface{}, error) {
71+
start := time.Now()
72+
resp, err := handler(ctx, req)
73+
reqTime := time.Since(start)
74+
if info.FullMethod != "/grpc.health.v1.Health/Check" {
75+
var clientIp = "unknown"
76+
if p, ok := peer.FromContext(ctx); ok {
77+
clientIp = p.Addr.String()
78+
}
79+
log.Printf(
80+
"time for method %q: %d ms. Client ip: %s",
81+
info.FullMethod, reqTime.Milliseconds(), clientIp)
82+
}
83+
return resp, err
84+
}
85+
86+
// define RPC interceptors to use
87+
interceptors := func() []grpc.UnaryServerInterceptor {
88+
// initialize with standard interceptors
89+
icpts := []grpc.UnaryServerInterceptor{
8490
promservermetrics.InFlightRequestInterceptor,
8591
promservermetrics.ResponseSizeUnaryInterceptor,
8692
grpcMetrics.UnaryServerInterceptor(),
87-
),
88-
)
93+
}
94+
95+
// optionally prepend interceptor to log request stats (WARNING: may potentially use a lot
96+
// of disk space, so disabled by default)
97+
logReqStats := strings.ToLower(common.Getenv("LOGREQSTATS", "false"))
98+
if (logReqStats != "false") && (logReqStats != "no") && (logReqStats != "0") {
99+
icpts = append([]grpc.UnaryServerInterceptor{reqStatsLogger}, icpts...)
100+
}
101+
return icpts
102+
}
103+
104+
// create gRPC server with middleware
105+
server := grpc.NewServer(grpc.ChainUnaryInterceptor(interceptors()...))
89106

90107
grpcMetrics.InitializeMetrics(server)
91108
grpc_health_v1.RegisterHealthServer(server, health.NewServer())

datastore/datastore/storagebackend/postgresql/putobservations.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ type tsInfo struct {
571571
// PutObservations ... (see documentation in StorageBackend interface)
572572
func (sbe *PostgreSQL) PutObservations(request *datastore.PutObsRequest) (codes.Code, string) {
573573

574-
log.Printf("Entered PutObservations with %v observations...", len(request.Observations))
574+
//log.Printf("Entered PutObservations with %v observations...", len(request.Observations))
575575

576576
// reject call if # of observations exceeds limit
577577
if len(request.Observations) > putObsLimit {

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ services:
5858
- HITIME=$HITIME
5959
- CLEANUPINTERVAL=$CLEANUPINTERVAL
6060
- PUTOBSLIMIT=$PUTOBSLIMIT
61+
- LOGREQSTATS=true
6162
restart: on-failure
6263
healthcheck:
6364
test:

0 commit comments

Comments
 (0)