From ad6e4fed09830d15e88b206440a5804bbbe2ffd9 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Sat, 16 Nov 2024 19:59:37 +0300 Subject: [PATCH] use scheduler Signed-off-by: Vasiliy Tolstov --- cmd/servicechecker/main.go | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/cmd/servicechecker/main.go b/cmd/servicechecker/main.go index 4be58c7..2904b19 100644 --- a/cmd/servicechecker/main.go +++ b/cmd/servicechecker/main.go @@ -114,22 +114,27 @@ func main() { l.Error(ctx, "failed to unmarshal", err) continue } - labels := []string{"check", check.Name, "task", task.Name, "service", svc, "endpoint", mth} - m.Counter(semconv.ClientRequestInflight, labels...).Inc() - ts := time.Now() - l.Info(ctx, fmt.Sprintf("try to call %s.%s via %s", svc, mth, task.Addr)) - err = grpcconn.Call(ctx, l, c, task.Addr, time.Duration(task.Timeout), - c.NewRequest(pkg, svc+"."+mth, req), - rsp) - te := time.Since(ts) - m.Summary(semconv.ClientRequestLatencyMicroseconds, labels...).Update(te.Seconds()) - m.Histogram(semconv.ClientRequestDurationSeconds, labels...).Update(te.Seconds()) - m.Counter(semconv.ClientRequestInflight, labels...).Dec() - if err != nil { - m.Counter(semconv.ClientRequestTotal, append(labels, "status", "failure")...).Inc() - } else { - m.Counter(semconv.ClientRequestTotal, append(labels, "status", "success")...).Inc() - } + + treq := c.NewRequest(pkg, svc+"."+mth, req) + + s.NewJob(time.Duration(check.Interval), func() { + labels := []string{"check", check.Name, "task", task.Name, "service", svc, "endpoint", mth} + m.Counter(semconv.ClientRequestInflight, labels...).Inc() + ts := time.Now() + l.Info(ctx, fmt.Sprintf("try to call %s.%s via %s", svc, mth, task.Addr)) + err = grpcconn.Call(ctx, l, c, task.Addr, time.Duration(task.Timeout), + treq, + rsp) + te := time.Since(ts) + m.Summary(semconv.ClientRequestLatencyMicroseconds, labels...).Update(te.Seconds()) + m.Histogram(semconv.ClientRequestDurationSeconds, labels...).Update(te.Seconds()) + m.Counter(semconv.ClientRequestInflight, labels...).Dec() + if err != nil { + m.Counter(semconv.ClientRequestTotal, append(labels, "status", "failure")...).Inc() + } else { + m.Counter(semconv.ClientRequestTotal, append(labels, "status", "success")...).Inc() + } + }) } }