@@ -117,60 +117,61 @@ func main() {
|
||||
l.Info(ctx, fmt.Sprintf("check %#+v", check))
|
||||
for _, task := range check.Tasks {
|
||||
l.Info(ctx, fmt.Sprintf("task %#+v", task))
|
||||
c, ok := clients[task.Type]
|
||||
if !ok {
|
||||
l.Error(ctx, fmt.Sprintf("unknown client %s", task.Type))
|
||||
continue
|
||||
}
|
||||
|
||||
pkg, svc, mth := grpcconn.ServiceMethod(task.Endpoint)
|
||||
pdesc, err := pfiles.FindDescriptorByName(protoreflect.FullName(pkg + "." + svc))
|
||||
if err != nil {
|
||||
l.Error(ctx, "failed to find service "+pkg+"."+svc)
|
||||
continue
|
||||
}
|
||||
|
||||
sdesc, ok := pdesc.(protoreflect.ServiceDescriptor)
|
||||
if !ok {
|
||||
l.Error(ctx, "failed to find service "+pkg+"."+svc)
|
||||
continue
|
||||
}
|
||||
|
||||
mdesc := sdesc.Methods().ByName(protoreflect.Name(mth))
|
||||
if mdesc == nil {
|
||||
l.Error(ctx, "failed to find method "+mth)
|
||||
continue
|
||||
}
|
||||
|
||||
req := dynamicpb.NewMessageType(mdesc.Input()).New()
|
||||
rsp := dynamicpb.NewMessageType(mdesc.Output()).New()
|
||||
|
||||
if err = jsonpbcodec.NewCodec().Unmarshal([]byte(task.Data), req); err != nil {
|
||||
l.Error(ctx, "failed to unmarshal", err)
|
||||
continue
|
||||
}
|
||||
|
||||
treq := c.NewRequest(pkg, svc+"."+mth, req)
|
||||
|
||||
sch.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()
|
||||
if task.GRPC != nil {
|
||||
c, ok := clients["grpc"]
|
||||
if !ok {
|
||||
l.Error(ctx, fmt.Sprintf("unknown client %s", "grpc"))
|
||||
continue
|
||||
}
|
||||
})
|
||||
|
||||
pkg, svc, mth := grpcconn.ServiceMethod(task.GRPC.Endpoint)
|
||||
pdesc, err := pfiles.FindDescriptorByName(protoreflect.FullName(pkg + "." + svc))
|
||||
if err != nil {
|
||||
l.Error(ctx, "failed to find service "+pkg+"."+svc)
|
||||
continue
|
||||
}
|
||||
|
||||
sdesc, ok := pdesc.(protoreflect.ServiceDescriptor)
|
||||
if !ok {
|
||||
l.Error(ctx, "failed to find service "+pkg+"."+svc)
|
||||
continue
|
||||
}
|
||||
|
||||
mdesc := sdesc.Methods().ByName(protoreflect.Name(mth))
|
||||
if mdesc == nil {
|
||||
l.Error(ctx, "failed to find method "+mth)
|
||||
continue
|
||||
}
|
||||
|
||||
req := dynamicpb.NewMessageType(mdesc.Input()).New()
|
||||
rsp := dynamicpb.NewMessageType(mdesc.Output()).New()
|
||||
|
||||
if err = jsonpbcodec.NewCodec().Unmarshal([]byte(task.GRPC.Data), req); err != nil {
|
||||
l.Error(ctx, "failed to unmarshal", err)
|
||||
continue
|
||||
}
|
||||
|
||||
treq := c.NewRequest(pkg, svc+"."+mth, req)
|
||||
|
||||
sch.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.GRPC.Addr))
|
||||
err = grpcconn.Call(ctx, l, c, task.GRPC.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()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user