Skip to content

EmbedAPI: log success requests #8689

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 10 commits into from
Nov 30, 2021
2 changes: 1 addition & 1 deletion common
6 changes: 3 additions & 3 deletions readthedocs/analytics/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import hashlib
import ipaddress
import logging
import structlog

import requests
from django.conf import settings
Expand All @@ -13,7 +13,7 @@
from user_agents import parse


log = logging.getLogger(__name__) # noqa
log = structlog.get_logger(__name__) # noqa


def get_client_ip(request):
Expand Down Expand Up @@ -79,7 +79,7 @@ def send_to_analytics(data):
data['ua'] = anonymize_user_agent(data['ua'])

resp = None
log.debug('Sending data to analytics: %s', data)
log.debug('Sending data to analytics.', data=data)
try:
resp = requests.post(
'https://www.google-analytics.com/collect',
Expand Down
10 changes: 5 additions & 5 deletions readthedocs/api/v2/client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Simple client to access our API with Slumber credentials."""

import logging
import structlog

import requests
from django.conf import settings
Expand All @@ -10,7 +10,7 @@

from .adapters import TimeoutHostHeaderSSLAdapter, TimeoutHTTPAdapter

log = logging.getLogger(__name__)
log = structlog.get_logger(__name__)


class DrfJsonSerializer(serialize.JsonSerializer):
Expand Down Expand Up @@ -63,9 +63,9 @@ def setup_api():
}
if settings.SLUMBER_USERNAME and settings.SLUMBER_PASSWORD:
log.debug(
'Using slumber v2 with user %s, pointed at %s',
settings.SLUMBER_USERNAME,
settings.SLUMBER_API_HOST,
'Using slumber v2.',
username=settings.SLUMBER_USERNAME,
api_host=settings.SLUMBER_API_HOST,
)
session.auth = (settings.SLUMBER_USERNAME, settings.SLUMBER_PASSWORD)
else:
Expand Down
4 changes: 2 additions & 2 deletions readthedocs/api/v2/mixins.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
import structlog

log = logging.getLogger(__name__)
log = structlog.get_logger(__name__)


class CachedResponseMixin:
Expand Down
18 changes: 9 additions & 9 deletions readthedocs/api/v2/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Utility functions that are used by both views and celery tasks."""

import itertools
import logging
import structlog

from rest_framework.pagination import PageNumberPagination

Expand All @@ -17,7 +17,7 @@
)
from readthedocs.builds.models import RegexAutomationRule, Version

log = logging.getLogger(__name__)
log = structlog.get_logger(__name__)


def sync_versions_to_db(project, versions, type): # pylint: disable=redefined-builtin
Expand Down Expand Up @@ -86,9 +86,9 @@ def sync_versions_to_db(project, versions, type): # pylint: disable=redefined-b
)

log.info(
'(Sync Versions) Updated Version: [%s=%s] ',
version_name,
version_id,
'Re-syncing versions: version updated.',
version_verbose_name=version_name,
version_id=version_id,
)
else:
# New Version
Expand Down Expand Up @@ -116,8 +116,9 @@ def sync_versions_to_db(project, versions, type): # pylint: disable=redefined-b
latest_version.save()
if added:
log.info(
'(Sync Versions) Added Versions: versions_count=%d versions=[%s]',
len(added), ' '.join(itertools.islice(added, 100)),
'Re-syncing versions: versions added.',
count=len(added),
versions=','.join(itertools.islice(added, 100)),
)
return added

Expand Down Expand Up @@ -214,8 +215,7 @@ def delete_versions_from_db(project, tags_data, branches_data):
_, deleted = to_delete_qs.delete()
versions_count = deleted.get('builds.Version', 0)
log.info(
'(Sync Versions) Deleted Versions: project=%s versions_count=%s',
project.slug, versions_count,
'Re-syncing versions: versions deleted.', project_slug=project.slug, count=versions_count,
)


