Skip to content

Commit

Permalink
fix: recorder should be reactive (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
clement2026 authored Sep 6, 2024
1 parent c685d6e commit fdb47ad
Show file tree
Hide file tree
Showing 16 changed files with 300 additions and 231 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
**/.idea
**/.vscode
**/.DS_Store

node_modules
go.work
Expand Down
144 changes: 78 additions & 66 deletions api/api.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified assets/web/html.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion internal/general/general_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (m *GeneralServer) DetectURL(req *api.DetectURLRequest, stream api.General_
urls := make([]string, 0, 5)
urls = append(urls, req.Url)
for _, mt := range mts {
url, err := metrics.MetricsURL(req.Url, mt, true)
url, err := metrics.MetricsURL(false, mt, req.Url, 1)
if err != nil {
logging.Sugar.Error(err)
panic(err)
Expand Down
2 changes: 1 addition & 1 deletion internal/metrics/metrics_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (m *MetricsServer) GoroutineMetrics(ctx context.Context, req *api.GoMetrics
}

func dispatch(ctx context.Context, req *api.GoMetricsRequest, mt MetricsType) (*api.GoMetricsResponse, error) {
u, err := MetricsURL(req.Url, mt, false)
u, err := MetricsURL(false, mt, req.Url, req.ProfileSeconds)
if err != nil {
logging.Sugar.Error(err)
return nil, err
Expand Down
13 changes: 9 additions & 4 deletions internal/metrics/metrics_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package metrics
import (
"errors"
"net/url"
"strconv"
)

type MetricsType string
Expand All @@ -18,10 +19,10 @@ const (
//
// If debug is true, it fetches data in text format(except for MetricsTypeCPU).
// If debug is false, it fetches data in pb.gz format.
func MetricsURL(baseUrl string, mt MetricsType, debug bool) (string, error) {
parse, err := url.Parse(baseUrl)
func MetricsURL(debug bool, mt MetricsType, urlStr string, profileSeconds uint64) (string, error) {
parse, err := url.Parse(urlStr)
if err != nil {
return "", errors.New("invalid url: " + baseUrl)
return "", errors.New("invalid url: " + urlStr)
}

var p *url.URL
Expand All @@ -30,7 +31,11 @@ func MetricsURL(baseUrl string, mt MetricsType, debug bool) (string, error) {
p = parse.JoinPath(string(mt))

if mt == MetricsTypeCPU {
params.Add("seconds", "1")
var sec = uint64(1)
if profileSeconds > 1 {
sec = profileSeconds
}
params.Add("seconds", strconv.Itoa(int(sec)))
} else if debug {
params.Add("debug", "1")
}
Expand Down
2 changes: 1 addition & 1 deletion internal/metrics/mock_metrics_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (m *MockMetricsServer) GoroutineMetrics(_ context.Context, req *api.GoMetri
}

func (m *MockMetricsServer) dispatch(req *api.GoMetricsRequest, mt MetricsType) (*api.GoMetricsResponse, error) {
_, err := MetricsURL(req.Url, mt, false)
_, err := MetricsURL(false, mt, req.Url, req.ProfileSeconds)
if err != nil {
return nil, err
}
Expand Down
2 changes: 2 additions & 0 deletions proto/api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ service General {

message GoMetricsRequest {
string url = 1;
// cpu profile only
uint64 profile_seconds = 2;
}

message GoMetricsResponse {
Expand Down
Loading

0 comments on commit fdb47ad

Please sign in to comment.