-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
feat: Initial monitors implementation #11602
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| from __future__ import absolute_import | ||
|
|
||
| from django.db import transaction | ||
| from django.utils import timezone | ||
| from rest_framework import serializers | ||
|
|
||
| from sentry import features | ||
| from sentry.api.authentication import DSNAuthentication | ||
| from sentry.api.base import Endpoint | ||
| from sentry.api.exceptions import ResourceDoesNotExist | ||
| from sentry.api.bases.project import ProjectPermission | ||
| from sentry.api.serializers import serialize | ||
| from sentry.models import Monitor, MonitorCheckIn, CheckInStatus, MonitorStatus, Project, ProjectKey, ProjectStatus | ||
| from sentry.utils.sdk import configure_scope | ||
|
|
||
|
|
||
| class CheckInSerializer(serializers.Serializer): | ||
| status = serializers.ChoiceField( | ||
| choices=( | ||
| ('ok', CheckInStatus.OK), | ||
| ('error', CheckInStatus.ERROR), | ||
| ('in_progress', CheckInStatus.IN_PROGRESS), | ||
| ), | ||
| ) | ||
| duration = serializers.IntegerField(required=False) | ||
|
|
||
|
|
||
| class MonitorCheckInDetailsEndpoint(Endpoint): | ||
| authentication_classes = Endpoint.authentication_classes + (DSNAuthentication,) | ||
| permission_classes = (ProjectPermission,) | ||
|
|
||
| # TODO(dcramer): this code needs shared with other endpoints as its security focused | ||
| # TODO(dcramer): this doesnt handle is_global roles | ||
| def convert_args(self, request, monitor_id, checkin_id, *args, **kwargs): | ||
| try: | ||
| monitor = Monitor.objects.get( | ||
| guid=monitor_id, | ||
| ) | ||
| except Monitor.DoesNotExist: | ||
| raise ResourceDoesNotExist | ||
|
|
||
| project = Project.objects.get_from_cache(id=monitor.project_id) | ||
| if project.status != ProjectStatus.VISIBLE: | ||
| raise ResourceDoesNotExist | ||
|
|
||
| if hasattr(request.auth, 'project_id') and project.id != request.auth.project_id: | ||
| return self.respond(status=400) | ||
|
|
||
| if not features.has('organizations:monitors', | ||
| project.organization, actor=request.user): | ||
| raise ResourceDoesNotExist | ||
|
|
||
| self.check_object_permissions(request, project) | ||
|
|
||
| with configure_scope() as scope: | ||
| scope.set_tag("organization", project.organization_id) | ||
| scope.set_tag("project", project.id) | ||
|
|
||
| try: | ||
| checkin = MonitorCheckIn.objects.get( | ||
| monitor=monitor, | ||
| guid=checkin_id, | ||
| ) | ||
| except MonitorCheckIn.DoesNotExist: | ||
| raise ResourceDoesNotExist | ||
|
|
||
| request._request.organization = project.organization | ||
|
|
||
| kwargs.update({ | ||
| 'checkin': checkin, | ||
| 'monitor': monitor, | ||
| 'project': project, | ||
| }) | ||
| return (args, kwargs) | ||
|
|
||
| def get(self, request, project, monitor, checkin): | ||
| """ | ||
| Retrieve a check-in | ||
| `````````````````` | ||
|
|
||
| :pparam string monitor_id: the id of the monitor. | ||
| :pparam string checkin_id: the id of the check-in. | ||
| :auth: required | ||
| """ | ||
| # we dont allow read permission with DSNs | ||
| if isinstance(request.auth, ProjectKey): | ||
| return self.respond(status=401) | ||
|
|
||
| return self.respond(serialize(checkin, request.user)) | ||
|
|
||
| def put(self, request, project, monitor, checkin): | ||
| """ | ||
| Update a check-in | ||
| ````````````````` | ||
|
|
||
| :pparam string monitor_id: the id of the monitor. | ||
| :pparam string checkin_id: the id of the check-in. | ||
| :auth: required | ||
| """ | ||
| if checkin.status in CheckInStatus.FINISHED_VALUES: | ||
| return self.respond(status=400) | ||
|
|
||
| serializer = CheckInSerializer( | ||
| data=request.DATA, | ||
| partial=True, | ||
| context={ | ||
| 'project': project, | ||
| 'request': request, | ||
| }, | ||
| ) | ||
| if not serializer.is_valid(): | ||
| return self.respond(serializer.errors, status=400) | ||
|
|
||
| result = serializer.object | ||
|
|
||
| current_datetime = timezone.now() | ||
| params = { | ||
| 'date_updated': current_datetime, | ||
| } | ||
| if 'duration' in result: | ||
| params['duration'] = result['duration'] | ||
| if 'status' in result: | ||
| params['status'] = getattr(CheckInStatus, result['status'].upper()) | ||
|
|
||
| with transaction.atomic(): | ||
| checkin.update(**params) | ||
| if checkin.status == CheckInStatus.ERROR: | ||
| monitor.mark_failed(current_datetime) | ||
| else: | ||
| monitor_params = { | ||
| 'last_checkin': current_datetime, | ||
| 'next_checkin': monitor.get_next_scheduled_checkin(current_datetime), | ||
| } | ||
| if checkin.status == CheckInStatus.OK: | ||
| monitor_params['status'] = MonitorStatus.OK | ||
| Monitor.objects.filter( | ||
| id=monitor.id, | ||
| ).exclude( | ||
| last_checkin__gt=current_datetime, | ||
| ).update(**monitor_params) | ||
|
|
||
| return self.respond(serialize(checkin, request.user)) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| from __future__ import absolute_import | ||
|
|
||
| from django.db import transaction | ||
| from rest_framework import serializers | ||
|
|
||
| from sentry import features | ||
| from sentry.api.authentication import DSNAuthentication | ||
| from sentry.api.base import Endpoint | ||
| from sentry.api.exceptions import ResourceDoesNotExist | ||
| from sentry.api.paginator import OffsetPaginator | ||
| from sentry.api.bases.project import ProjectPermission | ||
| from sentry.api.serializers import serialize | ||
| from sentry.models import Monitor, MonitorCheckIn, MonitorStatus, CheckInStatus, Project, ProjectKey, ProjectStatus | ||
| from sentry.utils.sdk import configure_scope | ||
|
|
||
|
|
||
| class CheckInSerializer(serializers.Serializer): | ||
| status = serializers.ChoiceField( | ||
| choices=( | ||
| ('ok', CheckInStatus.OK), | ||
| ('error', CheckInStatus.ERROR), | ||
| ('in_progress', CheckInStatus.IN_PROGRESS), | ||
| ), | ||
| ) | ||
| duration = serializers.IntegerField(required=False) | ||
|
|
||
|
|
||
| class MonitorCheckInsEndpoint(Endpoint): | ||
| authentication_classes = Endpoint.authentication_classes + (DSNAuthentication,) | ||
| permission_classes = (ProjectPermission,) | ||
|
|
||
| # TODO(dcramer): this code needs shared with other endpoints as its security focused | ||
| # TODO(dcramer): this doesnt handle is_global roles | ||
| def convert_args(self, request, monitor_id, *args, **kwargs): | ||
| try: | ||
| monitor = Monitor.objects.get( | ||
| guid=monitor_id, | ||
| ) | ||
| except Monitor.DoesNotExist: | ||
| raise ResourceDoesNotExist | ||
|
|
||
| project = Project.objects.get_from_cache(id=monitor.project_id) | ||
| if project.status != ProjectStatus.VISIBLE: | ||
| raise ResourceDoesNotExist | ||
|
|
||
| if hasattr(request.auth, 'project_id') and project.id != request.auth.project_id: | ||
| return self.respond(status=400) | ||
|
|
||
| if not features.has('organizations:monitors', | ||
| project.organization, actor=request.user): | ||
| raise ResourceDoesNotExist | ||
|
|
||
| self.check_object_permissions(request, project) | ||
|
|
||
| with configure_scope() as scope: | ||
| scope.set_tag("organization", project.organization_id) | ||
| scope.set_tag("project", project.id) | ||
|
|
||
| request._request.organization = project.organization | ||
|
|
||
| kwargs.update({ | ||
| 'monitor': monitor, | ||
| 'project': project, | ||
| }) | ||
| return (args, kwargs) | ||
|
|
||
| def get(self, request, project, monitor): | ||
| """ | ||
| Retrieve check-ins for an monitor | ||
| ````````````````````````````````` | ||
|
|
||
| :pparam string monitor_id: the id of the monitor. | ||
| :auth: required | ||
| """ | ||
| # we dont allow read permission with DSNs | ||
| if isinstance(request.auth, ProjectKey): | ||
| return self.respond(status=401) | ||
|
|
||
| queryset = MonitorCheckIn.objects.filter( | ||
| monitor_id=monitor.id, | ||
| ) | ||
|
|
||
| return self.paginate( | ||
| request=request, | ||
| queryset=queryset, | ||
| order_by='name', | ||
| on_results=lambda x: serialize(x, request.user), | ||
| paginator_cls=OffsetPaginator, | ||
| ) | ||
|
|
||
| def post(self, request, project, monitor): | ||
| """ | ||
| Create a new check-in for a monitor | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you thinking that users would make an API request directly, or would monitor creation be part of the SDKs?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it will happen via the UI or API |
||
| ``````````````````````````````````` | ||
|
|
||
| :pparam string monitor_id: the id of the monitor. | ||
| :auth: required | ||
| """ | ||
| serializer = CheckInSerializer( | ||
| data=request.DATA, | ||
| context={ | ||
| 'project': project, | ||
| 'request': request, | ||
| }, | ||
| ) | ||
| if not serializer.is_valid(): | ||
| return self.respond(serializer.errors, status=400) | ||
|
|
||
| result = serializer.object | ||
|
|
||
| with transaction.atomic(): | ||
| checkin = MonitorCheckIn.objects.create( | ||
| project_id=project.id, | ||
| monitor_id=monitor.id, | ||
| duration=result.get('duration'), | ||
| status=getattr(CheckInStatus, result['status'].upper()), | ||
| ) | ||
| if checkin.status == CheckInStatus.ERROR: | ||
| monitor.mark_failed(last_checkin=checkin.date_added) | ||
| else: | ||
| monitor_params = { | ||
| 'last_checkin': checkin.date_added, | ||
| 'next_checkin': monitor.get_next_scheduled_checkin(checkin.date_added), | ||
| } | ||
| if checkin.status == CheckInStatus.OK: | ||
| monitor_params['status'] = MonitorStatus.OK | ||
| Monitor.objects.filter( | ||
| id=monitor.id, | ||
| ).exclude( | ||
| last_checkin__gt=checkin.date_added, | ||
| ).update(**monitor_params) | ||
|
|
||
| return self.respond(serialize(checkin, request.user)) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| from __future__ import absolute_import | ||
|
|
||
| import six | ||
|
|
||
| from sentry.api.serializers import Serializer, register | ||
| from sentry.models import MonitorCheckIn | ||
|
|
||
|
|
||
| @register(MonitorCheckIn) | ||
| class MonitorCheckInSerializer(Serializer): | ||
| def serialize(self, obj, attrs, user): | ||
| return { | ||
| 'id': six.text_type(obj.guid), | ||
| 'status': obj.get_status_display(), | ||
| 'duration': obj.duration, | ||
| 'dateCreated': obj.date_added, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.