@@ -26,7 +26,7 @@ Planned milestones and features:
2626
2727## OCPP 1.6 Usage
2828
29- Go version 1.13 + is required.
29+ Go version 1.22 + is required.
3030
3131``` sh
3232go get github.com/lorenzodonini/ocpp-go
@@ -490,15 +490,18 @@ The websocket package supports configuring ping pong for both endpoints.
490490
491491By default, the client sends a ping every 54 seconds and waits for a pong for 60 seconds, before timing out.
492492The values can be configured as follows:
493+
493494``` go
494495cfg := ws.NewClientTimeoutConfig ()
495496cfg.PingPeriod = 10 * time.Second
496497cfg.PongWait = 20 * time.Second
497498websocketClient.SetTimeoutConfig (cfg)
498499```
499500
500- By default, the server does not send out any pings and waits for a ping from the client for 60 seconds, before timing out.
501+ By default, the server does not send out any pings and waits for a ping from the client for 60 seconds, before timing
502+ out.
501503To configure the server to send out pings, the ` PingPeriod ` and ` PongWait ` must be set to a value greater than 0:
504+
502505``` go
503506cfg := ws.NewServerTimeoutConfig ()
504507cfg.PingPeriod = 10 * time.Second
@@ -508,6 +511,74 @@ websocketServer.SetTimeoutConfig(cfg)
508511
509512To disable sending ping messages, set the ` PingPeriod ` value to ` 0 ` .
510513
514+ ### Metrics
515+
516+ The library currently supports only websocket and ocpp-j server metrics, which are exported via OpenTelemetry.
517+ To enable metrics, you simply need to set the metrics exporter on the server:
518+
519+ ``` go
520+ // sets up OTLP metrics exporter
521+ func setupMetrics (address string ) error {
522+ grpcOpts := []grpc.DialOption {
523+ grpc.WithTransportCredentials (insecure.NewCredentials ()),
524+ }
525+
526+ client , err := grpc.NewClient (address, grpcOpts...)
527+
528+ if err != nil {
529+ return errors.Wrap (err, " failed to create gRPC connection to collector" )
530+ }
531+
532+ ctx := context.Background ()
533+
534+ exporter , err := otlpmetricgrpc.New (ctx, otlpmetricgrpc.WithGRPCConn (client))
535+ if err != nil {
536+ return errors.Wrap (err, " failed to create otlp metric exporter" )
537+ }
538+
539+ resource , err := resource.New (ctx,
540+ resource.WithAttributes (
541+ semconv.ServiceNameKey .String (" centralSystem-demo" ),
542+ semconv.ServiceVersionKey .String (" example" ),
543+ ),
544+ resource.WithFromEnv (),
545+ resource.WithContainer (),
546+ resource.WithOS (),
547+ resource.WithOSType (),
548+ resource.WithHost (),
549+ )
550+ if err != nil {
551+ return errors.Wrap (err, " failed to create resource" )
552+ }
553+
554+ meterProvider := metricsdk.NewMeterProvider (
555+ metricsdk.WithReader (
556+ metricsdk.NewPeriodicReader (
557+ exporter,
558+ metricsdk.WithInterval (10 *time.Second ),
559+ ),
560+ ),
561+ metricsdk.WithResource (resource),
562+ )
563+
564+ otel.SetMeterProvider (meterProvider)
565+ return nil
566+ }
567+ ```
568+
569+ You can check out the [ reference implementation] ( example/1.6/cs/central_system_sim.go ) , and deploy it with:
570+
571+ ``` bash
572+ make example-ocpp16-observability
573+ ```
574+
575+ > Note: Deploying the example requires docker and docker compose to be installed.
576+
577+ The deployment will start a central system with metrics enabled and a full observability
578+ stack (https://github.com/grafana/docker-otel-lgtm ).
579+
580+ You can log in to Grafana at http://localhost:3000 with the credentials admin/admin.
581+
511582## OCPP 2.0.1 Usage
512583
513584Experimental support for version 2.0.1 is now supported!
0 commit comments