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
chore(JSON): Promote Body Paths API (#9592)
Piyush Singariyabca761498a0e
diff --git a/.gitignore b/.gitignoreindex c002fbe276..de8890e584 100644--- a/.gitignore+++ b/.gitignore@@ -49,6 +49,7 @@ ee/query-service/tests/test-deploy/data/# local data*.backup*.db+**/db/deploy/docker/clickhouse-setup/data//deploy/docker-swarm/clickhouse-setup/data/bin/diff --git a/Makefile b/Makefileindex decec5eadf..14e7b0b57e 100644--- a/Makefile+++ b/Makefile@@ -72,6 +72,12 @@ devenv-up: devenv-clickhouse devenv-signoz-otel-collector ## Start both clickhou@echo " - ClickHouse: http://localhost:8123"@echo " - Signoz OTel Collector: grpc://localhost:4317, http://localhost:4318"+.PHONY: devenv-clickhouse-clean+devenv-clickhouse-clean: ## Clean all ClickHouse data from filesystem+ @echo "Removing ClickHouse data..."+ @rm -rf .devenv/docker/clickhouse/fs/tmp/*+ @echo "ClickHouse data cleaned!"+############################################################### go commands##############################################################diff --git a/docs/api/openapi.yml b/docs/api/openapi.ymlindex 0b1414be94..bd814e513d 100644--- a/docs/api/openapi.yml+++ b/docs/api/openapi.yml@@ -849,6 +849,75 @@ paths:summary: Deprecated create session by email passwordtags:- sessions+ /api/v1/logs/promote_paths:+ get:+ deprecated: false+ description: This endpoints promotes and indexes paths+ operationId: PromotePaths+ responses:+ "200":+ content:+ application/json:+ schema:+ properties:+ data:+ items:+ $ref: '#/components/schemas/PromotetypesPromotePath'+ nullable: true+ type: array+ status:+ type: string+ type: object+ description: OK+ "400":+ content:+ application/json:+ schema:+ $ref: '#/components/schemas/RenderErrorResponse'+ description: Bad Request+ "500":+ content:+ application/json:+ schema:+ $ref: '#/components/schemas/RenderErrorResponse'+ description: Internal Server Error+ summary: Promote and index paths+ tags:+ - promoted_paths+ - logs+ - json_logs+ post:+ deprecated: false+ description: This endpoints promotes and indexes paths+ operationId: PromotePaths+ requestBody:+ content:+ application/json:+ schema:+ items:+ $ref: '#/components/schemas/PromotetypesPromotePath'+ nullable: true+ type: array+ responses:+ "201":+ description: Created+ "400":+ content:+ application/json:+ schema:+ $ref: '#/components/schemas/RenderErrorResponse'+ description: Bad Request+ "500":+ content:+ application/json:+ schema:+ $ref: '#/components/schemas/RenderErrorResponse'+ description: Internal Server Error+ summary: Promote and index paths+ tags:+ - promoted_paths+ - logs+ - json_logs/api/v1/org/preferences:get:deprecated: false@@ -2137,6 +2206,26 @@ components:type: objectPreferencetypesValue:type: object+ PromotetypesPromotePath:+ properties:+ indexes:+ items:+ $ref: '#/components/schemas/PromotetypesWrappedIndex'+ type: array+ path:+ type: string+ promote:+ type: boolean+ type: object+ PromotetypesWrappedIndex:+ properties:+ column_type:+ type: string+ granularity:+ type: integer+ type:+ type: string+ type: objectRenderErrorResponse:properties:error:diff --git a/pkg/apiserver/signozapiserver/promote.go b/pkg/apiserver/signozapiserver/promote.gonew file mode 100644index 0000000000..8a51cd54af--- /dev/null+++ b/pkg/apiserver/signozapiserver/promote.go@@ -0,0 +1,43 @@+package signozapiserver++import (+ "net/http"++ "github.com/SigNoz/signoz/pkg/http/handler"+ "github.com/SigNoz/signoz/pkg/types/promotetypes"+ "github.com/gorilla/mux"+)++func (provider *provider) addPromoteRoutes(router *mux.Router) error {+ if err := router.Handle("/api/v1/logs/promote_paths", handler.New(provider.authZ.EditAccess(provider.promoteHandler.HandlePromoteAndIndexPaths), handler.OpenAPIDef{+ ID: "PromotePaths",+ Tags: []string{"promoted_paths", "logs", "json_logs"},+ Summary: "Promote and index paths",+ Description: "This endpoints promotes and indexes paths",+ Request: new([]*promotetypes.PromotePath),+ RequestContentType: "application/json",+ Response: nil,+ ResponseContentType: "",+ SuccessStatusCode: http.StatusCreated,+ ErrorStatusCodes: []int{http.StatusBadRequest},+ })).Methods(http.MethodPost).GetError(); err != nil {+ return err+ }++ if err := router.Handle("/api/v1/logs/promote_paths", handler.New(provider.authZ.ViewAccess(provider.promoteHandler.ListPromotedAndIndexedPaths), handler.OpenAPIDef{+ ID: "PromotePaths",+ Tags: []string{"promoted_paths", "logs", "json_logs"},+ Summary: "Promote and index paths",+ Description: "This endpoints promotes and indexes paths",+ Request: nil,+ RequestContentType: "",+ Response: new([]*promotetypes.PromotePath),+ ResponseContentType: "",+ SuccessStatusCode: http.StatusOK,+ ErrorStatusCodes: []int{http.StatusBadRequest},+ })).Methods(http.MethodGet).GetError(); err != nil {+ return err+ }++ return nil+}diff --git a/pkg/apiserver/signozapiserver/provider.go b/pkg/apiserver/signozapiserver/provider.goindex b2cfbb6a1f..71f9b04862 100644--- a/pkg/apiserver/signozapiserver/provider.go+++ b/pkg/apiserver/signozapiserver/provider.go@@ -12,6 +12,7 @@ import ("github.com/SigNoz/signoz/pkg/modules/authdomain""github.com/SigNoz/signoz/pkg/modules/organization""github.com/SigNoz/signoz/pkg/modules/preference"+ "github.com/SigNoz/signoz/pkg/modules/promote""github.com/SigNoz/signoz/pkg/modules/session""github.com/SigNoz/signoz/pkg/modules/user""github.com/SigNoz/signoz/pkg/types"@@ -30,6 +31,7 @@ type provider struct {authDomainHandler authdomain.HandlerpreferenceHandler preference.HandlerglobalHandler global.Handler+ promoteHandler promote.Handler}func NewFactory(@@ -41,9 +43,10 @@ func NewFactory(authDomainHandler authdomain.Handler,preferenceHandler preference.Handler,globalHandler global.Handler,+ promoteHandler promote.Handler,) factory.ProviderFactory[apiserver.APIServer, apiserver.Config] {return factory.NewProviderFactory(factory.MustNewName("signoz"), func(ctx context.Context, providerSettings factory.ProviderSettings, config apiserver.Config) (apiserver.APIServer, error) {- return newProvider(ctx, providerSettings, config, orgGetter, authz, orgHandler, userHandler, sessionHandler, authDomainHandler, preferenceHandler, globalHandler)+ return newProvider(ctx, providerSettings, config, orgGetter, authz, orgHandler, userHandler, sessionHandler, authDomainHandler, preferenceHandler, globalHandler, promoteHandler)})}@@ -59,6 +62,7 @@ func newProvider(authDomainHandler authdomain.Handler,preferenceHandler preference.Handler,globalHandler global.Handler,+ promoteHandler promote.Handler,) (apiserver.APIServer, error) {settings := factory.NewScopedProviderSettings(providerSettings, "github.com/SigNoz/signoz/pkg/apiserver/signozapiserver")router := mux.NewRouter().UseEncodedPath()@@ -73,6 +77,7 @@ func newProvider(authDomainHandler: authDomainHandler,preferenceHandler: preferenceHandler,globalHandler: globalHandler,+ promoteHandler: promoteHandler,}provider.authZ = middleware.NewAuthZ(settings.Logger(), orgGetter, authz)@@ -113,6 +118,10 @@ func (provider *provider) AddToRouter(router *mux.Router) error {return err}+ if err := provider.addPromoteRoutes(router); err != nil {+ return err+ }+return nil}diff --git a/pkg/errors/errors.go b/pkg/errors/errors.goindex 1c05d11087..4b35e34e28 100644--- a/pkg/errors/errors.go+++ b/pkg/errors/errors.go@@ -209,6 +209,11 @@ func NewUnexpectedf(code Code, format string, args ...any) *base {return Newf(TypeInvalidInput, code, format, args...)}+// NewMethodNotAllowedf is a wrapper around Newf with TypeMethodNotAllowed.+func NewMethodNotAllowedf(code Code, format string, args ...any) *base {+ return Newf(TypeMethodNotAllowed, code, format, args...)+}+// WrapTimeoutf is a wrapper around Wrapf with TypeTimeout.func WrapTimeoutf(cause error, code Code, format string, args ...any) *base {return Wrapf(cause, TypeTimeout, code, format, args...)diff --git a/pkg/modules/promote/implpromote/handler.go b/pkg/modules/promote/implpromote/handler.gonew file mode 100644index 0000000000..19efd78780--- /dev/null+++ b/pkg/modules/promote/implpromote/handler.go@@ -0,0 +1,60 @@+package implpromote++import (+ "net/http"++ "github.com/SigNoz/signoz/pkg/errors"+ "github.com/SigNoz/signoz/pkg/http/binding"+ "github.com/SigNoz/signoz/pkg/http/render"+ "github.com/SigNoz/signoz/pkg/modules/promote"+ "github.com/SigNoz/signoz/pkg/types/authtypes"+ "github.com/SigNoz/signoz/pkg/types/promotetypes"+)++type handler struct {+ module promote.Module+}++func NewHandler(module promote.Module) promote.Handler {+ return &handler{module: module}+}++func (h *handler) HandlePromoteAndIndexPaths(w http.ResponseWriter, r *http.Request) {+ // TODO(Nitya): Use in multi tenant setup+ _, err := authtypes.ClaimsFromContext(r.Context())+ if err != nil {+ render.Error(w, errors.NewInternalf(errors.CodeInternal, "failed to get org id from context"))+ return+ }++ var req []*promotetypes.PromotePath+ if err := binding.JSON.BindBody(r.Body, &req); err != nil {+ render.Error(w, err)+ return+ }++ err = h.module.PromoteAndIndexPaths(r.Context(), req...)+ if err != nil {+ render.Error(w, err)+ return+ }++ render.Success(w, http.StatusCreated, nil)+}++func (h *handler) ListPromotedAndIndexedPaths(w http.ResponseWriter, r *http.Request) {+ // TODO(Nitya): Use in multi tenant setup+ _, err := authtypes.ClaimsFromContext(r.Context())+ if err != nil {+ render.Error(w, errors.NewInternalf(errors.CodeInternal, "failed to get org id from context"))+ return+ }++ paths, err := h.module.ListPromotedAndIndexedPaths(r.Context())+ if err != nil {+ render.Error(w, err)+ return+ }++ render.Success(w, http.StatusOK, paths)+}diff --git a/pkg/modules/promote/implpromote/module.go b/pkg/modules/promote/implpromote/module.gonew file mode 100644index 0000000000..1590d42455--- /dev/null+++ b/pkg/modules/promote/implpromote/module.go@@ -0,0 +1,201 @@+package implpromote++import (+ "context"+ "maps"+ "slices"+ "strings"++ schemamigrator "github.com/SigNoz/signoz-otel-collector/cmd/signozschemamigrator/schema_migrator"+ "github.com/SigNoz/signoz/pkg/errors"+ "github.com/SigNoz/signoz/pkg/modules/promote"+ "github.com/SigNoz/signoz/pkg/telemetrylogs"+ "github.com/SigNoz/signoz/pkg/telemetrystore"+ "github.com/SigNoz/signoz/pkg/types/promotetypes"+ "github.com/SigNoz/signoz/pkg/types/telemetrytypes"+)++var (+ CodeFailedToCreateIndex = errors.MustNewCode("failed_to_create_index_promoted_paths")+ CodeFailedToQueryPromotedPaths = errors.MustNewCode("failed_to_query_promoted_paths")+)++type module struct {+ metadataStore telemetrytypes.MetadataStore+ telemetryStore telemetrystore.TelemetryStore+}++func NewModule(metadataStore telemetrytypes.MetadataStore, telemetrystore telemetrystore.TelemetryStore) promote.Module {+ return &module{metadataStore: metadataStore, telemetryStore: telemetrystore}+}++func (m *module) ListPromotedAndIndexedPaths(ctx context.Context) ([]promotetypes.PromotePath, error) {+ logsIndexes, err := m.metadataStore.ListLogsJSONIndexes(ctx)+ if err != nil {+ return nil, err+ }+ // Flatten the map values (which are slices) into a single slice+ indexes := slices.Concat(slices.Collect(maps.Values(logsIndexes))...)++ aggr := map[string][]promotetypes.WrappedIndex{}+ for _, index := range indexes {+ path, columnType, err := schemamigrator.UnfoldJSONSubColumnIndexExpr(index.Expression)+ if err != nil {+ return nil, err+ }++ // clean backticks from the path+ path = strings.ReplaceAll(path, "`", "")++ aggr[path] = append(aggr[path], promotetypes.WrappedIndex{+ ColumnType: columnType,+ Type: index.Type,+ Granularity: index.Granularity,+ })+ }+ promotedPaths, err := m.listPromotedPaths(ctx)+ if err != nil {+ return nil, err+ }++ response := []promotetypes.PromotePath{}+ for _, path := range promotedPaths {+ fullPath := telemetrylogs.BodyPromotedColumnPrefix + path+ path = telemetrylogs.BodyJSONStringSearchPrefix + path+ item := promotetypes.PromotePath{+ Path: path,+ Promote: true,+ }+ indexes, ok := aggr[fullPath]+ if ok {+ item.Indexes = indexes+ delete(aggr, fullPath)+ }+ response = append(response, item)+ }++ // add the paths that are not promoted but have indexes+ for path, indexes := range aggr {+ path := strings.TrimPrefix(path, telemetrylogs.BodyJSONColumnPrefix)+ path = telemetrylogs.BodyJSONStringSearchPrefix + path+ response = append(response, promotetypes.PromotePath{+ Path: path,+ Indexes: indexes,+ })+ }+ return response, nil+}++func (m *module) listPromotedPaths(ctx context.Context) ([]string, error) {+ paths, err := m.metadataStore.ListPromotedPaths(ctx)+ if err != nil {+ return nil, err+ }+ return slices.Collect(maps.Keys(paths)), nil+}++// PromotePaths inserts provided JSON paths into the promoted paths table for logs queries.+func (m *module) PromotePaths(ctx context.Context, paths []string) error {+ if len(paths) == 0 {+ return errors.NewInvalidInputf(errors.CodeInvalidInput, "paths cannot be empty")+ }++ return m.metadataStore.PromotePaths(ctx, paths...)+}++// createIndexes creates string ngram + token filter indexes on JSON path subcolumns for LIKE queries.+func (m *module) createIndexes(ctx context.Context, indexes []schemamigrator.Index) error {+ if len(indexes) == 0 {+ return nil+ }++ for _, index := range indexes {+ alterStmt := schemamigrator.AlterTableAddIndex{+ Database: telemetrylogs.DBName,+ Table: telemetrylogs.LogsV2LocalTableName,+ Index: index,+ }+ op := alterStmt.OnCluster(m.telemetryStore.Cluster())+ if err := m.telemetryStore.ClickhouseDB().Exec(ctx, op.ToSQL()); err != nil {+ return errors.WrapInternalf(err, CodeFailedToCreateIndex, "failed to create index")+ }+ }++ return nil+}++// PromoteAndIndexPaths handles promoting paths and creating indexes in one call.+func (m *module) PromoteAndIndexPaths(+ ctx context.Context,+ paths ...*promotetypes.PromotePath,+) error {+ if len(paths) == 0 {+ return errors.NewInvalidInputf(errors.CodeInvalidInput, "paths cannot be empty")+ }++ pathsStr := []string{}+ // validate the paths+ for _, path := range paths {+ if err := path.ValidateAndSetDefaults(); err != nil {+ return err+ }+ pathsStr = append(pathsStr, path.Path)+ }++ existingPromotedPaths, err := m.metadataStore.ListPromotedPaths(ctx, pathsStr...)+ if err != nil {+ return err+ }++ var toInsert []string+ indexes := []schemamigrator.Index{}+ for _, it := range paths {+ if it.Promote {+ if _, promoted := existingPromotedPaths[it.Path]; !promoted {+ toInsert = append(toInsert, it.Path)+ }+ }+ if len(it.Indexes) > 0 {+ parentColumn := telemetrylogs.LogsV2BodyJSONColumn+ // if the path is already promoted or is being promoted, add it to the promoted column+ if _, promoted := existingPromotedPaths[it.Path]; promoted || it.Promote {+ parentColumn = telemetrylogs.LogsV2BodyPromotedColumn+ }++ for _, index := range it.Indexes {+ var typeIndex schemamigrator.IndexType+ switch {+ case strings.HasPrefix(index.Type, string(schemamigrator.IndexTypeNGramBF)):+ typeIndex = schemamigrator.IndexTypeNGramBF+ case strings.HasPrefix(index.Type, string(schemamigrator.IndexTypeTokenBF)):+ typeIndex = schemamigrator.IndexTypeTokenBF+ case strings.HasPrefix(index.Type, string(schemamigrator.IndexTypeMinMax)):+ typeIndex = schemamigrator.IndexTypeMinMax+ default:+ return errors.NewInvalidInputf(errors.CodeInvalidInput, "invalid index type: %s", index.Type)+ }+ indexes = append(indexes, schemamigrator.Index{+ Name: schemamigrator.JSONSubColumnIndexName(parentColumn, it.Path, index.JSONDataType.StringValue(), typeIndex),+ Expression: schemamigrator.JSONSubColumnIndexExpr(parentColumn, it.Path, index.JSONDataType.StringValue()),+ Type: index.Type,+ Granularity: index.Granularity,+ })+ }+ }+ }++ if len(toInsert) > 0 {+ err := m.PromotePaths(ctx, toInsert)+ if err != nil {+ return err+ }+ }++ if len(indexes) > 0 {+ if err := m.createIndexes(ctx, indexes); err != nil {+ return err+ }+ }++ return nil+}diff --git a/pkg/modules/promote/promote.go b/pkg/modules/promote/promote.gonew file mode 100644index 0000000000..3146121714--- /dev/null+++ b/pkg/modules/promote/promote.go@@ -0,0 +1,18 @@+package promote++import (+ "context"+ "net/http"++ "github.com/SigNoz/signoz/pkg/types/promotetypes"+)++type Module interface {+ ListPromotedAndIndexedPaths(ctx context.Context) ([]promotetypes.PromotePath, error)+ PromoteAndIndexPaths(ctx context.Context, paths ...*promotetypes.PromotePath) error+}++type Handler interface {+ HandlePromoteAndIndexPaths(w http.ResponseWriter, r *http.Request)+ ListPromotedAndIndexedPaths(w http.ResponseWriter, r *http.Request)+}diff --git a/pkg/query-service/app/clickhouseReader/reader.go b/pkg/query-service/app/clickhouseReader/reader.goindex d122f239f0..0e64566df6 100644--- a/pkg/query-service/app/clickhouseReader/reader.go+++ b/pkg/query-service/app/clickhouseReader/reader.go@@ -43,6 +43,7 @@ import ("github.com/SigNoz/signoz/pkg/query-service/app/traces/tracedetail""github.com/SigNoz/signoz/pkg/query-service/common""github.com/SigNoz/signoz/pkg/query-service/constants"+chErrors "github.com/SigNoz/signoz/pkg/query-service/errors""github.com/SigNoz/signoz/pkg/query-service/metrics""github.com/SigNoz/signoz/pkg/query-service/model"diff --git a/pkg/query-service/app/http_handler.go b/pkg/query-service/app/http_handler.goindex d8b487f813..2d88980aea 100644--- a/pkg/query-service/app/http_handler.go+++ b/pkg/query-service/app/http_handler.go@@ -555,6 +555,7 @@ func (aH *APIHandler) RegisterRoutes(router *mux.Router, am *middleware.AuthZ) {router.HandleFunc("/api/v1/settings/ttl", am.ViewAccess(aH.getTTL)).Methods(http.MethodGet)router.HandleFunc("/api/v2/settings/ttl", am.AdminAccess(aH.setCustomRetentionTTL)).Methods(http.MethodPost)router.HandleFunc("/api/v2/settings/ttl", am.ViewAccess(aH.getCustomRetentionTTL)).Methods(http.MethodGet)+router.HandleFunc("/api/v1/settings/apdex", am.AdminAccess(aH.Signoz.Handlers.Apdex.Set)).Methods(http.MethodPost)router.HandleFunc("/api/v1/settings/apdex", am.ViewAccess(aH.Signoz.Handlers.Apdex.Get)).Methods(http.MethodGet)diff --git a/pkg/query-service/app/logs/v4/query_builder_test.go b/pkg/query-service/app/logs/v4/query_builder_test.goindex 37ebe5a976..1d42088dca 100644--- a/pkg/query-service/app/logs/v4/query_builder_test.go+++ b/pkg/query-service/app/logs/v4/query_builder_test.go@@ -5,6 +5,7 @@ import ("github.com/SigNoz/signoz/pkg/query-service/constants"v3 "github.com/SigNoz/signoz/pkg/query-service/model/v3"+ "github.com/stretchr/testify/assert")func Test_getClickhouseKey(t *testing.T) {@@ -1210,9 +1211,8 @@ func TestPrepareLogsQuery(t *testing.T) {t.Errorf("PrepareLogsQuery() error = %v, wantErr %v", err, tt.wantErr)return}- if got != tt.want {- t.Errorf("PrepareLogsQuery() = %v, want %v", got, tt.want)- }++ assert.Equal(t, tt.want, got)})}}diff --git a/pkg/signoz/module.go b/pkg/signoz/module.goindex 678f561751..32c67120c1 100644--- a/pkg/signoz/module.go+++ b/pkg/signoz/module.go@@ -20,6 +20,8 @@ import ("github.com/SigNoz/signoz/pkg/modules/organization/implorganization""github.com/SigNoz/signoz/pkg/modules/preference""github.com/SigNoz/signoz/pkg/modules/preference/implpreference"+ "github.com/SigNoz/signoz/pkg/modules/promote"+ "github.com/SigNoz/signoz/pkg/modules/promote/implpromote""github.com/SigNoz/signoz/pkg/modules/quickfilter""github.com/SigNoz/signoz/pkg/modules/quickfilter/implquickfilter""github.com/SigNoz/signoz/pkg/modules/rawdataexport"@@ -65,6 +67,7 @@ type Modules struct {Services services.ModuleSpanPercentile spanpercentile.ModuleMetricsExplorer metricsexplorer.Module+ Promote promote.Module}func NewModules(@@ -108,5 +111,6 @@ func NewModules(SpanPercentile: implspanpercentile.NewModule(querier, providerSettings),Services: implservices.NewModule(querier, telemetryStore),MetricsExplorer: implmetricsexplorer.NewModule(telemetryStore, telemetryMetadataStore, cache, ruleStore, dashboard, providerSettings, config.MetricsExplorer),+ Promote: implpromote.NewModule(telemetryMetadataStore, telemetryStore),}}diff --git a/pkg/signoz/openapi.go b/pkg/signoz/openapi.goindex 0bdd23d172..a76342aea1 100644--- a/pkg/signoz/openapi.go+++ b/pkg/signoz/openapi.go@@ -14,6 +14,7 @@ import ("github.com/SigNoz/signoz/pkg/modules/authdomain""github.com/SigNoz/signoz/pkg/modules/organization""github.com/SigNoz/signoz/pkg/modules/preference"+ "github.com/SigNoz/signoz/pkg/modules/promote""github.com/SigNoz/signoz/pkg/modules/session""github.com/SigNoz/signoz/pkg/modules/user""github.com/SigNoz/signoz/pkg/types/ctxtypes"@@ -38,6 +39,7 @@ func NewOpenAPI(ctx context.Context, instrumentation instrumentation.Instrumentastruct{ authdomain.Handler }{},struct{ preference.Handler }{},struct{ global.Handler }{},+ struct{ promote.Handler }{},).New(ctx, instrumentation.ToProviderSettings(), apiserver.Config{})if err != nil {return nil, errdiff --git a/pkg/signoz/provider.go b/pkg/signoz/provider.goindex 9e30ce3850..3a757f2ab4 100644--- a/pkg/signoz/provider.go+++ b/pkg/signoz/provider.go@@ -24,6 +24,7 @@ import ("github.com/SigNoz/signoz/pkg/modules/organization""github.com/SigNoz/signoz/pkg/modules/organization/implorganization""github.com/SigNoz/signoz/pkg/modules/preference/implpreference"+ "github.com/SigNoz/signoz/pkg/modules/promote/implpromote""github.com/SigNoz/signoz/pkg/modules/session/implsession""github.com/SigNoz/signoz/pkg/modules/user""github.com/SigNoz/signoz/pkg/modules/user/impluser"@@ -234,6 +235,7 @@ func NewAPIServerProviderFactories(orgGetter organization.Getter, authz authz.Auimplauthdomain.NewHandler(modules.AuthDomain),implpreference.NewHandler(modules.Preference),signozglobal.NewHandler(global),+ implpromote.NewHandler(modules.Promote),),)}diff --git a/pkg/telemetrymetadata/body_json_metadata.go b/pkg/telemetrymetadata/body_json_metadata.goindex a3fde204ff..ee2d7a2ee1 100644--- a/pkg/telemetrymetadata/body_json_metadata.go+++ b/pkg/telemetrymetadata/body_json_metadata.go@@ -7,7 +7,6 @@ import ("strings""time"- "github.com/ClickHouse/clickhouse-go/v2""github.com/ClickHouse/clickhouse-go/v2/lib/chcol"schemamigrator "github.com/SigNoz/signoz-otel-collector/cmd/signozschemamigrator/schema_migrator""github.com/SigNoz/signoz-otel-collector/constants"@@ -15,7 +14,6 @@ import ("github.com/SigNoz/signoz/pkg/errors""github.com/SigNoz/signoz/pkg/querybuilder""github.com/SigNoz/signoz/pkg/telemetrylogs"- "github.com/SigNoz/signoz/pkg/telemetrystore""github.com/SigNoz/signoz/pkg/types/telemetrytypes""github.com/huandu/go-sqlbuilder")@@ -34,6 +32,10 @@ var (CodeFailScanVariant = errors.MustNewCode("fail_scan_variant")CodeFailBuildJSONPathsQuery = errors.MustNewCode("fail_build_json_paths_query")CodeNoPathsToQueryIndexes = errors.MustNewCode("no_paths_to_query_indexes_provided")++ CodeFailedToPrepareBatch = errors.MustNewCode("failed_to_prepare_batch_promoted_paths")+ CodeFailedToSendBatch = errors.MustNewCode("failed_to_send_batch_promoted_paths")+ CodeFailedToAppendPath = errors.MustNewCode("failed_to_append_path_promoted_paths"))// GetBodyJSONPaths extracts body JSON paths from the path_types table@@ -48,7 +50,7 @@ var (// TODO(Piyush): Remove this lint skip//// nolint:unused-func getBodyJSONPaths(ctx context.Context, telemetryStore telemetrystore.TelemetryStore,+func (t *telemetryMetaStore) getBodyJSONPaths(ctx context.Context,fieldKeySelectors []*telemetrytypes.FieldKeySelector) ([]*telemetrytypes.TelemetryFieldKey, bool, error) {query, args, limit, err := buildGetBodyJSONPathsQuery(fieldKeySelectors)@@ -56,7 +58,7 @@ func getBodyJSONPaths(ctx context.Context, telemetryStore telemetrystore.Telemetreturn nil, false, err}- rows, err := telemetryStore.ClickhouseDB().Query(ctx, query, args...)+ rows, err := t.telemetrystore.ClickhouseDB().Query(ctx, query, args...)if err != nil {return nil, false, errors.WrapInternalf(err, CodeFailExtractBodyJSONKeys, "failed to extract body JSON keys")}@@ -96,12 +98,12 @@ func getBodyJSONPaths(ctx context.Context, telemetryStore telemetrystore.Telemetreturn nil, false, errors.WrapInternalf(rows.Err(), CodeFailIterateBodyJSONKeys, "error iterating body JSON keys")}- promoted, err := GetPromotedPaths(ctx, telemetryStore.ClickhouseDB(), paths...)+ promoted, err := t.GetPromotedPaths(ctx, paths...)if err != nil {return nil, false, err}- indexes, err := getJSONPathIndexes(ctx, telemetryStore, paths...)+ indexes, err := t.getJSONPathIndexes(ctx, paths...)if err != nil {return nil, false, err}@@ -163,7 +165,7 @@ func buildGetBodyJSONPathsQuery(fieldKeySelectors []*telemetrytypes.FieldKeySele// TODO(Piyush): Remove this lint skip//// nolint:unused-func getJSONPathIndexes(ctx context.Context, telemetryStore telemetrystore.TelemetryStore, paths ...string) (map[string][]telemetrytypes.JSONDataTypeIndex, error) {+func (t *telemetryMetaStore) getJSONPathIndexes(ctx context.Context, paths ...string) (map[string][]telemetrytypes.JSONDataTypeIndex, error) {filteredPaths := []string{}for _, path := range paths {if strings.Contains(path, telemetrylogs.ArraySep) || strings.Contains(path, telemetrylogs.ArrayAnyIndex) {@@ -176,7 +178,7 @@ func getJSONPathIndexes(ctx context.Context, telemetryStore telemetrystore.Telem}// list indexes for the paths- indexesMap, err := ListLogsJSONIndexes(ctx, telemetryStore, filteredPaths...)+ indexesMap, err := t.ListLogsJSONIndexes(ctx, filteredPaths...)if err != nil {return nil, errors.WrapInternalf(err, CodeFailLoadLogsJSONIndexes, "failed to list JSON path indexes")}@@ -215,7 +217,6 @@ func getJSONPathIndexes(ctx context.Context, telemetryStore telemetrystore.Telem}func buildListLogsJSONIndexesQuery(cluster string, filters ...string) (string, []any) {- // This aggregates all types per path and gets the max last_seen, then applies LIMITsb := sqlbuilder.Select("name", "type_full", "expr", "granularity",).From(fmt.Sprintf("clusterAllReplicas('%s', %s)", cluster, SkipIndexTableName))@@ -236,9 +237,9 @@ func buildListLogsJSONIndexesQuery(cluster string, filters ...string) (string, [return sb.BuildWithFlavor(sqlbuilder.ClickHouse)}-func ListLogsJSONIndexes(ctx context.Context, telemetryStore telemetrystore.TelemetryStore, filters ...string) (map[string][]schemamigrator.Index, error) {- query, args := buildListLogsJSONIndexesQuery(telemetryStore.Cluster(), filters...)- rows, err := telemetryStore.ClickhouseDB().Query(ctx, query, args...)+func (t *telemetryMetaStore) ListLogsJSONIndexes(ctx context.Context, filters ...string) (map[string][]schemamigrator.Index, error) {+ query, args := buildListLogsJSONIndexesQuery(t.telemetrystore.Cluster(), filters...)+ rows, err := t.telemetrystore.ClickhouseDB().Query(ctx, query, args...)if err != nil {return nil, errors.WrapInternalf(err, CodeFailLoadLogsJSONIndexes, "failed to load string indexed columns")}@@ -264,9 +265,16 @@ func ListLogsJSONIndexes(ctx context.Context, telemetryStore telemetrystore.Telereturn indexesMap, nil}-func ListPromotedPaths(ctx context.Context, conn clickhouse.Conn) (map[string]struct{}, error) {- query := fmt.Sprintf("SELECT path FROM %s.%s", DBName, PromotedPathsTableName)- rows, err := conn.Query(ctx, query)+func (t *telemetryMetaStore) ListPromotedPaths(ctx context.Context, paths ...string) (map[string]struct{}, error) {+ sb := sqlbuilder.Select("path").From(fmt.Sprintf("%s.%s", DBName, PromotedPathsTableName))+ pathConditions := []string{}+ for _, path := range paths {+ pathConditions = append(pathConditions, sb.Equal("path", path))+ }+ sb.Where(sb.Or(pathConditions...))+ query, args := sb.BuildWithFlavor(sqlbuilder.ClickHouse)++ rows, err := t.telemetrystore.ClickhouseDB().Query(ctx, query, args...)if err != nil {return nil, errors.WrapInternalf(err, CodeFailLoadPromotedPaths, "failed to load promoted paths")}@@ -285,14 +293,14 @@ func ListPromotedPaths(ctx context.Context, conn clickhouse.Conn) (map[string]st}// TODO(Piyush): Remove this if not used in future-func ListJSONValues(ctx context.Context, conn clickhouse.Conn, path string, limit int) (*telemetrytypes.TelemetryFieldValues, bool, error) {+func (t *telemetryMetaStore) ListJSONValues(ctx context.Context, path string, limit int) (*telemetrytypes.TelemetryFieldValues, bool, error) {path = CleanPathPrefixes(path)if strings.Contains(path, telemetrylogs.ArraySep) || strings.Contains(path, telemetrylogs.ArrayAnyIndex) {return nil, false, errors.NewInvalidInputf(errors.CodeInvalidInput, "array paths are not supported")}- promoted, err := IsPathPromoted(ctx, conn, path)+ promoted, err := t.IsPathPromoted(ctx, path)if err != nil {return nil, false, err}@@ -325,7 +333,7 @@ func ListJSONValues(ctx context.Context, conn clickhouse.Conn, path string, limicontextWithTimeout, cancel := context.WithTimeout(ctx, 5*time.Second)defer cancel()query, args := sb.BuildWithFlavor(sqlbuilder.ClickHouse)- rows, err := conn.Query(contextWithTimeout, query, args...)+ rows, err := t.telemetrystore.ClickhouseDB().Query(contextWithTimeout, query, args...)if err != nil {if errors.Is(err, context.DeadlineExceeded) {return nil, false, errors.WrapTimeoutf(err, errors.CodeTimeout, "query timed out").WithAdditional("failed to list JSON values")@@ -447,10 +455,10 @@ func derefValue(v any) any {}// IsPathPromoted checks if a specific path is promoted-func IsPathPromoted(ctx context.Context, conn clickhouse.Conn, path string) (bool, error) {+func (t *telemetryMetaStore) IsPathPromoted(ctx context.Context, path string) (bool, error) {split := strings.Split(path, telemetrylogs.ArraySep)query := fmt.Sprintf("SELECT 1 FROM %s.%s WHERE path = ? LIMIT 1", DBName, PromotedPathsTableName)- rows, err := conn.Query(ctx, query, split[0])+ rows, err := t.telemetrystore.ClickhouseDB().Query(ctx, query, split[0])if err != nil {return false, errors.WrapInternalf(err, CodeFailCheckPathPromoted, "failed to check if path %s is promoted", path)}@@ -460,7 +468,7 @@ func IsPathPromoted(ctx context.Context, conn clickhouse.Conn, path string) (boo}// GetPromotedPaths checks if a specific path is promoted-func GetPromotedPaths(ctx context.Context, conn clickhouse.Conn, paths ...string) (*utils.ConcurrentSet[string], error) {+func (t *telemetryMetaStore) GetPromotedPaths(ctx context.Context, paths ...string) (*utils.ConcurrentSet[string], error) {sb := sqlbuilder.Select("path").From(fmt.Sprintf("%s.%s", DBName, PromotedPathsTableName))pathConditions := []string{}for _, path := range paths {@@ -469,7 +477,7 @@ func GetPromotedPaths(ctx context.Context, conn clickhouse.Conn, paths ...stringsb.Where(sb.Or(pathConditions...))query, args := sb.BuildWithFlavor(sqlbuilder.ClickHouse)- rows, err := conn.Query(ctx, query, args...)+ rows, err := t.telemetrystore.ClickhouseDB().Query(ctx, query, args...)if err != nil {return nil, errors.WrapInternalf(err, CodeFailCheckPathPromoted, "failed to get promoted paths")}@@ -494,3 +502,29 @@ func CleanPathPrefixes(path string) string {path = strings.TrimPrefix(path, telemetrylogs.BodyPromotedColumnPrefix)return path}++func (t *telemetryMetaStore) PromotePaths(ctx context.Context, paths ...string) error {+ batch, err := t.telemetrystore.ClickhouseDB().PrepareBatch(ctx,+ fmt.Sprintf("INSERT INTO %s.%s (path, created_at) VALUES", DBName,+ PromotedPathsTableName))+ if err != nil {+ return errors.WrapInternalf(err, CodeFailedToPrepareBatch, "failed to prepare batch")+ }++ nowMs := uint64(time.Now().UnixMilli())+ for _, p := range paths {+ trimmed := strings.TrimSpace(p)+ if trimmed == "" {+ continue+ }+ if err := batch.Append(trimmed, nowMs); err != nil {+ _ = batch.Abort()+ return errors.WrapInternalf(err, CodeFailedToAppendPath, "failed to append path")+ }+ }++ if err := batch.Send(); err != nil {+ return errors.WrapInternalf(err, CodeFailedToSendBatch, "failed to send batch")+ }+ return nil+}diff --git a/pkg/types/promotetypes/types.go b/pkg/types/promotetypes/types.gonew file mode 100644index 0000000000..0a0251e547--- /dev/null+++ b/pkg/types/promotetypes/types.go@@ -0,0 +1,76 @@+package promotetypes++import (+ "strings"++ "github.com/SigNoz/signoz-otel-collector/constants"+ "github.com/SigNoz/signoz-otel-collector/pkg/keycheck"+ "github.com/SigNoz/signoz/pkg/errors"+ "github.com/SigNoz/signoz/pkg/telemetrylogs"+ "github.com/SigNoz/signoz/pkg/types/telemetrytypes"+)++type WrappedIndex struct {+ JSONDataType telemetrytypes.JSONDataType `json:"-"`+ ColumnType string `json:"column_type"`+ Type string `json:"type"`+ Granularity int `json:"granularity"`+}++type PromotePath struct {+ Path string `json:"path"`+ Promote bool `json:"promote,omitempty"`++ Indexes []WrappedIndex `json:"indexes,omitempty"`+}++func (i *PromotePath) ValidateAndSetDefaults() error {+ if i.Path == "" {+ return errors.Newf(errors.TypeInvalidInput, errors.CodeInvalidInput, "path is required")+ }++ if strings.Contains(i.Path, " ") {+ return errors.Newf(errors.TypeInvalidInput, errors.CodeInvalidInput, "path cannot contain spaces")+ }++ if strings.Contains(i.Path, telemetrylogs.ArraySep) || strings.Contains(i.Path, telemetrylogs.ArrayAnyIndex) {+ return errors.Newf(errors.TypeInvalidInput, errors.CodeInvalidInput, "array paths can not be promoted or indexed")+ }++ if strings.HasPrefix(i.Path, constants.BodyJSONColumnPrefix) || strings.HasPrefix(i.Path, constants.BodyPromotedColumnPrefix) {+ return errors.Newf(errors.TypeInvalidInput, errors.CodeInvalidInput, "`%s`, `%s` don't add these prefixes to the path", constants.BodyJSONColumnPrefix, constants.BodyPromotedColumnPrefix)+ }++ if !strings.HasPrefix(i.Path, telemetrylogs.BodyJSONStringSearchPrefix) {+ return errors.Newf(errors.TypeInvalidInput, errors.CodeInvalidInput, "path must start with `body.`")+ }++ // remove the "body." prefix from the path+ i.Path = strings.TrimPrefix(i.Path, telemetrylogs.BodyJSONStringSearchPrefix)++ isCardinal := keycheck.IsCardinal(i.Path)+ if isCardinal {+ return errors.Newf(errors.TypeInvalidInput, errors.CodeInvalidInput, "cardinal paths can not be promoted or indexed")+ }++ for idx, index := range i.Indexes {+ if index.Type == "" {+ return errors.Newf(errors.TypeInvalidInput, errors.CodeInvalidInput, "index type is required")+ }+ if index.Granularity <= 0 {+ return errors.Newf(errors.TypeInvalidInput, errors.CodeInvalidInput, "index granularity must be greater than 0")+ }++ jsonDataType, ok := telemetrytypes.MappingStringToJSONDataType[index.ColumnType]+ if !ok {+ return errors.Newf(errors.TypeInvalidInput, errors.CodeInvalidInput, "invalid column type: %s", index.ColumnType)+ }+ if !jsonDataType.IndexSupported {+ return errors.Newf(errors.TypeInvalidInput, errors.CodeInvalidInput, "index is not supported for column type: %s", index.ColumnType)+ }++ i.Indexes[idx].JSONDataType = jsonDataType+ }++ return nil+}diff --git a/pkg/types/telemetrytypes/store.go b/pkg/types/telemetrytypes/store.goindex ae890b1020..9e5a3d69f3 100644--- a/pkg/types/telemetrytypes/store.go+++ b/pkg/types/telemetrytypes/store.go@@ -3,6 +3,7 @@ package telemetrytypesimport ("context"+ schemamigrator "github.com/SigNoz/signoz-otel-collector/cmd/signozschemamigrator/schema_migrator""github.com/SigNoz/signoz/pkg/types/metrictypes")@@ -30,4 +31,13 @@ type MetadataStore interface {// FetchTemporalityMulti fetches the temporality for multiple metricsFetchTemporalityMulti(ctx context.Context, metricNames ...string) (map[string]metrictypes.Temporality, error)++ // ListLogsJSONIndexes lists the JSON indexes for the logs table.+ ListLogsJSONIndexes(ctx context.Context, filters ...string) (map[string][]schemamigrator.Index, error)++ // ListPromotedPaths lists the promoted paths.+ ListPromotedPaths(ctx context.Context, paths ...string) (map[string]struct{}, error)++ // PromotePaths promotes the paths.+ PromotePaths(ctx context.Context, paths ...string) error}diff --git a/pkg/types/telemetrytypes/telemetrytypestest/metadata_store.go b/pkg/types/telemetrytypes/telemetrytypestest/metadata_store.goindex eada498bd4..e21a060255 100644--- a/pkg/types/telemetrytypes/telemetrytypestest/metadata_store.go+++ b/pkg/types/telemetrytypes/telemetrytypestest/metadata_store.go@@ -4,6 +4,7 @@ import ("context""strings"+ schemamigrator "github.com/SigNoz/signoz-otel-collector/cmd/signozschemamigrator/schema_migrator""github.com/SigNoz/signoz/pkg/types/metrictypes""github.com/SigNoz/signoz/pkg/types/telemetrytypes")@@ -11,19 +12,23 @@ import (// MockMetadataStore implements the MetadataStore interface for testing purposestype MockMetadataStore struct {// Maps to store test data- KeysMap map[string][]*telemetrytypes.TelemetryFieldKey- RelatedValuesMap map[string][]string- AllValuesMap map[string]*telemetrytypes.TelemetryFieldValues- TemporalityMap map[string]metrictypes.Temporality+ KeysMap map[string][]*telemetrytypes.TelemetryFieldKey+ RelatedValuesMap map[string][]string+ AllValuesMap map[string]*telemetrytypes.TelemetryFieldValues+ TemporalityMap map[string]metrictypes.Temporality+ PromotedPathsMap map[string]struct{}+ LogsJSONIndexesMap map[string][]schemamigrator.Index}// NewMockMetadataStore creates a new instance of MockMetadataStore with initialized mapsfunc NewMockMetadataStore() *MockMetadataStore {return &MockMetadataStore{- KeysMap: make(map[string][]*telemetrytypes.TelemetryFieldKey),- RelatedValuesMap: make(map[string][]string),- AllValuesMap: make(map[string]*telemetrytypes.TelemetryFieldValues),- TemporalityMap: make(map[string]metrictypes.Temporality),+ KeysMap: make(map[string][]*telemetrytypes.TelemetryFieldKey),+ RelatedValuesMap: make(map[string][]string),+ AllValuesMap: make(map[string]*telemetrytypes.TelemetryFieldValues),+ TemporalityMap: make(map[string]metrictypes.Temporality),+ PromotedPathsMap: make(map[string]struct{}),+ LogsJSONIndexesMap: make(map[string][]schemamigrator.Index),}}@@ -284,3 +289,21 @@ func (m *MockMetadataStore) FetchTemporalityMulti(ctx context.Context, metricNamfunc (m *MockMetadataStore) SetTemporality(metricName string, temporality metrictypes.Temporality) {m.TemporalityMap[metricName] = temporality}++// PromotePaths promotes the paths.+func (m *MockMetadataStore) PromotePaths(ctx context.Context, paths ...string) error {+ for _, path := range paths {+ m.PromotedPathsMap[path] = struct{}{}+ }+ return nil+}++// ListPromotedPaths lists the promoted paths.+func (m *MockMetadataStore) ListPromotedPaths(ctx context.Context, paths ...string) (map[string]struct{}, error) {+ return m.PromotedPathsMap, nil+}++// ListLogsJSONIndexes lists the JSON indexes for the logs table.+func (m *MockMetadataStore) ListLogsJSONIndexes(ctx context.Context, filters ...string) (map[string][]schemamigrator.Index, error) {+ return m.LogsJSONIndexesMap, nil+}