Expand Down
4 changes: 2 additions & 2 deletions readthedocs/api/v2/views/footer_views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Endpoint to generate footer HTML."""

import logging
import structlog
import re
from functools import lru_cache

Expand All @@ -24,7 +24,7 @@
parse_version_failsafe,
)

log = logging.getLogger(__name__)
log = structlog.get_logger(__name__)


def get_version_compare_data(project, base_version=None):
Expand Down
46 changes: 29 additions & 17 deletions readthedocs/api/v2/views/integrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import hashlib
import hmac
import json
import logging
import structlog
import re

from django.shortcuts import get_object_or_404
Expand All @@ -29,7 +29,7 @@
from readthedocs.integrations.models import HttpExchange, Integration
from readthedocs.projects.models import Feature, Project

log = logging.getLogger(__name__)
log = structlog.get_logger(__name__)

GITHUB_EVENT_HEADER = 'HTTP_X_GITHUB_EVENT'
GITHUB_SIGNATURE_HEADER = 'HTTP_X_HUB_SIGNATURE'
Expand Down Expand Up @@ -86,8 +86,9 @@ def post(self, request, project_slug):
raise NotFound('Project not found')
if not self.is_payload_valid():
log.warning(
'Invalid payload for project: %s and integration: %s',
project_slug, self.integration_type
'Invalid payload for project and integration.',
project_slug=project_slug,
integration_type=self.integration_type,
)
return Response(
{'detail': self.invalid_payload_msg},
Expand Down Expand Up @@ -184,9 +185,9 @@ def get_response_push(self, project, branches):
to_build, not_building = build_branches(project, branches)
if not_building:
log.info(
'Skipping project branches: project=%s branches=%s',
project,
branches,
'Skipping project branches.',
project_slug=project.slug,
branches=branches,
)
triggered = bool(to_build)
return {
Expand Down Expand Up @@ -338,8 +339,8 @@ def is_payload_valid(self):
secret = self.get_integration().secret
if not secret:
log.info(
'Skipping payload signature validation. project=%s',
self.project.slug,
'Skipping payload signature validation.',
project_slug=self.project.slug,
)
return True
if not signature:
Expand Down Expand Up @@ -405,7 +406,11 @@ def handle_webhook(self):

# Sync versions when a branch/tag was created/deleted
if event in (GITHUB_CREATE, GITHUB_DELETE):
log.info('Triggered sync_versions: project=%s event=%s', self.project, event)
log.info(
'Triggered sync_versions.',
project_slug=self.project.slug,
webhook_event=event,
)
return self.sync_versions_response(self.project)

# Handle pull request events
Expand Down Expand Up @@ -441,7 +446,7 @@ def handle_webhook(self):
# already have the CREATE/DELETE events. So we don't trigger the sync twice.
return self.sync_versions_response(self.project, sync=False)

log.info('Triggered sync_versions: project=%s events=%s', self.project, events)
log.info('Triggered sync_versions.', project_slug=self.project.slug, events=events)
return self.sync_versions_response(self.project)

# Trigger a build for all branches in the push
Expand Down Expand Up @@ -512,8 +517,8 @@ def is_payload_valid(self):
secret = self.get_integration().secret
if not secret:
log.info(
'Skipping payload signature validation. project=%s',
self.project.slug,
'Skipping payload signature validation.',
project_slug=self.project.slug,
)
return True
if not token:
Expand Down Expand Up @@ -554,8 +559,12 @@ def handle_webhook(self):
after = data.get('after')
# Tag/branch created/deleted
if GITLAB_NULL_HASH in (before, after):
log.info('Triggered sync_versions: project=%s before=%s after=%s',
self.project, before, after)
log.info(
'Triggered sync_versions.',
project_slug=self.project.slug,
before=before,
after=after,
)
return self.sync_versions_response(self.project)
# Normal push to master
try:
Expand Down Expand Up @@ -652,8 +661,11 @@ def handle_webhook(self):
# will be triggered with the normal push.
if branches:
return self.get_response_push(self.project, branches)
log.info('Triggered sync_versions: project=%s event=%s',
self.project, event)
log.info(
'Triggered sync_versions.',
project_slug=self.project.slug,
webhook_event=event,
)
return self.sync_versions_response(self.project)
except KeyError:
raise ParseError('Invalid request')
Expand Down
8 changes: 4 additions & 4 deletions readthedocs/api/v2/views/model_views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Endpoints for listing Projects, Versions, Builds, etc."""

