Files
signoz/pkg/query-service/postprocess/process.go
Pandey 95ed125bd9
Some checks failed
build-staging / prepare (push) Has been cancelled
build-staging / js-build (push) Has been cancelled
build-staging / go-build (push) Has been cancelled
build-staging / staging (push) Has been cancelled
Release Drafter / update_release_draft (push) Has been cancelled
feat(instrumentation): add OTel exception semantic convention log handler (#10665)
* feat(instrumentation): add OTel exception semantic convention log handler

Add a loghandler.Wrapper that enriches error log records with OpenTelemetry
exception semantic convention attributes (exception.type, exception.code,
exception.message, exception.stacktrace).

- Add errors.Attr() helper for standardized error logging under "exception" key
- Add exception log handler that replaces raw error attrs with structured group
- Wire exception handler into the instrumentation SDK logger chain
- Remove LogValue() from errors.base as the handler now owns structuring

* refactor: replace "error", err with errors.Attr(err) across codebase

Migrate all slog error logging from ad-hoc "error", err key-value pairs
to the standardized errors.Attr(err) helper, enabling the exception log
handler to enrich these logs with OTel semantic convention attributes.

* refactor: enforce attr-only slog style across codebase

Change sloglint from kv-only to attr-only, requiring all slog calls to
use typed attributes (slog.String, slog.Any, etc.) instead of key-value
pairs. Convert all existing kv-style slog calls in non-excluded paths.

* refactor: tighten slog.Any to specific types and standardize error attrs

- Replace slog.Any with slog.String for string values (action, key, where_clause)
- Replace slog.Any with slog.Uint64 for uint64 values (start, end, step, etc.)
- Replace slog.Any("err", err) with errors.Attr(err) in dispatcher and segment analytics
- Replace slog.Any("error", ctx.Err()) with errors.Attr in factory registry

* fix(instrumentation): use Unwrapb message for exception.message

Use the explicit error message (m) from Unwrapb instead of
foundErr.Error(), which resolves to the inner cause's message
for wrapped errors.

* feat(errors): capture stacktrace at error creation time

Store program counters ([]uintptr) in base errors at creation time
using runtime.Callers, inspired by thanos-io/thanos/pkg/errors. The
exception log handler reads the stacktrace from the error instead of
capturing at log time, showing where the error originated.

* fix(instrumentation): apply default log wrappers uniformly in NewLogger

Move correlation, filtering, and exception wrappers into NewLogger so
all call sites (including CLI loggers in cmd/) get them automatically.

* refactor(instrumentation): remove variadic wrappers from NewLogger

NewLogger no longer accepts arbitrary wrappers. The core wrappers
(correlation, filtering, exception) are hardcoded, preventing callers
from accidentally duplicating behavior.

* refactor: migrate remaining "error", <var> to errors.Attr across legacy paths

Replace all remaining "error", <variable> key-value pairs with
errors.Attr(<variable>) in pkg/query-service/ and ee/query-service/
paths that were missed in the initial migration due to non-standard
variable names (res.Err, filterErr, apiErrorObj.Err, etc).

* refactor(instrumentation): use flat exception.* keys instead of nested group

Use flat keys (exception.type, exception.code, exception.message,
exception.stacktrace) instead of a nested slog.Group in the exception
log handler.
2026-03-22 04:06:31 +00:00

132 lines
5.3 KiB
Go

package postprocess
import (
"log/slog"
"github.com/SigNoz/govaluate"
"github.com/SigNoz/signoz/pkg/errors"
"github.com/SigNoz/signoz/pkg/query-service/app/queryBuilder"
v3 "github.com/SigNoz/signoz/pkg/query-service/model/v3"
)
// postProcessResult applies having clause, metric limit, reduce function to the result
// This function is effective for metrics data source for now, but it can be extended to other data sources
// if needed
// Much of this work can be done in the ClickHouse query, but we decided to do it here because:
// 1. Effective use of caching
// 2. Easier to add new functions
func PostProcessResult(result []*v3.Result, queryRangeParams *v3.QueryRangeParamsV3) ([]*v3.Result, error) {
// Having clause is not part of the clickhouse query, so we need to apply it here
// It's not included in the query because it doesn't work nicely with caching
// With this change, if you have a query with a having clause, and then you change the having clause
// to something else, the query will still be cached.
ApplyHavingClause(result, queryRangeParams)
// We apply the metric limit here because it's not part of the clickhouse query
// The limit in the context of the time series query is the number of time series
// So for the limit to work, we need to know what series to keep and what to discard
// For us to know that, we need to execute the query first, and then apply the limit
// which we found expensive, because we are executing the query twice on the same data
// So we decided to apply the limit here, after the query is executed
// The function is named applyMetricLimit because it only applies to metrics data source
// In traces and logs, the limit is achieved using subqueries
ApplyMetricLimit(result, queryRangeParams)
// We apply the functions here it's easier to add new functions
ApplyFunctions(result, queryRangeParams)
// Each series in the result produces N number of points, where N is (end - start) / step
// For the panel type table, we need to show one point for each series in the row
// We do that by applying a reduce function to each series
applyReduceTo(result, queryRangeParams)
// expressions are executed at query serivce so the value of time.now in the invdividual
// queries will be different so for table panel we are making it same.
if queryRangeParams.CompositeQuery.PanelType == v3.PanelTypeTable {
tablePanelResultProcessor(result)
}
canDefaultZero := make(map[string]bool)
for _, query := range queryRangeParams.CompositeQuery.BuilderQueries {
canDefaultZero[query.QueryName] = query.CanDefaultZero()
}
for _, query := range queryRangeParams.CompositeQuery.BuilderQueries {
// The way we distinguish between a formula and a query is by checking if the expression
// is the same as the query name
// TODO(srikanthccv): Update the UI to send a flag to distinguish between a formula and a query
if query.Expression != query.QueryName {
expression, err := govaluate.NewEvaluableExpressionWithFunctions(query.Expression, EvalFuncs())
// This shouldn't happen here, because it should have been caught earlier in validation
if err != nil {
slog.Error("error in expression", errors.Attr(err))
return nil, err
}
formulaResult, err := processResults(result, expression, canDefaultZero)
if err != nil {
slog.Error("error in expression", errors.Attr(err))
return nil, err
}
formulaResult.QueryName = query.QueryName
ApplyHavingClause([]*v3.Result{formulaResult}, queryRangeParams)
ApplyMetricLimit([]*v3.Result{formulaResult}, queryRangeParams)
result = append(result, formulaResult)
}
}
// we are done with the formula calculations, only send the results for enabled queries
removeDisabledQueries := func(result []*v3.Result) []*v3.Result {
var newResult []*v3.Result
for _, res := range result {
if queryRangeParams.CompositeQuery.BuilderQueries[res.QueryName].Disabled {
continue
}
newResult = append(newResult, res)
}
return newResult
}
if queryRangeParams.CompositeQuery.QueryType == v3.QueryTypeBuilder {
result = removeDisabledQueries(result)
}
if queryRangeParams.CompositeQuery.FillGaps {
FillGaps(result, queryRangeParams)
}
if queryRangeParams.FormatForWeb &&
queryRangeParams.CompositeQuery.QueryType == v3.QueryTypeBuilder &&
queryRangeParams.CompositeQuery.PanelType == v3.PanelTypeTable {
result = TransformToTableForBuilderQueries(result, queryRangeParams)
}
return result, nil
}
// ApplyFunctions applies functions for each query in the composite query
// The functions can be more than one, and they are applied in the order they are defined
func ApplyFunctions(results []*v3.Result, queryRangeParams *v3.QueryRangeParamsV3) {
for idx, result := range results {
builderQueries := queryRangeParams.CompositeQuery.BuilderQueries
if builderQueries != nil {
functions := builderQueries[result.QueryName].Functions
for _, function := range functions {
results[idx] = queryBuilder.ApplyFunction(function, result)
}
}
}
}
func tablePanelResultProcessor(results []*v3.Result) {
var ts int64
for ridx := range results {
for sidx := range results[ridx].Series {
for pidx := range results[ridx].Series[sidx].Points {
if ts == 0 {
ts = results[ridx].Series[sidx].Points[pidx].Timestamp
} else {
results[ridx].Series[sidx].Points[pidx].Timestamp = ts
}
}
}
}
}