Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
102 changes: 59 additions & 43 deletions internal/server/mock/metrics/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import (
"context"
"fmt"
"hash/fnv"
"maps"
"slices"
"strings"
"time"

"github.com/skpr/api/pb"
Expand All @@ -19,6 +18,46 @@ type Server struct {
pb.UnimplementedMetricsServer
}

type MockMetric struct {
Min float64
Max float64
Source pb.MetricSource
}

// metricData is built once at package initialization
var metricData = map[pb.MetricType]map[string]MockMetric{
pb.MetricType_CLUSTER: {
"requests": {250_000, 4_000_000, pb.MetricSource_SYSTEM},
"httpcode_target_200": {500, 1_000, pb.MetricSource_SYSTEM},
"httpcode_target_300": {25, 50, pb.MetricSource_SYSTEM},
"httpcode_target_400": {10, 25, pb.MetricSource_SYSTEM},
"httpcode_target_500": {0, 10, pb.MetricSource_SYSTEM},
},
pb.MetricType_ENVIRONMENT: {
"requests": {25_000, 200_000, pb.MetricSource_SYSTEM},
"cpu": {25, 100, pb.MetricSource_SYSTEM},
"memory": {512_000, 4_294_967_296, pb.MetricSource_SYSTEM}, // 512KB to 4GB
"replicas": {2, 8, pb.MetricSource_SYSTEM},
"php_active": {4, 48, pb.MetricSource_SYSTEM},
"php_idle": {2, 12, pb.MetricSource_SYSTEM},
"php_queued": {0, 8, pb.MetricSource_SYSTEM},
"cache_hit_rate": {60, 99, pb.MetricSource_SYSTEM},
"invalidation_paths": {10, 50, pb.MetricSource_SYSTEM},
"invalidation_requests": {5, 20, pb.MetricSource_SYSTEM},
"origin_errors": {0, 10, pb.MetricSource_SYSTEM},
"httpcode_target_200": {200, 500, pb.MetricSource_SYSTEM},
"httpcode_target_300": {25, 50, pb.MetricSource_SYSTEM},
"httpcode_target_400": {10, 25, pb.MetricSource_SYSTEM},
"httpcode_target_500": {0, 10, pb.MetricSource_SYSTEM},
"response_times_avg": {0.1, 0.25, pb.MetricSource_SYSTEM},
"response_times_p95": {2.0, 5.0, pb.MetricSource_SYSTEM},
"response_times_p99": {10.0, 20.0, pb.MetricSource_SYSTEM},
"mock_application_1": {0, 100, pb.MetricSource_APPLICATION},
"mock_application_2": {0.3, 1.4, pb.MetricSource_APPLICATION},
"mock_application_3": {0, 100_000_000, pb.MetricSource_APPLICATION},
},
}

// deterministicRange returns a specific value consistently for a point in a time series.
func deterministicRange(t time.Time, minVal, maxVal float64, seconds int64, key string) float64 {
h := fnv.New32a()
Expand All @@ -31,54 +70,31 @@ func deterministicRange(t time.Time, minVal, maxVal float64, seconds int64, key
return minVal + (hashVal * rangeSize)
}

func metricMappings(metricType pb.MetricType) map[string][]float64 {
data := map[pb.MetricType]map[string][]float64{
pb.MetricType_CLUSTER: {
"requests": {250_000, 4_000_000},
"httpcode_target_200": {500, 1_000},
"httpcode_target_300": {25, 50},
"httpcode_target_400": {10, 25},
"httpcode_target_500": {0, 10},
},
pb.MetricType_ENVIRONMENT: {
"requests": {25_000, 200_000},
"cpu": {25, 100},
"memory": {512_000, 4_294_967_296}, // 512KB to 4GB
"replicas": {2, 8},
"php_active": {4, 48},
"php_idle": {2, 12},
"php_queued": {0, 8},
"cache_hit_rate": {60, 99},
"invalidation_paths": {10, 50},
"invalidation_requests": {5, 20},
"origin_errors": {0, 10},
"httpcode_target_200": {200, 500},
"httpcode_target_300": {25, 50},
"httpcode_target_400": {10, 25},
"httpcode_target_500": {0, 10},
"response_times_avg": {0.1, 0.25},
"response_times_p95": {2.0, 5.0},
"response_times_p99": {10.0, 20.0},
},
}
return data[metricType]
func metricMappings(metricType pb.MetricType) map[string]MockMetric {
return metricData[metricType]
}

// AvailableMetrics lists all available metrics.
func (s *Server) AvailableMetrics(ctx context.Context, req *pb.AvailableMetricsRequest) (*pb.AvailableMetricsResponse, error) {
mappings := metricMappings(req.Type)
keys := slices.Collect(maps.Keys(mappings))

metrics := make([]*pb.MetricDefinition, len(keys))
for i, key := range keys {
metrics[i] = &pb.MetricDefinition{
Name: key,
Type: req.Type,
availableMetrics := []*pb.MetricDefinition{}
for key, metric := range mappings {
// Hide application metrics for the prod environment.
if req.Environment != nil && *req.Environment == "prod" && metric.Source == pb.MetricSource_APPLICATION {
continue
}

availableMetrics = append(availableMetrics, &pb.MetricDefinition{
Name: key,
Type: req.Type,
Title: strings.ReplaceAll(key, "_", " "),
Source: metric.Source,
})
}

return &pb.AvailableMetricsResponse{
Metrics: metrics,
Metrics: availableMetrics,
}, nil
}

Expand All @@ -88,15 +104,15 @@ func (s *Server) AbsoluteRange(ctx context.Context, req *pb.AbsoluteRangeRequest
if _, ok := mappings[req.Metric]; !ok {
return nil, status.Errorf(codes.NotFound, "metric %s not found", req.Metric)
}
metricMin, metricMax := mappings[req.Metric][0], mappings[req.Metric][1]
metricMin, metricMax := mappings[req.Metric].Min, mappings[req.Metric].Max

output := []*pb.MetricValue{}
seedKey := fmt.Sprintf("%s_%s", req.Type, req.Metric)
metricTime := req.StartTime.AsTime()
for metricTime.Before(req.EndTime.AsTime()) {
cacheKey := fmt.Sprintf("%s_%s", req.Type, req.Metric)
metric := pb.MetricValue{
Timestamp: timestamppb.New(metricTime),
Value: deterministicRange(metricTime, metricMin, metricMax, 60, cacheKey),
Value: new(deterministicRange(metricTime, metricMin, metricMax, 60, seedKey)),
}
output = append(output, &metric)
metricTime = metricTime.Add(time.Minute)
Expand Down
16 changes: 13 additions & 3 deletions metrics.proto
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,28 @@ service metrics {
rpc AbsoluteRange (AbsoluteRangeRequest) returns (AbsoluteRangeResponse) {}
}

// Top-level enum definition
// What the metric scope is.
enum MetricType {
METRIC_TYPE_UNSPECIFIED = 0;
CLUSTER = 1;
PROJECT = 2;
ENVIRONMENT = 3;
}

// Where this metric data is sourced.
enum MetricSource {
METRIC_SOURCE_UNSPECIFIED = 0;
SYSTEM = 1;
APPLICATION = 2;
}

/**
* Input for AvailableMetricsRequest.
*/
message AvailableMetricsRequest {
MetricType type = 1; // The type of the metric.
optional string environment = 2; // Name of the environment. Required when using ENVIRONMENT type.
optional MetricSource source = 3; // Whether to return only metrics with a specific source.
}

/**
Expand All @@ -43,6 +51,8 @@ message AvailableMetricsResponse {
message MetricDefinition {
string name = 1; // The name of the metric.
MetricType type = 2; // The type of the metric.
MetricSource source = 3; // The source of the metric data.
string title = 4; // A human-readable title for the metric.
}

/**
Expand All @@ -67,6 +77,6 @@ message AbsoluteRangeResponse {
* Output for a single metric response.
*/
message MetricValue {
google.protobuf.Timestamp timestamp = 1; // The timestamp of the metric..
double value = 2; // The value of the metric at the given timestamp.
google.protobuf.Timestamp timestamp = 1; // The timestamp of the metric.
optional double value = 2; // The value of the metric at the given timestamp.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optional here is to support 0 being different from undefined. Nothing currently uses it, but I didn't want to do the 3 repo update again to change it later :)

}
Loading