import json
import logging
import structlog

from allauth.socialaccount.models import SocialAccount
from django.conf import settings
Expand Down Expand Up @@ -40,7 +40,7 @@
RemoteProjectPagination,
)

log = logging.getLogger(__name__)
log = structlog.get_logger(__name__)


class PlainTextBuildRenderer(BaseRenderer):
Expand Down Expand Up @@ -269,8 +269,8 @@ def retrieve(self, *args, **kwargs):
data['commands'] = json.loads(json_resp)
except Exception:
log.exception(
'Failed to read build data from storage. path=%s.',
storage_path,
'Failed to read build data from storage.',
path=storage_path,
)
return Response(data)

Expand Down
4 changes: 2 additions & 2 deletions readthedocs/api/v2/views/task_views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Endpoints relating to task/job status, etc."""

import logging
import structlog

from django.urls import reverse
from redis import ConnectionError
Expand All @@ -12,7 +12,7 @@
from readthedocs.oauth import tasks


log = logging.getLogger(__name__)
log = structlog.get_logger(__name__)

SUCCESS_STATES = ('SUCCESS',)
FAILURE_STATES = (
Expand Down
4 changes: 2 additions & 2 deletions readthedocs/audit/apps.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"""Audit module."""

import logging
import structlog

from django.apps import AppConfig

log = logging.getLogger(__name__)
log = structlog.get_logger(__name__)


class AuditConfig(AppConfig):
Expand Down
4 changes: 2 additions & 2 deletions readthedocs/builds/apps.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import logging
import structlog

from django.apps import AppConfig
from django.utils.translation import ugettext_lazy as _

log = logging.getLogger(__name__)
log = structlog.get_logger(__name__)


class Config(AppConfig):
Expand Down
9 changes: 5 additions & 4 deletions readthedocs/builds/automation_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
- action_arg: An additional argument to apply the action
"""

import logging
import structlog

from readthedocs.core.utils import trigger_build
from readthedocs.projects.constants import PRIVATE, PUBLIC

log = logging.getLogger(__name__)
log = structlog.get_logger(__name__)


def activate_version(version, match_result, action_arg, *args, **kwargs):
Expand Down Expand Up @@ -72,8 +72,9 @@ def delete_version(version, match_result, action_arg, *args, **kwargs):
"""Delete a version if isn't marked as the default version."""
if version.project.default_version == version.slug:
log.info(
"Skipping deleting default version. project=%s version=%s",
version.project.slug, version.slug,
"Skipping deleting default version.",
project_slug=version.project.slug,
version_slug=version.slug,
)
return
version.delete()
4 changes: 2 additions & 2 deletions readthedocs/builds/filters.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import logging
import structlog

from django.forms.widgets import HiddenInput
from django.utils.translation import ugettext_lazy as _
from django_filters import CharFilter, ChoiceFilter, FilterSet

from readthedocs.builds.constants import BUILD_STATE_FINISHED

log = logging.getLogger(__name__)
log = structlog.get_logger(__name__)


class BuildListFilter(FilterSet):
Expand Down
6 changes: 3 additions & 3 deletions readthedocs/builds/managers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Build and Version class model Managers."""

import logging
import structlog

from django.core.exceptions import ObjectDoesNotExist
from django.db import models
Expand All @@ -18,7 +18,7 @@
from readthedocs.builds.querysets import VersionQuerySet
from readthedocs.core.utils.extend import get_override_class

log = logging.getLogger(__name__)
log = structlog.get_logger(__name__)


__all__ = ['VersionManager']
Expand Down Expand Up @@ -78,7 +78,7 @@ def get_object_or_log(self, **kwargs):
try:
return super().get(**kwargs)
except ObjectDoesNotExist:
log.warning('Version not found for given kwargs. %s', kwargs)
log.warning('Version not found for given kwargs.', kwargs=kwargs)


class InternalVersionManager(VersionManager):
Expand Down
Loading