devices: added tracing and telemetry to devices_list and update

This commit is contained in:
Tigor Hutasuhut 2024-08-15 10:40:35 +07:00
parent cf14079f1f
commit 6c09278e0a
4 changed files with 18 additions and 7 deletions

View file

@ -2,6 +2,7 @@ package api
import (
"github.com/tigorlazuardi/bluemage/go/pkg/errs"
"github.com/tigorlazuardi/bluemage/go/pkg/telemetry"
"golang.org/x/net/context"
. "github.com/go-jet/jet/v2/sqlite"
@ -61,7 +62,7 @@ func (request ListDevicesRequest) Statement() SelectStatement {
func (api *API) DevicesList(ctx context.Context, req ListDevicesRequest) (resp []model.Devices, err error) {
ctx, span := tracer.Start(ctx, "DevicesList")
defer span.End()
defer func() { telemetry.EndWithStatus(span, err) }()
stmt := req.Statement()
if err := stmt.QueryContext(ctx, api.DB, &resp); err != nil {

View file

@ -8,16 +8,21 @@ import (
"github.com/tigorlazuardi/bluemage/go/gen/models"
"github.com/tigorlazuardi/bluemage/go/pkg/errs"
"github.com/tigorlazuardi/bluemage/go/pkg/log"
"github.com/tigorlazuardi/bluemage/go/pkg/telemetry"
)
func (api *API) DevicesUpdate(ctx context.Context, slug string, update *models.DeviceSetter) (err error) {
ctx, span := tracer.Start(ctx, "DevicesUpdate")
defer func() { telemetry.EndWithStatus(span, err) }()
exist, err := api.DevicesExist(ctx, slug)
if err != nil {
return err
}
if !exist {
return errs.Failf("device with slug %q does not exist", slug)
err = errs.Failf("device with slug %q does not exist", slug)
return err
}
ctx, coll := log.WithQueryCollector(ctx)
@ -30,16 +35,19 @@ func (api *API) DevicesUpdate(ctx context.Context, slug string, update *models.D
if err != nil {
if sqlite3Err := new(sqlite3.Error); errors.As(err, &sqlite3Err) {
if sqlite3Err.Code == sqlite3.ErrConstraint {
return errs.
err = errs.
Wrapw(
err, "a device with the same slug already exists",
"slug", slug,
"update", update,
"query", coll,
)
return err
}
}
return errs.Wrapw(err, "failed to update device", "slug", slug, "update", update, "query", coll)
err = errs.Wrapw(err, "failed to update device", "slug", slug, "update", update, "query", coll)
return err
}
return nil

View file

@ -40,7 +40,7 @@ type DeviceConverter interface {
JetModelDeviceToProtoDevice(model.Devices) *device.Device
// goverter:useZeroValueOnPointerInconsistency
ListDevicesRequestToAPIListDevicesRequest(*device.ListDevicesRequest) api.ListDevicesRequest
ProtoListDevicesRequestToAPIListDevicesRequest(*device.ListDevicesRequest) api.ListDevicesRequest
// goverter:ignore Slug SingleFolderMode CreatedAt UpdatedAt
// goverter:map Nsfw NSFW

View file

@ -48,12 +48,14 @@ func (d *DeviceHandler) GetDevice(ctx context.Context, request *connect.Request[
// ListDevices implements v1connect.DeviceServiceHandler.
func (d *DeviceHandler) ListDevices(ctx context.Context, request *connect.Request[device.ListDevicesRequest]) (*connect.Response[device.ListDevicesResponse], error) {
listRequest := deviceConvert.ListDevicesRequestToAPIListDevicesRequest(request.Msg)
listRequest := deviceConvert.ProtoListDevicesRequestToAPIListDevicesRequest(request.Msg)
devices, err := d.API.DevicesList(ctx, listRequest)
if err != nil {
return nil, errs.IntoConnectError(err)
}
resp := &device.ListDevicesResponse{}
resp := &device.ListDevicesResponse{
Devices: make([]*device.Device, 0, len(devices)),
}
for _, device := range devices {
resp.Devices = append(resp.Devices, deviceConvert.JetModelDeviceToProtoDevice(device))
}