Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/7077 | Related metrics api #7149

Open
wants to merge 33 commits into
base: feat/7080
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
f6ac729
feat(summary view): added metric details apis | 7082
aniketio-ctrl Feb 11, 2025
9385029
feat(summary view): added metric details apis | 7082
aniketio-ctrl Feb 11, 2025
0ca886e
feat(summary view): added list metric details apis | 7084
aniketio-ctrl Feb 11, 2025
bab1399
feat(summary view): added tree map metric details apis | 7087
aniketio-ctrl Feb 11, 2025
9676b7e
feat(summary view): added list metric details apis | 7084
aniketio-ctrl Feb 12, 2025
969d6b0
feat(summary): updated contribution queries for metric metadata| 7082
aniketio-ctrl Feb 13, 2025
2a53b95
Merge branch 'feat/7080' of github.com:SigNoz/signoz into feat/7082
aniketio-ctrl Feb 13, 2025
51794ba
Merge branch 'feat/7082' of github.com:SigNoz/signoz into feat/7084
aniketio-ctrl Feb 13, 2025
d4bdcb1
feat(summary): added which table to use functions| 7084
aniketio-ctrl Feb 13, 2025
03d9c62
feat(summary): added clickhouse settings| 7084
aniketio-ctrl Feb 16, 2025
bba49c1
Merge branch 'feat/7084' of github.com:SigNoz/signoz into feat/7087
aniketio-ctrl Feb 16, 2025
3e43a96
feat(summary): added clickhouse settings| 7087
aniketio-ctrl Feb 16, 2025
7394c06
feat(summary): added clickhouse queries| 7082
aniketio-ctrl Feb 16, 2025
43a3122
feat(explorer): added clickhouse queries| 7077
aniketio-ctrl Feb 16, 2025
8149bb5
feat(explorer): added clickhouse queries| 7077
aniketio-ctrl Feb 17, 2025
d83daa6
feat(summary): removed cardinality from metadata | 7082
aniketio-ctrl Feb 17, 2025
a41d413
Merge branch 'feat/7082' of github.com:SigNoz/signoz into feat/7084
aniketio-ctrl Feb 17, 2025
08b9e9b
feat(summary): updated list metrics api into two parts| 7084
aniketio-ctrl Feb 17, 2025
4590195
feat(summary): added default values for list api| 7084
aniketio-ctrl Feb 17, 2025
e7269bb
Merge branch 'feat/7084' of github.com:SigNoz/signoz into feat/7087
aniketio-ctrl Feb 17, 2025
ea4c7ac
feat(summary): updated tree map samples query into two parts| 7087
aniketio-ctrl Feb 18, 2025
c1f86b1
feat(summary): updated tree map samples query into two parts| 7087
aniketio-ctrl Feb 18, 2025
bc61850
Merge branch 'feat/7087' of github.com:SigNoz/signoz into feat/7077_1
aniketio-ctrl Feb 18, 2025
c5459f3
feat(explorer): updated related metrics query| 7077
aniketio-ctrl Feb 18, 2025
e2ccc5c
feat(explorer): added clickhouse max threads settings| 7077
aniketio-ctrl Feb 18, 2025
4809dc0
Merge branch 'feat/7080' of github.com:SigNoz/signoz into feat/7077_1
aniketio-ctrl Feb 18, 2025
d91e7c1
feat(explorer): added clickhouse max threads settings| 7077
aniketio-ctrl Feb 18, 2025
843d9e1
feat(explorer): added clickhouse max threads settings| 7077
aniketio-ctrl Feb 18, 2025
baebb13
feat(explorer): added clickhouse max threads settings| 7077
aniketio-ctrl Feb 18, 2025
a70cf32
feat(explorer): added query range with related metrics api| 7077
aniketio-ctrl Feb 19, 2025
211b0b9
feat(explorer): added distributed ts table and query builder| 7077
aniketio-ctrl Feb 20, 2025
c2bb82f
Merge branch 'feat/7077_1' of github.com:SigNoz/signoz into feat/7077_1
aniketio-ctrl Feb 20, 2025
e87753a
Merge branch 'feat/7080' into feat/7077_1
aniketio-ctrl Feb 20, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(summary): added which table to use functions| 7084
  • Loading branch information
