Skip to content

Commit

Permalink
chore: add additional info for host metrics onboarding (#6529)
Browse files Browse the repository at this point in the history
  • Loading branch information
srikanthccv authored Nov 25, 2024
1 parent b85f792 commit 2e4956c
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 3 deletions.
11 changes: 11 additions & 0 deletions pkg/query-service/app/clickhouseReader/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -3384,6 +3384,17 @@ func (r *ClickHouseReader) GetMetricMetadata(ctx context.Context, metricName, se
}, nil
}

// GetCountOfThings returns the count of things in the query
// This is a generic function that can be used to check if any data exists for a given query
func (r *ClickHouseReader) GetCountOfThings(ctx context.Context, query string) (uint64, error) {
var count uint64
err := r.db.QueryRow(ctx, query).Scan(&count)
if err != nil {
return 0, err
}
return count, nil
}

func (r *ClickHouseReader) GetLatestReceivedMetric(
ctx context.Context, metricNames []string,
) (*model.MetricStatus, *model.ApiError) {
Expand Down
49 changes: 49 additions & 0 deletions pkg/query-service/app/inframetrics/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package inframetrics

import (
"context"
"fmt"
"math"
"sort"
"strings"
"time"

"go.signoz.io/signoz/pkg/query-service/app/metrics/v4/helpers"
"go.signoz.io/signoz/pkg/query-service/common"
"go.signoz.io/signoz/pkg/query-service/constants"
"go.signoz.io/signoz/pkg/query-service/interfaces"
"go.signoz.io/signoz/pkg/query-service/model"
v3 "go.signoz.io/signoz/pkg/query-service/model/v3"
Expand Down Expand Up @@ -310,6 +312,45 @@ func (h *HostsRepo) getTopHostGroups(ctx context.Context, req model.HostListRequ
return topHostGroups, allHostGroups, nil
}

func (h *HostsRepo) DidSendHostMetricsData(ctx context.Context, req model.HostListRequest) (bool, error) {

names := []string{}
for _, metricName := range metricNamesForHosts {
names = append(names, metricName)
}

namesStr := "'" + strings.Join(names, "','") + "'"

query := fmt.Sprintf("SELECT count() FROM %s.%s WHERE metric_name IN (%s)",
constants.SIGNOZ_METRIC_DBNAME, constants.SIGNOZ_TIMESERIES_v4_1DAY_TABLENAME, namesStr)

count, err := h.reader.GetCountOfThings(ctx, query)
if err != nil {
return false, err
}

return count > 0, nil
}

func (h *HostsRepo) IsSendingK8SAgentMetrics(ctx context.Context, req model.HostListRequest) (bool, error) {
names := []string{}
for _, metricName := range metricNamesForHosts {
names = append(names, metricName)
}
namesStr := "'" + strings.Join(names, "','") + "'"

query := fmt.Sprintf(`
SELECT count()
FROM %s.%s
WHERE metric_name IN (%s)
AND unix_milli >= toUnixTimestamp(now() - INTERVAL 60 MINUTE) * 1000
AND JSONExtractString(labels, 'host_name') LIKE '%%-otel-agent%%'`,
constants.SIGNOZ_METRIC_DBNAME, constants.SIGNOZ_TIMESERIES_V4_TABLENAME, namesStr)

count, err := h.reader.GetCountOfThings(ctx, query)
return count > 0, err
}

func (h *HostsRepo) GetHostList(ctx context.Context, req model.HostListRequest) (model.HostListResponse, error) {
resp := model.HostListResponse{}

Expand All @@ -330,6 +371,14 @@ func (h *HostsRepo) GetHostList(ctx context.Context, req model.HostListRequest)
resp.Type = model.ResponseTypeGroupedList
}

// don't fail the request if we can't get these values
if sendingK8SAgentMetrics, err := h.IsSendingK8SAgentMetrics(ctx, req); err == nil {
resp.IsSendingK8SAgentMetrics = sendingK8SAgentMetrics
}
if sentAnyHostMetricsData, err := h.DidSendHostMetricsData(ctx, req); err == nil {
resp.SentAnyHostMetricsData = sentAnyHostMetricsData
}

step := int64(math.Max(float64(common.MinAllowedStepInterval(req.Start, req.End)), 60))

query := HostsTableListQuery.Clone()
Expand Down
1 change: 1 addition & 0 deletions pkg/query-service/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ const (
SIGNOZ_SPAN_INDEX_LOCAL_TABLENAME = "signoz_index_v2"
SIGNOZ_SPAN_INDEX_V3_LOCAL_TABLENAME = "signoz_index_v3"
SIGNOZ_TIMESERIES_v4_LOCAL_TABLENAME = "time_series_v4"
SIGNOZ_TIMESERIES_V4_TABLENAME = "distributed_time_series_v4"
SIGNOZ_TIMESERIES_v4_6HRS_LOCAL_TABLENAME = "time_series_v4_6hrs"
SIGNOZ_TIMESERIES_v4_1DAY_LOCAL_TABLENAME = "time_series_v4_1day"
SIGNOZ_TIMESERIES_v4_1WEEK_LOCAL_TABLENAME = "time_series_v4_1week"
Expand Down
2 changes: 2 additions & 0 deletions pkg/query-service/interfaces/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ type Reader interface {
// Query Progress tracking helpers.
ReportQueryStartForProgressTracking(queryId string) (reportQueryFinished func(), err *model.ApiError)
SubscribeToQueryProgress(queryId string) (<-chan model.QueryProgress, func(), *model.ApiError)

GetCountOfThings(ctx context.Context, query string) (uint64, error)
}

type Querier interface {
Expand Down
8 changes: 5 additions & 3 deletions pkg/query-service/model/infra.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ type HostListRecord struct {
}

type HostListResponse struct {
Type ResponseType `json:"type"`
Records []HostListRecord `json:"records"`
Total int `json:"total"`
Type ResponseType `json:"type"`
Records []HostListRecord `json:"records"`
Total int `json:"total"`
SentAnyHostMetricsData bool `json:"sentAnyHostMetricsData"`
IsSendingK8SAgentMetrics bool `json:"isSendingK8SAgentMetrics"`
}

func (r *HostListResponse) SortBy(orderBy *v3.OrderBy) {
Expand Down

0 comments on commit 2e4956c

Please sign in to comment.