From 8fb8efb16b77bb29cd19c361c830bd0b63a4d4d7 Mon Sep 17 00:00:00 2001 From: Toni Juvani Date: Thu, 24 Apr 2025 23:28:31 +0300 Subject: [PATCH] Tibber: pass more descriptive user agent when connecting (#20841) --- .devcontainer/devcontainer.json | 2 +- meter/tibber-pulse.go | 27 ++++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 642ba38e7..c4d57330b 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -2,7 +2,7 @@ // README at: https://github.com/devcontainers/templates/tree/main/src/go { "name": "evcc", - "image": "mcr.microsoft.com/devcontainers/go:1-1.23-bookworm", + "image": "mcr.microsoft.com/devcontainers/go:1-1.24-bookworm", "features": { "ghcr.io/devcontainers/features/docker-outside-of-docker:1": { "moby": true, diff --git a/meter/tibber-pulse.go b/meter/tibber-pulse.go index 9dedd9c46..d538ade3a 100644 --- a/meter/tibber-pulse.go +++ b/meter/tibber-pulse.go @@ -3,7 +3,9 @@ package meter import ( "context" "encoding/json" + "fmt" "net/http" + "runtime/debug" "strings" "time" @@ -19,6 +21,29 @@ func init() { registry.AddCtx("tibber-pulse", NewTibberFromConfig) } +func getUserAgent() string { + evccVersion := "0.203.2+unknown" + graphqlClientVersion := "0.13.2+unknown" + + if info, ok := debug.ReadBuildInfo(); ok { + evccVersion = baseVersion(info.Main.Version) + for _, dep := range info.Deps { + if dep.Path == "github.com/hasura/go-graphql-client" { + graphqlClientVersion = baseVersion(dep.Version) + } + } + } + + return fmt.Sprintf("evcc/%s hasura/go-graphql-client/%s", evccVersion, graphqlClientVersion) +} + +func baseVersion(v string) string { + if i := strings.IndexAny(v, "-+"); i != -1 { + return v[:i] + } + return v +} + type Tibber struct { data *util.Monitor[tibber.LiveMeasurement] } @@ -78,7 +103,7 @@ func NewTibberFromConfig(ctx context.Context, other map[string]interface{}) (api Transport: &transport.Decorator{ Base: http.DefaultTransport, Decorator: transport.DecorateHeaders(map[string]string{ - "User-Agent": "go-graphql-client/0.9.0", + "User-Agent": getUserAgent(), }), }, },