mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
CloudMigration: Add user uid to on prem event collection (#94804)
* add useruid to report events * test
This commit is contained in:
parent
3e9033c3bc
commit
924b0d0863
@ -245,7 +245,7 @@ func (cma *CloudMigrationAPI) CreateSession(c *contextmodel.ReqContext) response
|
||||
|
||||
return response.ErrOrFallback(http.StatusBadRequest, "bad request data", err)
|
||||
}
|
||||
s, err := cma.cloudMigrationService.CreateSession(ctx, cloudmigration.CloudMigrationSessionRequest{
|
||||
s, err := cma.cloudMigrationService.CreateSession(ctx, c.SignedInUser, cloudmigration.CloudMigrationSessionRequest{
|
||||
AuthToken: cmd.AuthToken,
|
||||
OrgID: c.SignedInUser.OrgID,
|
||||
})
|
||||
@ -286,7 +286,7 @@ func (cma *CloudMigrationAPI) DeleteSession(c *contextmodel.ReqContext) response
|
||||
return response.ErrOrFallback(http.StatusBadRequest, "invalid session uid", err)
|
||||
}
|
||||
|
||||
_, err := cma.cloudMigrationService.DeleteSession(ctx, c.OrgID, uid)
|
||||
_, err := cma.cloudMigrationService.DeleteSession(ctx, c.OrgID, c.SignedInUser, uid)
|
||||
if err != nil {
|
||||
span.SetStatus(codes.Error, "session delete error")
|
||||
span.RecordError(err)
|
||||
@ -511,7 +511,7 @@ func (cma *CloudMigrationAPI) UploadSnapshot(c *contextmodel.ReqContext) respons
|
||||
return response.ErrOrFallback(http.StatusBadRequest, "invalid snapshot uid", err)
|
||||
}
|
||||
|
||||
if err := cma.cloudMigrationService.UploadSnapshot(ctx, c.OrgID, sessUid, snapshotUid); err != nil {
|
||||
if err := cma.cloudMigrationService.UploadSnapshot(ctx, c.OrgID, c.SignedInUser, sessUid, snapshotUid); err != nil {
|
||||
span.SetStatus(codes.Error, "error uploading snapshot")
|
||||
span.RecordError(err)
|
||||
|
||||
|
@ -16,14 +16,14 @@ type Service interface {
|
||||
ValidateToken(ctx context.Context, mig CloudMigrationSession) error
|
||||
DeleteToken(ctx context.Context, uid string) error
|
||||
|
||||
CreateSession(ctx context.Context, req CloudMigrationSessionRequest) (*CloudMigrationSessionResponse, error)
|
||||
CreateSession(ctx context.Context, signedInUser *user.SignedInUser, req CloudMigrationSessionRequest) (*CloudMigrationSessionResponse, error)
|
||||
GetSession(ctx context.Context, orgID int64, migUID string) (*CloudMigrationSession, error)
|
||||
DeleteSession(ctx context.Context, orgID int64, migUID string) (*CloudMigrationSession, error)
|
||||
DeleteSession(ctx context.Context, orgID int64, signedInUser *user.SignedInUser, migUID string) (*CloudMigrationSession, error)
|
||||
GetSessionList(ctx context.Context, orgID int64) (*CloudMigrationSessionListResponse, error)
|
||||
|
||||
CreateSnapshot(ctx context.Context, signedInUser *user.SignedInUser, sessionUid string) (*CloudMigrationSnapshot, error)
|
||||
GetSnapshot(ctx context.Context, query GetSnapshotsQuery) (*CloudMigrationSnapshot, error)
|
||||
GetSnapshotList(ctx context.Context, query ListSnapshotsQuery) ([]CloudMigrationSnapshot, error)
|
||||
UploadSnapshot(ctx context.Context, orgID int64, sessionUid string, snapshotUid string) error
|
||||
UploadSnapshot(ctx context.Context, orgID int64, signedInUser *user.SignedInUser, sessionUid string, snapshotUid string) error
|
||||
CancelSnapshot(ctx context.Context, sessionUid string, snapshotUid string) error
|
||||
}
|
||||
|
@ -394,7 +394,7 @@ func (s *Service) GetSessionList(ctx context.Context, orgID int64) (*cloudmigrat
|
||||
return &cloudmigration.CloudMigrationSessionListResponse{Sessions: migrations}, nil
|
||||
}
|
||||
|
||||
func (s *Service) CreateSession(ctx context.Context, cmd cloudmigration.CloudMigrationSessionRequest) (*cloudmigration.CloudMigrationSessionResponse, error) {
|
||||
func (s *Service) CreateSession(ctx context.Context, signedInUser *user.SignedInUser, cmd cloudmigration.CloudMigrationSessionRequest) (*cloudmigration.CloudMigrationSessionResponse, error) {
|
||||
ctx, span := s.tracer.Start(ctx, "CloudMigrationService.CreateSession")
|
||||
defer span.End()
|
||||
|
||||
@ -419,7 +419,7 @@ func (s *Service) CreateSession(ctx context.Context, cmd cloudmigration.CloudMig
|
||||
return nil, cloudmigration.ErrSessionCreationFailure.Errorf("error creating migration")
|
||||
}
|
||||
|
||||
s.report(ctx, &migration, gmsclient.EventConnect, 0, nil)
|
||||
s.report(ctx, &migration, gmsclient.EventConnect, 0, nil, signedInUser.UserUID)
|
||||
|
||||
return &cloudmigration.CloudMigrationSessionResponse{
|
||||
UID: cm.UID,
|
||||
@ -429,18 +429,18 @@ func (s *Service) CreateSession(ctx context.Context, cmd cloudmigration.CloudMig
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Service) DeleteSession(ctx context.Context, orgID int64, sessionUID string) (*cloudmigration.CloudMigrationSession, error) {
|
||||
func (s *Service) DeleteSession(ctx context.Context, orgID int64, signedInUser *user.SignedInUser, sessionUID string) (*cloudmigration.CloudMigrationSession, error) {
|
||||
ctx, span := s.tracer.Start(ctx, "CloudMigrationService.DeleteSession")
|
||||
defer span.End()
|
||||
|
||||
session, snapshots, err := s.store.DeleteMigrationSessionByUID(ctx, orgID, sessionUID)
|
||||
if err != nil {
|
||||
s.report(ctx, session, gmsclient.EventDisconnect, 0, err)
|
||||
s.report(ctx, session, gmsclient.EventDisconnect, 0, err, signedInUser.UserUID)
|
||||
return nil, fmt.Errorf("deleting migration from db for session %v: %w", sessionUID, err)
|
||||
}
|
||||
|
||||
err = s.deleteLocalFiles(snapshots)
|
||||
s.report(ctx, session, gmsclient.EventDisconnect, 0, err)
|
||||
s.report(ctx, session, gmsclient.EventDisconnect, 0, err, signedInUser.UserUID)
|
||||
return session, nil
|
||||
}
|
||||
|
||||
@ -506,7 +506,7 @@ func (s *Service) CreateSnapshot(ctx context.Context, signedInUser *user.SignedI
|
||||
asyncCtx, cancelFunc := context.WithCancel(asyncCtx)
|
||||
s.cancelFunc = cancelFunc
|
||||
|
||||
s.report(asyncCtx, session, gmsclient.EventStartBuildingSnapshot, 0, nil)
|
||||
s.report(asyncCtx, session, gmsclient.EventStartBuildingSnapshot, 0, nil, signedInUser.UserUID)
|
||||
|
||||
start := time.Now()
|
||||
err := s.buildSnapshot(asyncCtx, signedInUser, initResp.MaxItemsPerPartition, initResp.Metadata, snapshot)
|
||||
@ -527,7 +527,7 @@ func (s *Service) CreateSnapshot(ctx context.Context, signedInUser *user.SignedI
|
||||
}
|
||||
|
||||
span.SetStatus(codes.Ok, "snapshot built")
|
||||
s.report(asyncCtx, session, gmsclient.EventDoneBuildingSnapshot, time.Since(start), err)
|
||||
s.report(asyncCtx, session, gmsclient.EventDoneBuildingSnapshot, time.Since(start), err, signedInUser.UserUID)
|
||||
}()
|
||||
|
||||
return &snapshot, nil
|
||||
@ -685,7 +685,7 @@ func (s *Service) GetSnapshotList(ctx context.Context, query cloudmigration.List
|
||||
return snapshotList, nil
|
||||
}
|
||||
|
||||
func (s *Service) UploadSnapshot(ctx context.Context, orgID int64, sessionUid string, snapshotUid string) error {
|
||||
func (s *Service) UploadSnapshot(ctx context.Context, orgID int64, signedInUser *user.SignedInUser, sessionUid string, snapshotUid string) error {
|
||||
ctx, span := s.tracer.Start(ctx, "CloudMigrationService.UploadSnapshot",
|
||||
trace.WithAttributes(
|
||||
attribute.String("sessionUid", sessionUid),
|
||||
@ -740,7 +740,7 @@ func (s *Service) UploadSnapshot(ctx context.Context, orgID int64, sessionUid st
|
||||
|
||||
asyncCtx, s.cancelFunc = context.WithCancel(asyncCtx)
|
||||
|
||||
s.report(asyncCtx, session, gmsclient.EventStartUploadingSnapshot, 0, nil)
|
||||
s.report(asyncCtx, session, gmsclient.EventStartUploadingSnapshot, 0, nil, signedInUser.UserUID)
|
||||
|
||||
start := time.Now()
|
||||
err := s.uploadSnapshot(asyncCtx, session, snapshot, uploadUrl)
|
||||
@ -760,7 +760,7 @@ func (s *Service) UploadSnapshot(ctx context.Context, orgID int64, sessionUid st
|
||||
}
|
||||
}
|
||||
|
||||
s.report(asyncCtx, session, gmsclient.EventDoneUploadingSnapshot, time.Since(start), err)
|
||||
s.report(asyncCtx, session, gmsclient.EventDoneUploadingSnapshot, time.Since(start), err, signedInUser.UserUID)
|
||||
}()
|
||||
|
||||
return nil
|
||||
@ -808,6 +808,7 @@ func (s *Service) report(
|
||||
t gmsclient.LocalEventType,
|
||||
d time.Duration,
|
||||
evtErr error,
|
||||
userUID string,
|
||||
) {
|
||||
ctx, span := s.tracer.Start(ctx, "CloudMigrationService.report")
|
||||
defer span.End()
|
||||
@ -832,6 +833,7 @@ func (s *Service) report(
|
||||
e := gmsclient.EventRequestDTO{
|
||||
Event: t,
|
||||
LocalID: id,
|
||||
UserUID: userUID,
|
||||
}
|
||||
|
||||
if d != 0 {
|
||||
|
@ -37,11 +37,11 @@ func (s *NoopServiceImpl) GetSessionList(ctx context.Context, orgID int64) (*clo
|
||||
return nil, cloudmigration.ErrFeatureDisabledError
|
||||
}
|
||||
|
||||
func (s *NoopServiceImpl) CreateSession(ctx context.Context, cm cloudmigration.CloudMigrationSessionRequest) (*cloudmigration.CloudMigrationSessionResponse, error) {
|
||||
func (s *NoopServiceImpl) CreateSession(ctx context.Context, signedInUser *user.SignedInUser, cm cloudmigration.CloudMigrationSessionRequest) (*cloudmigration.CloudMigrationSessionResponse, error) {
|
||||
return nil, cloudmigration.ErrMigrationDisabled
|
||||
}
|
||||
|
||||
func (s *NoopServiceImpl) DeleteSession(ctx context.Context, orgID int64, uid string) (*cloudmigration.CloudMigrationSession, error) {
|
||||
func (s *NoopServiceImpl) DeleteSession(ctx context.Context, orgID int64, signedInUser *user.SignedInUser, uid string) (*cloudmigration.CloudMigrationSession, error) {
|
||||
return nil, cloudmigration.ErrFeatureDisabledError
|
||||
}
|
||||
|
||||
@ -57,7 +57,7 @@ func (s *NoopServiceImpl) GetSnapshotList(ctx context.Context, query cloudmigrat
|
||||
return nil, cloudmigration.ErrFeatureDisabledError
|
||||
}
|
||||
|
||||
func (s *NoopServiceImpl) UploadSnapshot(ctx context.Context, orgID int64, sessionUid string, snapshotUid string) error {
|
||||
func (s *NoopServiceImpl) UploadSnapshot(ctx context.Context, orgID int64, signedInUser *user.SignedInUser, sessionUid string, snapshotUid string) error {
|
||||
return cloudmigration.ErrFeatureDisabledError
|
||||
}
|
||||
|
||||
|
@ -547,12 +547,13 @@ func Test_NonCoreDataSourcesHaveWarning(t *testing.T) {
|
||||
|
||||
func TestDeleteSession(t *testing.T) {
|
||||
s := setUpServiceTest(t, false).(*Service)
|
||||
user := &user.SignedInUser{UserUID: "user123"}
|
||||
|
||||
t.Run("when deleting a session that does not exist in the database, it returns an error", func(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
t.Cleanup(cancel)
|
||||
|
||||
session, err := s.DeleteSession(ctx, 2, "invalid-session-uid")
|
||||
session, err := s.DeleteSession(ctx, 2, user, "invalid-session-uid")
|
||||
require.Nil(t, session)
|
||||
require.Error(t, err)
|
||||
})
|
||||
@ -570,12 +571,12 @@ func TestDeleteSession(t *testing.T) {
|
||||
OrgID: 3,
|
||||
}
|
||||
|
||||
createResp, err := s.CreateSession(ctx, cmd)
|
||||
createResp, err := s.CreateSession(ctx, user, cmd)
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, createResp.UID)
|
||||
require.NotEmpty(t, createResp.Slug)
|
||||
|
||||
deletedSession, err := s.DeleteSession(ctx, cmd.OrgID, createResp.UID)
|
||||
deletedSession, err := s.DeleteSession(ctx, cmd.OrgID, user, createResp.UID)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, deletedSession)
|
||||
require.Equal(t, deletedSession.UID, createResp.UID)
|
||||
@ -597,7 +598,7 @@ func TestReportEvent(t *testing.T) {
|
||||
s.gmsClient = gmsMock
|
||||
|
||||
require.NotPanics(t, func() {
|
||||
s.report(ctx, nil, gmsclient.EventConnect, time.Minute, nil)
|
||||
s.report(ctx, nil, gmsclient.EventConnect, time.Minute, nil, "user123")
|
||||
})
|
||||
|
||||
require.Zero(t, gmsMock.reportEventCalled)
|
||||
@ -613,7 +614,7 @@ func TestReportEvent(t *testing.T) {
|
||||
s.gmsClient = gmsMock
|
||||
|
||||
require.NotPanics(t, func() {
|
||||
s.report(ctx, &cloudmigration.CloudMigrationSession{}, gmsclient.EventConnect, time.Minute, nil)
|
||||
s.report(ctx, &cloudmigration.CloudMigrationSession{}, gmsclient.EventConnect, time.Minute, nil, "user123")
|
||||
})
|
||||
|
||||
require.Equal(t, 1, gmsMock.reportEventCalled)
|
||||
|
@ -45,7 +45,7 @@ func (m FakeServiceImpl) DeleteToken(_ context.Context, _ string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m FakeServiceImpl) CreateSession(_ context.Context, _ cloudmigration.CloudMigrationSessionRequest) (*cloudmigration.CloudMigrationSessionResponse, error) {
|
||||
func (m FakeServiceImpl) CreateSession(_ context.Context, _ *user.SignedInUser, _ cloudmigration.CloudMigrationSessionRequest) (*cloudmigration.CloudMigrationSessionResponse, error) {
|
||||
if m.ReturnError {
|
||||
return nil, cloudmigration.ErrSessionCreationFailure
|
||||
}
|
||||
@ -64,7 +64,7 @@ func (m FakeServiceImpl) GetSession(_ context.Context, _ int64, _ string) (*clou
|
||||
return &cloudmigration.CloudMigrationSession{UID: "fake"}, nil
|
||||
}
|
||||
|
||||
func (m FakeServiceImpl) DeleteSession(_ context.Context, _ int64, _ string) (*cloudmigration.CloudMigrationSession, error) {
|
||||
func (m FakeServiceImpl) DeleteSession(_ context.Context, _ int64, _ *user.SignedInUser, _ string) (*cloudmigration.CloudMigrationSession, error) {
|
||||
if m.ReturnError {
|
||||
return nil, fmt.Errorf("mock error")
|
||||
}
|
||||
@ -154,7 +154,7 @@ func (m FakeServiceImpl) GetSnapshotList(ctx context.Context, query cloudmigrati
|
||||
return cloudSnapshots, nil
|
||||
}
|
||||
|
||||
func (m FakeServiceImpl) UploadSnapshot(ctx context.Context, _ int64, sessionUid string, snapshotUid string) error {
|
||||
func (m FakeServiceImpl) UploadSnapshot(ctx context.Context, _ int64, _ *user.SignedInUser, sessionUid string, snapshotUid string) error {
|
||||
if m.ReturnError {
|
||||
return fmt.Errorf("mock error")
|
||||
}
|
||||
|
@ -50,6 +50,7 @@ type EventRequestDTO struct {
|
||||
Event LocalEventType `json:"event"`
|
||||
Error string `json:"error"`
|
||||
DurationIfFinished time.Duration `json:"duration"`
|
||||
UserUID string `json:"userUid"`
|
||||
}
|
||||
|
||||
type LocalEventType string
|
||||
|
Loading…
Reference in New Issue
Block a user