Skip to content

🎧 Your Personal Streaming Service

feat(insights): report the app store or hosting platform via ND_PLATFORM (#5956)

Deluan Quintãob617a878b90a
diff --git a/core/metrics/insights.go b/core/metrics/insights.go
index 78391779a..66d0b89bd 100644
--- a/core/metrics/insights.go
+++ b/core/metrics/insights.go
@@ -10,6 +10,7 @@ import (
"path/filepath"
"runtime"
"runtime/debug"
+ "strings"
"sync"
"sync/atomic"
"time"
@@ -153,6 +154,17 @@ func getFSInfo(path string) *insights.FSInfo {
return &info
}
+// installedPackage returns the official installer format used, as written by our own packagers.
+func installedPackage() string {
+ data, _ := os.ReadFile(filepath.Join(conf.Server.DataFolder.String(), ".package"))
+ return strings.TrimSpace(string(data))
+}
+
+// hostingPlatform is env-based, not a file, as app stores can only inject env vars into our image.
+func hostingPlatform() string {
+ return strings.TrimSpace(os.Getenv("ND_PLATFORM"))
+}
+
var staticData = sync.OnceValue(func() insights.Data {
// Basic info
data := insights.Data{
@@ -165,11 +177,8 @@ var staticData = sync.OnceValue(func() insights.Data {
data.OS.Containerized = consts.InContainer
// Install info
- packageFilename := filepath.Join(conf.Server.DataFolder.String(), ".package")
- packageFileData, err := os.ReadFile(packageFilename)
- if err == nil {
- data.OS.Package = string(packageFileData)
- }
+ data.OS.Package = installedPackage()
+ data.Platform = hostingPlatform()
// OS info
data.OS.Type = runtime.GOOS
diff --git a/core/metrics/insights/data.go b/core/metrics/insights/data.go
index 126d759bc..8559d4204 100644
--- a/core/metrics/insights/data.go
+++ b/core/metrics/insights/data.go
@@ -4,7 +4,9 @@ type Data struct {
InsightsID string `json:"id"`
Version string `json:"version"`
Uptime int64 `json:"uptime"`
- Build struct {
+ // Platform is the app store or hosting provider this instance runs on, self-declared via ND_PLATFORM
+ Platform string `json:"platform,omitempty"`
+ Build struct {
// build settings used by the Go compiler
Settings map[string]string `json:"settings"`
GoVersion string `json:"goVersion"`
diff --git a/core/metrics/insights_internal_test.go b/core/metrics/insights_internal_test.go
new file mode 100644
index 000000000..74c8ce236
--- /dev/null
+++ b/core/metrics/insights_internal_test.go
@@ -0,0 +1,70 @@
+package metrics
+
+import (
+ "os"
+ "path/filepath"
+
+ "github.com/navidrome/navidrome/conf"
+ "github.com/navidrome/navidrome/conf/configtest"
+ . "github.com/onsi/ginkgo/v2"
+ . "github.com/onsi/gomega"
+)
+
+var _ = Describe("installedPackage", func() {
+ BeforeEach(func() {
+ DeferCleanup(configtest.SetupConfig())
+ conf.Server.DataFolder = conf.NewDir(GinkgoT().TempDir())
+ })
+
+ It("returns empty when there's no .package file", func() {
+ Expect(installedPackage()).To(BeEmpty())
+ })
+
+ It("reads the .package file from the data folder", func() {
+ writePackageFile("deb")
+
+ Expect(installedPackage()).To(Equal("deb"))
+ })
+
+ It("trims surrounding whitespace, as the msi packager writes a trailing newline", func() {
+ writePackageFile("msi\n")
+
+ Expect(installedPackage()).To(Equal("msi"))
+ })
+
+ It("ignores ND_PLATFORM", func() {
+ GinkgoT().Setenv("ND_PLATFORM", "zimaos")
+
+ Expect(installedPackage()).To(BeEmpty())
+ })
+})
+
+var _ = Describe("hostingPlatform", func() {
+ BeforeEach(func() {
+ // Setenv registers the restore, then unset so an inherited value can't leak in
+ GinkgoT().Setenv("ND_PLATFORM", "")
+ Expect(os.Unsetenv("ND_PLATFORM")).To(Succeed())
+ })
+
+ It("returns empty when ND_PLATFORM is not set", func() {
+ Expect(hostingPlatform()).To(BeEmpty())
+ })
+
+ It("reads ND_PLATFORM", func() {
+ GinkgoT().Setenv("ND_PLATFORM", "zimaos")
+
+ Expect(hostingPlatform()).To(Equal("zimaos"))
+ })
+
+ It("trims surrounding whitespace", func() {
+ GinkgoT().Setenv("ND_PLATFORM", " pikapods\n")
+
+ Expect(hostingPlatform()).To(Equal("pikapods"))
+ })
+})
+
+func writePackageFile(content string) {
+ GinkgoHelper()
+ path := filepath.Join(conf.Server.DataFolder.String(), ".package")
+ Expect(os.WriteFile(path, []byte(content), 0600)).To(Succeed())
+}
diff --git a/core/metrics/metrics_suite_test.go b/core/metrics/metrics_suite_test.go
new file mode 100644
index 000000000..bae622e90
--- /dev/null
+++ b/core/metrics/metrics_suite_test.go
@@ -0,0 +1,17 @@
+package metrics
+
+import (
+ "testing"
+
+ "github.com/navidrome/navidrome/log"
+ "github.com/navidrome/navidrome/tests"
+ . "github.com/onsi/ginkgo/v2"
+ . "github.com/onsi/gomega"
+)
+
+func TestMetrics(t *testing.T) {
+ tests.Init(t, false)
+ log.SetLevel(log.LevelFatal)
+ RegisterFailHandler(Fail)
+ RunSpecs(t, "Metrics Suite")
+}