Skip to content
Open
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
1 change: 1 addition & 0 deletions .mise.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ run = "go mod tidy"

[tasks.generate]
description = "Build the grpc protocol"
sources = ['**/*.proto']
run = '''
rm -rf $PB_DIR
mkdir -p $PB_DIR
Expand Down
90 changes: 90 additions & 0 deletions internal/server/mock/metrics/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,105 @@ package metrics

import (
"context"
"fmt"
"hash/fnv"
"maps"
"slices"
"time"

"github.com/skpr/api/pb"
"google.golang.org/protobuf/types/known/timestamppb"
)

// Server implements the GRPC "events" definition.
type Server struct {
pb.UnimplementedMetricsServer
}

func deterministicRange(t time.Time, minVal, maxVal, seconds int64, key string) int64 {
h := fnv.New32a()

bucketKey := fmt.Sprintf("%d-%s", t.Unix()/seconds, key)
_, _ = h.Write([]byte(bucketKey))
hashVal := int64(h.Sum32())

rangeSize := maxVal - minVal + 1
return minVal + (hashVal % rangeSize)
}

func metricMappings(metricType pb.MetricType) map[string][]int64 {
data := map[pb.MetricType]map[string][]int64{
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, 4096},
"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": {100, 250},
"response_times_p95": {2_000, 5_000},
"response_times_p99": {10_000, 20_000},
},
}
return data[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,
}
}

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

// AbsoluteRange gets a metric for a given timestamp range.
func (s *Server) AbsoluteRange(ctx context.Context, req *pb.AbsoluteRangeRequest) (*pb.AbsoluteRangeResponse, error) {
mappings := metricMappings(req.Type)
metricMin, metricMax := mappings[req.Metric][0], mappings[req.Metric][1]

output := []*pb.MetricValue{}
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),
}
output = append(output, &metric)
metricTime = metricTime.Add(time.Minute)
}

return &pb.AbsoluteRangeResponse{
Metrics: output,
}, nil
}

func (s *Server) ClusterRequests(ctx context.Context, req *pb.ClusterRequestsRequest) (*pb.ClusterRequestsResponse, error) {
return &pb.ClusterRequestsResponse{
Metrics: []*pb.MetricClusterRequests{
Expand Down
64 changes: 64 additions & 0 deletions metrics.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ syntax = "proto3";

package workflow;

import "google/protobuf/timestamp.proto";
import "google/protobuf/duration.proto";

option go_package = "./pb";

service metrics {
Expand All @@ -26,6 +29,67 @@ service metrics {
rpc InvalidationPaths (InvalidationPathsRequest) returns (InvalidationPathsResponse) {}
// The amount of resources used by an environment.
rpc ResourceUsage (ResourceUsageRequest) returns (ResourceUsageResponse) {}
// A list of all available metrics.
rpc AvailableMetrics (AvailableMetricsRequest) returns (AvailableMetricsResponse) {}
// Metric values for a given absolute range.
rpc AbsoluteRange (AbsoluteRangeRequest) returns (AbsoluteRangeResponse) {}
}

// Top-level enum definition
enum MetricType {
METRIC_TYPE_UNSPECIFIED = 0;
CLUSTER = 1;
PROJECT = 2;
ENVIRONMENT = 3;
}

/**
* 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.
}

/**
* Output for AvailableMetricsRequest.
*/
message AvailableMetricsResponse {
repeated MetricDefinition metrics = 1; // A list of all available metrics for the type.
}

/**
* Record for an individual metric that's available.
*/
message MetricDefinition {
string name = 1; // The name of the metric.
MetricType type = 2; // The type of the metric.
}

/**
* Input for AbsoluteRangeRequest.
*/
message AbsoluteRangeRequest {
MetricType type = 1; // The type of the metric.
optional string environment = 2; // Name of the environment. Required when using ENVIRONMENT type.
string metric = 3; // The metric that's being requested.
google.protobuf.Timestamp start_time = 4; // The start time for metrics gathering.
google.protobuf.Timestamp end_time = 5; // The end time for metrics gathering.
}

/**
* Output for AbsoluteRangeRequest.
*/
message AbsoluteRangeResponse {
repeated MetricValue metrics = 1; // List of metrics.
}

/**
* Output for a single metric response.
*/
message MetricValue {
google.protobuf.Timestamp timestamp = 1; // The timestamp of the metric..
int64 value = 2; // The value of the metric at the given timestamp.
}

/**
Expand Down
Loading