|
| 1 | +# Observability in ocpp-go |
| 2 | + |
| 3 | +## Metrics |
| 4 | + |
| 5 | +The library currently supports only websocket and ocpp-j server metrics, which are exported via OpenTelemetry. |
| 6 | +To enable metrics, you need to set the metrics exporter on the server: |
| 7 | + |
| 8 | +```go |
| 9 | +// sets up OTLP metrics exporter |
| 10 | +func setupMetrics(address string) error { |
| 11 | +grpcOpts := []grpc.DialOption{ |
| 12 | +grpc.WithTransportCredentials(insecure.NewCredentials()), |
| 13 | +} |
| 14 | + |
| 15 | +client, err := grpc.NewClient(address, grpcOpts...) |
| 16 | + |
| 17 | +if err != nil { |
| 18 | +return errors.Wrap(err, "failed to create gRPC connection to collector") |
| 19 | +} |
| 20 | + |
| 21 | +ctx := context.Background() |
| 22 | + |
| 23 | +exporter, err := otlpmetricgrpc.New(ctx, otlpmetricgrpc.WithGRPCConn(client)) |
| 24 | +if err != nil { |
| 25 | +return errors.Wrap(err, "failed to create otlp metric exporter") |
| 26 | +} |
| 27 | + |
| 28 | +resource, err := resource.New(ctx, |
| 29 | +resource.WithAttributes( |
| 30 | +semconv.ServiceNameKey.String("centralSystem-demo"), |
| 31 | +semconv.ServiceVersionKey.String("example"), |
| 32 | +), |
| 33 | +resource.WithFromEnv(), |
| 34 | +resource.WithContainer(), |
| 35 | +resource.WithOS(), |
| 36 | +resource.WithOSType(), |
| 37 | +resource.WithHost(), |
| 38 | +) |
| 39 | +if err != nil { |
| 40 | +return errors.Wrap(err, "failed to create resource") |
| 41 | +} |
| 42 | + |
| 43 | +meterProvider := metricsdk.NewMeterProvider( |
| 44 | +metricsdk.WithReader( |
| 45 | +metricsdk.NewPeriodicReader( |
| 46 | +exporter, |
| 47 | +metricsdk.WithInterval(10*time.Second), |
| 48 | +), |
| 49 | +), |
| 50 | +metricsdk.WithResource(resource), |
| 51 | +) |
| 52 | + |
| 53 | +otel.SetMeterProvider(meterProvider) |
| 54 | +return nil |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +You can check out the [reference implementation](../example/1.6/cs/central_system_sim.go), and deploy it with: |
| 59 | + |
| 60 | +```bash |
| 61 | +make example-ocpp16-observability |
| 62 | +``` |
| 63 | + |
| 64 | +> Note: Deploying the example requires docker and docker compose to be installed. |
| 65 | +
|
| 66 | +The deployment will start a central system with metrics enabled and a |
| 67 | +full [observability stack](https://github.com/grafana/docker-otel-lgtm). |
| 68 | + |
| 69 | +You can log in to Grafana at http://localhost:3000 with the credentials `admin/admin`. |
0 commit comments