Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2024-11-17 13:16:28 +03:00
parent 1bba83e6fc
commit 09c107ac3e

View File

@ -159,6 +159,21 @@ func main() {
func newHTTPTask(ctx context.Context, l logger.Logger, m meter.Meter, check string, task *config.Task) (any, []any, error) {
var err error
c, ok := clients["http"]
if !ok {
err = fmt.Errorf("unknown client http")
l.Error(ctx, "failed to get client", err)
return nil, nil, err
}
var req interface{}
var rsp interface{}
var treq client.Request
var opts []client.CallOption
var labels []string
if task.HTTP.OpenAPI != "" {
openapiBuf, err := os.ReadFile(task.HTTP.OpenAPI)
if err != nil {
l.Error(ctx, "failed to unmarshal openapi file", err)
@ -172,30 +187,27 @@ func newHTTPTask(ctx context.Context, l logger.Logger, m meter.Meter, check stri
}
_ = doc
c, ok := clients["http"]
if !ok {
err = fmt.Errorf("unknown client http")
l.Error(ctx, "failed to get client", err)
return nil, nil, err
}
errmap := make(map[string]interface{}, 1)
errmap["default"] = &codecpb.Frame{}
opts := []client.CallOption{
opts = []client.CallOption{
httpcli.ErrorMap(errmap),
httpcli.Method(task.HTTP.Method),
httpcli.Path(task.HTTP.Endpoint),
// client.WithContentType("application/json"),
}
rsp := &codecpb.Frame{}
treq := c.NewRequest(task.Name, task.Name, &codecpb.Frame{Data: []byte(task.HTTP.Data)})
req = &codecpb.Frame{Data: []byte(task.HTTP.Data)}
rsp = &codecpb.Frame{}
treq = c.NewRequest(task.Name, task.Name, req)
labels = []string{"check", check, "task", task.Name, "service", task.Name, "endpoint", task.Name}
}
fn := func() {
labels := []string{"check", check, "task", task.Name, "service", task.Name, "endpoint", task.Name}
m.Counter(semconv.ClientRequestInflight, labels...).Inc()
ts := time.Now()
l.Info(ctx, fmt.Sprintf("try to call %s.%s via %s", task.Name, task.Name, task.HTTP.Addr))
// l.Info(ctx, fmt.Sprintf("try to call %s.%s via %s", task.Name, task.Name, task.HTTP.Addr))
err = httpconn.Call(ctx, l, c, task.HTTP.Addr, time.Duration(task.Timeout),
treq,
rsp,
@ -217,6 +229,19 @@ func newHTTPTask(ctx context.Context, l logger.Logger, m meter.Meter, check stri
func newGRPCTask(ctx context.Context, l logger.Logger, m meter.Meter, check string, task *config.Task) (any, []any, error) {
var err error
c, ok := clients["grpc"]
if !ok {
err = fmt.Errorf("unknown client grpc")
l.Error(ctx, "failed to get client", err)
return nil, nil, err
}
var req interface{}
var rsp interface{}
var treq client.Request
var labels []string
if task.GRPC.Protoset != "" {
protosetBuf, err := os.ReadFile(task.GRPC.Protoset)
if err != nil {
l.Error(ctx, "failed to unmarshal protoset file", err)
@ -236,13 +261,6 @@ func newGRPCTask(ctx context.Context, l logger.Logger, m meter.Meter, check stri
return nil, nil, err
}
c, ok := clients["grpc"]
if !ok {
err = fmt.Errorf("unknown client grpc")
l.Error(ctx, "failed to get client", err)
return nil, nil, err
}
pkg, svc, mth := grpcconn.ServiceMethod(task.GRPC.Endpoint)
pdesc, err := pfiles.FindDescriptorByName(protoreflect.FullName(pkg + "." + svc))
if err != nil {
@ -264,21 +282,23 @@ func newGRPCTask(ctx context.Context, l logger.Logger, m meter.Meter, check stri
return nil, nil, err
}
req := dynamicpb.NewMessageType(mdesc.Input()).New()
rsp := dynamicpb.NewMessageType(mdesc.Output()).New()
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)
return nil, nil, err
}
treq := c.NewRequest(pkg, svc+"."+mth, req)
treq = c.NewRequest(pkg, svc+"."+mth, req)
labels = []string{"check", check, "task", task.Name, "service", svc, "endpoint", mth}
}
fn := func() {
labels := []string{"check", check, "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)