aniketio-ctrl committed Feb 13, 2025
commit d4bdcb1f2d975c7ec46d901b48c035cdc9e115cf
69 changes: 35 additions & 34 deletions pkg/query-service/app/clickhouseReader/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -5385,11 +5385,6 @@ func (r *ClickHouseReader) GetActiveTimeSeriesForMetricName(ctx context.Context,

func (r *ClickHouseReader) ListSummaryMetrics(ctx context.Context, req *metrics_explorer.SummaryListMetricsRequest) (*metrics_explorer.SummaryListMetricsResponse, *model.ApiError) {
var args []interface{}

// Convert time range to Unix timestamps
startUnix, _ := time.Parse("2006-01-02T15:04:05Z", req.StartDate)
endUnix, _ := time.Parse("2006-01-02T15:04:05Z", req.EndDate)

// Build filters dynamically
conditions, _ := utils.BuildFilterConditions(&req.Filters, "t")

Expand All @@ -5404,41 +5399,47 @@ func (r *ClickHouseReader) ListSummaryMetrics(ctx context.Context, req *metrics_
}

whereClause := strings.Join(conditions, " AND ")
start, end, tsTable := utils.WhichTSTableToUse(req.StartDate, req.EndDate)
sampleTable, countExp := utils.WhichSampleTableToUse(req.StartDate, req.EndDate)

// Final query with simplified structure
query := fmt.Sprintf(`
SELECT
t.metric_name AS metric_name,
argMax(t.description, t.fingerprint) AS description,
argMax(t.type, t.fingerprint) AS type,
t.unit,
COUNT(DISTINCT t.fingerprint) AS cardinality,
COALESCE(MAX(s.data_points), 0) AS dataPoints,
MAX(s.last_received_time) AS lastReceived,
COUNT(DISTINCT t.metric_name) OVER () AS total
FROM %s.%s AS t
LEFT JOIN (
SELECT
metric_name,
COUNT(*) AS data_points,
MAX(unix_milli) AS last_received_time
FROM %s.%s
WHERE unix_milli BETWEEN ? AND ?
GROUP BY metric_name
) AS s USING (metric_name)
WHERE t.unix_milli BETWEEN ? AND ? AND
%s
GROUP BY t.metric_name, t.unit
%s
LIMIT %d OFFSET %d;`,
signozMetricDBName, signozTSTableNameV4,
signozMetricDBName, signozSampleTableName,
whereClause, orderByClause, req.Limit, req.Offset)
t.metric_name AS metric_name,
ANY_VALUE(t.description) AS description,
ANY_VALUE(t.type) AS type,
t.unit,
COUNT(DISTINCT t.fingerprint) AS cardinality,
COALESCE(MAX(s.data_points), 0) AS dataPoints,
MAX(s.last_received_time) AS lastReceived,
COUNT(DISTINCT t.metric_name) OVER () AS total
FROM (
-- First, filter the main table before the join
SELECT metric_name, description, type, unit, fingerprint
FROM %s.%s
WHERE unix_milli BETWEEN ? AND ?
AND %s
) AS t
LEFT JOIN (
-- Also filter the joined table early
SELECT
metric_name,
%s AS data_points,
MAX(unix_milli) AS last_received_time
FROM %s.%s
WHERE unix_milli BETWEEN ? AND ?
GROUP BY metric_name
) AS s USING (metric_name)
GROUP BY t.metric_name, t.unit
%s
LIMIT %d OFFSET %d;`,
signozMetricDBName, tsTable, whereClause,
countExp, signozMetricDBName, sampleTable,
orderByClause, req.Limit, req.Offset)

// Add query parameters
args = append(args,
startUnix.UnixMilli(), endUnix.UnixMilli(), // For samples subquery
startUnix.UnixMilli(), endUnix.UnixMilli(), // For main query
start, end, // For samples subquery
start, end, // For main query
)

rows, err := r.db.Query(ctx, query, args...)
Expand Down
7 changes: 4 additions & 3 deletions pkg/query-service/app/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package app

import (
"bytes"
"github.com/gorilla/mux"
"go.signoz.io/signoz/pkg/query-service/model"
"io"
"net/http"

"github.com/gorilla/mux"
"go.signoz.io/signoz/pkg/query-service/model"

explorer "go.signoz.io/signoz/pkg/query-service/app/metricsexplorer"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -73,7 +74,7 @@ func (aH *APIHandler) ListMetrics(w http.ResponseWriter, r *http.Request) {
return
}

slmr, apiErr := aH.APIHandler.SummaryService.ListMetricsWithSummary(ctx, params)
slmr, apiErr := aH.SummaryService.ListMetricsWithSummary(ctx, params)
if apiErr != nil {
zap.L().Error("error parsing metric query range params", zap.Error(apiErr.Err))
RespondError(w, apiError, nil)
Expand Down
46 changes: 11 additions & 35 deletions pkg/query-service/model/metrics_explorer/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,12 @@ import (
)

type SummaryListMetricsRequest struct {
Offset int `json:"offset"`
Limit int `json:"limit"`
OrderBy []v3.OrderBy `json:"orderBy"`
StartDate int64 `json:"startDate"`
EndDate int64 `json:"endDate"`
Filters SummaryFilterSet `json:"filters"`
}

type SummaryFilterItems struct {
v3.FilterItem
FilterTypeKey FilterTypeKey `json:"filterTypeKey"`
}

type SummaryFilterSet struct {
v3.FilterSet
Items []SummaryFilterItems `json:"items"`
Offset int `json:"offset"`
Limit int `json:"limit"`
OrderBy []v3.OrderBy `json:"orderBy"`
StartDate int64 `json:"startDate"`
EndDate int64 `json:"endDate"`
Filters v3.FilterSet `json:"filters"`
}

type TreeMapType string
Expand All @@ -31,11 +21,11 @@ const (
)

type TreeMapMetricsRequest struct {
Limit int `json:"limit"`
Treemap TreeMapType `json:"treemap"`
StartDate int64 `json:"startDate"`
EndDate int64 `json:"endDate"`
Filters SummaryFilterSet `json:"filters"`
Limit int `json:"limit"`
Treemap TreeMapType `json:"treemap"`
StartDate int64 `json:"startDate"`
EndDate int64 `json:"endDate"`
Filters v3.FilterSet `json:"filters"`
}

type MetricDetail struct {
Expand Down Expand Up @@ -128,20 +118,6 @@ type FilterKeyResponse struct {
AttributeKeys []v3.AttributeKey `json:"attributeKeys"`
}

type FilterTypeKey string

const (
FilterKeyMetricName FilterTypeKey = "metric_name"
FilterKeyType FilterTypeKey = "type"
FilterKeyAttributes FilterTypeKey = "attributes"
FilterKeyUnit FilterTypeKey = "unit"
)

var AvailableColumnFilter = []string{
string(FilterKeyMetricName),
string(FilterKeyUnit),
}

var AvailableColumnFilterMap = map[string]bool{
"metric_name": true,
"unit": true,
Expand Down
61 changes: 53 additions & 8 deletions pkg/query-service/utils/filter_conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package utils
import (
"fmt"
"strings"
"time"

"go.signoz.io/signoz/pkg/query-service/constants"
"go.signoz.io/signoz/pkg/query-service/model/metrics_explorer"
v3 "go.signoz.io/signoz/pkg/query-service/model/v3"
)

// skipKey is an optional parameter to skip processing of a specific key
func BuildFilterConditions(fs *metrics_explorer.SummaryFilterSet, skipKey string) ([]string, error) {
func BuildFilterConditions(fs *v3.FilterSet, skipKey string) ([]string, error) {
if fs == nil || len(fs.Items) == 0 {
return nil, nil
}
Expand All @@ -30,13 +32,10 @@ func BuildFilterConditions(fs *metrics_explorer.SummaryFilterSet, skipKey string

// Determine if the key is a JSON key or a normal column
isJSONKey := false
switch item.FilterTypeKey {
case metrics_explorer.FilterKeyAttributes:
isJSONKey = true // Assuming attributes are stored as JSON
case metrics_explorer.FilterKeyMetricName:
isJSONKey = false // Assuming metric names are normal columns
case metrics_explorer.FilterKeyUnit:
isJSONKey = false // Assuming units are normal columns
if _, exists := metrics_explorer.AvailableColumnFilterMap[item.Key.Key]; exists {
isJSONKey = false
} else {
isJSONKey = true
}

condition, err := buildSingleFilterCondition(item.Key.Key, op, fmtVal, isJSONKey)
Expand Down Expand Up @@ -94,3 +93,49 @@ func buildSingleFilterCondition(key string, op v3.FilterOperator, fmtVal string,
return "", fmt.Errorf("unsupported filter operator: %s", op)
}
}

var (
sixHoursInMilliseconds = time.Hour.Milliseconds() * 6
oneDayInMilliseconds = time.Hour.Milliseconds() * 24
oneWeekInMilliseconds = oneDayInMilliseconds * 7
)

func WhichTSTableToUse(start, end int64) (int64, int64, string) {

var tableName string
if end-start < sixHoursInMilliseconds {
// adjust the start time to nearest 1 hour
start = start - (start % (time.Hour.Milliseconds() * 1))
tableName = constants.SIGNOZ_TIMESERIES_v4_LOCAL_TABLENAME
} else if end-start < oneDayInMilliseconds {
// adjust the start time to nearest 6 hours
start = start - (start % (time.Hour.Milliseconds() * 6))
tableName = constants.SIGNOZ_TIMESERIES_v4_6HRS_LOCAL_TABLENAME
} else if end-start < oneWeekInMilliseconds {
// adjust the start time to nearest 1 day
start = start - (start % (time.Hour.Milliseconds() * 24))
tableName = constants.SIGNOZ_TIMESERIES_v4_1DAY_LOCAL_TABLENAME
} else {
if constants.UseMetricsPreAggregation() {
// adjust the start time to nearest 1 week
start = start - (start % (time.Hour.Milliseconds() * 24 * 7))
tableName = constants.SIGNOZ_TIMESERIES_v4_1WEEK_LOCAL_TABLENAME
} else {
// continue to use the 1 day table
start = start - (start % (time.Hour.Milliseconds() * 24))
tableName = constants.SIGNOZ_TIMESERIES_v4_1DAY_LOCAL_TABLENAME
}
}

return start, end, tableName
}

func WhichSampleTableToUse(start, end int64) (string, string) {
if end-start < oneDayInMilliseconds {
return constants.SIGNOZ_SAMPLES_V4_TABLENAME, "count(*)"
} else if end-start < oneWeekInMilliseconds {
return constants.SIGNOZ_SAMPLES_V4_AGG_5M_TABLENAME, "sum(count)"
} else {
return constants.SIGNOZ_SAMPLES_V4_AGG_30M_TABLENAME, "sum(count)"
}
}
Loading