SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.
TypeScript apmapplication-monitoringdistributed-tracinggogood-first-issuejaegerloglogsmetricsmonitoringnextjsobservabilityopen-sourceopentelemetryprometheusreactreactjsself-hostedtracingtypescript
feat(web): add support web settings (#11444)
Vikrant Gupta3ffb5bd43be6
diff --git a/conf/example.yaml b/conf/example.yamlindex f4b3a52ec9..1ed81eb8be 100644--- a/conf/example.yaml+++ b/conf/example.yaml@@ -60,6 +60,14 @@ web:index: index.html# The directory containing the static build files.directory: /etc/signoz/web+ # Settings exposed to the web.+ settings:+ posthog:+ # Whether to enable PostHog in web.+ enabled: true+ appcues:+ # Whether to enable Appcues in web.+ enabled: true##################### Cache #####################cache:diff --git a/pkg/web/config.go b/pkg/web/config.goindex c5ab0147d3..54cdd9531f 100644--- a/pkg/web/config.go+++ b/pkg/web/config.go@@ -14,6 +14,24 @@ type Config struct {// The directory from which to serve the web files.Directory string `mapstructure:"directory"`++ // Settings that are exposed to the web.+ Settings Settings `mapstructure:"settings"`+}++// Settings that are exposed to the web.+type Settings struct {+ Posthog Posthog `mapstructure:"posthog"`++ Appcues Appcues `mapstructure:"appcues"`+}++type Posthog struct {+ Enabled bool `mapstructure:"enabled"`+}++type Appcues struct {+ Enabled bool `mapstructure:"enabled"`}func NewConfigFactory() factory.ConfigFactory {@@ -25,6 +43,14 @@ func newConfig() factory.Config {Enabled: true,Index: "index.html",Directory: "/etc/signoz/web",+ Settings: Settings{+ Posthog: Posthog{+ Enabled: true,+ },+ Appcues: Appcues{+ Enabled: true,+ },+ },}}diff --git a/pkg/web/config_test.go b/pkg/web/config_test.goindex 6bc18be995..f4997cbbc7 100644--- a/pkg/web/config_test.go+++ b/pkg/web/config_test.go@@ -38,6 +38,7 @@ func TestNewWithEnvProvider(t *testing.T) {Enabled: false,Index: def.Index,Directory: def.Directory,+ Settings: def.Settings,}assert.Equal(t, expected, actual)diff --git a/pkg/web/routerweb/provider.go b/pkg/web/routerweb/provider.goindex b8ec9e6a89..85cafd7baf 100644--- a/pkg/web/routerweb/provider.go+++ b/pkg/web/routerweb/provider.go@@ -2,6 +2,8 @@ package routerwebimport ("context"+ "encoding/json"+ "html/template""net/http""os""path/filepath"@@ -42,8 +44,16 @@ func New(ctx context.Context, settings factory.ProviderSettings, config web.Confreturn nil, errors.WrapInvalidInputf(err, errors.CodeInvalidInput, "cannot read %q in web directory", config.Index)}+ settingsJSON, err := json.Marshal(config.Settings)+ if err != nil {+ return nil, errors.WrapInternalf(err, errors.CodeInternal, "cannot marshal web settings to JSON")+ }+logger := factory.NewScopedProviderSettings(settings, "github.com/SigNoz/signoz/pkg/web/routerweb").Logger()- indexContents := web.NewIndex(ctx, logger, config.Index, raw, web.TemplateData{BaseHref: globalConfig.ExternalPathTrailing()})+ indexContents := web.NewIndex(ctx, logger, config.Index, raw, web.TemplateData{+ BaseHref: globalConfig.ExternalPathTrailing(),+ Settings: template.JS(settingsJSON),+ })return &provider{config: config,diff --git a/pkg/web/routerweb/provider_test.go b/pkg/web/routerweb/provider_test.goindex f6305b04c6..461adf6348 100644--- a/pkg/web/routerweb/provider_test.go+++ b/pkg/web/routerweb/provider_test.go@@ -2,6 +2,7 @@ package routerwebimport ("context"+ "encoding/json""io""net""net/http"@@ -19,6 +20,11 @@ import ("github.com/stretchr/testify/require")+func expectedHTML(baseHref string, settings web.Settings) string {+ settingsJSON, _ := json.Marshal(settings)+ return `<html><head><base href="` + baseHref + `" /></head><body><script>window.signozBootData={settings:` + string(settingsJSON) + `}</script>Welcome to test data!!!</body></html>`+}+func startServer(t *testing.T, config web.Config, globalConfig global.Config) string {t.Helper()@@ -54,53 +60,79 @@ func httpGet(t *testing.T, url string) string {func TestServeTemplatedIndex(t *testing.T) {t.Parallel()+ emptySettings := web.Settings{}+testCases := []struct {name stringpath stringglobalConfig global.Config+ webConfig web.Configexpected string}{{name: "RootBaseHrefAtRoot",path: "/",globalConfig: global.Config{},- expected: `<html><head><base href="/" /></head><body>Welcome to test data!!!</body></html>`,+ webConfig: web.Config{Index: "valid_template.html", Directory: "testdata"},+ expected: expectedHTML("/", emptySettings),},{name: "RootBaseHrefAtNonExistentPath",path: "/does-not-exist",globalConfig: global.Config{},- expected: `<html><head><base href="/" /></head><body>Welcome to test data!!!</body></html>`,+ webConfig: web.Config{Index: "valid_template.html", Directory: "testdata"},+ expected: expectedHTML("/", emptySettings),},{name: "RootBaseHrefAtDirectory",path: "/assets",globalConfig: global.Config{},- expected: `<html><head><base href="/" /></head><body>Welcome to test data!!!</body></html>`,+ webConfig: web.Config{Index: "valid_template.html", Directory: "testdata"},+ expected: expectedHTML("/", emptySettings),},{name: "SubPathBaseHrefAtRoot",path: "/",globalConfig: global.Config{ExternalURL: &url.URL{Scheme: "https", Host: "example.com", Path: "/signoz"}},- expected: `<html><head><base href="/signoz/" /></head><body>Welcome to test data!!!</body></html>`,+ webConfig: web.Config{Index: "valid_template.html", Directory: "testdata"},+ expected: expectedHTML("/signoz/", emptySettings),},{name: "SubPathBaseHrefAtNonExistentPath",path: "/does-not-exist",globalConfig: global.Config{ExternalURL: &url.URL{Scheme: "https", Host: "example.com", Path: "/signoz"}},- expected: `<html><head><base href="/signoz/" /></head><body>Welcome to test data!!!</body></html>`,+ webConfig: web.Config{Index: "valid_template.html", Directory: "testdata"},+ expected: expectedHTML("/signoz/", emptySettings),},{name: "SubPathBaseHrefAtDirectory",path: "/assets",globalConfig: global.Config{ExternalURL: &url.URL{Scheme: "https", Host: "example.com", Path: "/signoz"}},- expected: `<html><head><base href="/signoz/" /></head><body>Welcome to test data!!!</body></html>`,+ webConfig: web.Config{Index: "valid_template.html", Directory: "testdata"},+ expected: expectedHTML("/signoz/", emptySettings),+ },+ {+ name: "WithPopulatedSettings",+ path: "/",+ globalConfig: global.Config{},+ webConfig: web.Config{+ Index: "valid_template.html",+ Directory: "testdata",+ Settings: web.Settings{+ Posthog: web.Posthog{Enabled: true},+ Appcues: web.Appcues{Enabled: true},+ },+ },+ expected: expectedHTML("/", web.Settings{+ Posthog: web.Posthog{Enabled: true},+ Appcues: web.Appcues{Enabled: true},+ }),},}for _, testCase := range testCases {t.Run(testCase.name, func(t *testing.T) {- base := startServer(t, web.Config{Index: "valid_template.html", Directory: "testdata"}, testCase.globalConfig)+ base := startServer(t, testCase.webConfig, testCase.globalConfig)assert.Equal(t, testCase.expected, strings.TrimSuffix(httpGet(t, base+testCase.path), "\n"))})diff --git a/pkg/web/routerweb/testdata/valid_template.html b/pkg/web/routerweb/testdata/valid_template.htmlindex f48c8d9a11..991be76c9b 100644--- a/pkg/web/routerweb/testdata/valid_template.html+++ b/pkg/web/routerweb/testdata/valid_template.html@@ -1 +1 @@-<html><head><base href="[[.BaseHref]]" /></head><body>Welcome to test data!!!</body></html>+<html><head><base href="[[.BaseHref]]" /></head><body><script>window.signozBootData={settings:[[.Settings]]}</script>Welcome to test data!!!</body></html>diff --git a/pkg/web/template.go b/pkg/web/template.goindex 84c1585bf4..26dda6b6d6 100644--- a/pkg/web/template.go+++ b/pkg/web/template.go@@ -11,8 +11,14 @@ import (// Field names map to the HTML attributes they populate in the template:// - BaseHref → <base href="[[.BaseHref]]" />+// - Settings → window.signozBootData = { settings: [[.Settings]] }type TemplateData struct {BaseHref string++ // Settings is the pre-serialized JSON of web.Settings for injection into a+ // <script> block. The template.JS type prevents html/template from+ // HTML-escaping the value.+ Settings template.JS}// If the template cannot be parsed or executed, the raw bytes are