Skip to content
This repository was archived by the owner on Sep 2, 2022. It is now read-only.

Commit 64c3248

Browse files
authored
Merge pull request #7 from apilytics/apilytics-version
Send Apilytics version info together with metrics, improve pipelines
2 parents aa0e9ff + 25dbc42 commit 64c3248

11 files changed

Lines changed: 118 additions & 28 deletions

File tree

.github/check_commits.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/sh
22

3-
head="$(git remote show origin | awk '/HEAD branch/ {print $NF}')"
3+
head="$GITHUB_BASE_REF"
44
current="$(git rev-parse HEAD)"
55

66
if ! git merge-base --is-ancestor "origin/${head}" "$current"; then

.github/workflows/cd.yml

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,26 @@ name: "CD"
33
on:
44
workflow_dispatch:
55
inputs:
6-
version:
7-
description: "Version of the release, e.g. 1.0.0"
6+
release-type:
7+
description: "Type of release?"
88
required: true
9+
type: choice
10+
options:
11+
- patch
12+
- minor
13+
- major
914

1015
jobs:
1116
release:
1217
name: "Release"
18+
if: ${{ github.ref == 'refs/heads/master' }}
1319
runs-on: ubuntu-latest
1420
steps:
1521
- name: "Checkout code"
1622
uses: actions/checkout@v2
1723
with:
1824
token: ${{ secrets.GH_PAT }}
1925

20-
- name: "Update the changelog"
21-
# Find the first line that starts with `###` or `## [<number>` from the CHANGELOG and insert the new version header before it.
22-
run: >
23-
current_date="$(date '+%Y-%m-%d')"
24-
&& sed -i "0,/^\(###\|## *\[[0-9]\).*/{s//## [${{ github.event.inputs.version }}] - ${current_date}\n\n&/}" CHANGELOG.md
25-
26-
- name: "Bump pyproject.toml to new version"
27-
run: sed -i 's/^version *=.*/version = "${{ github.event.inputs.version }}"/' pyproject.toml
28-
29-
- name: "Extract version's changelog for release notes and tag message"
30-
# 1. Find the lines between the first `## [<number>` and the second `## [<number>`.
31-
# 2. Remove all leading and trailing newlines from the output.
32-
run: sed '1,/^## *\[[0-9]/d;/^## *\[[0-9]/Q' CHANGELOG.md | sed -e :a -e '/./,$!d;/^\n*$/{$d;N;};/\n$/ba' > release_notes.txt
33-
3426
- name: "Set up Python"
3527
id: setup-python
3628
uses: actions/setup-python@v2
@@ -43,6 +35,15 @@ jobs:
4335
virtualenvs-create: true
4436
virtualenvs-in-project: true
4537

38+
- name: "Bump pyproject.toml to new version"
39+
id: bump
40+
run: |
41+
poetry version ${{ github.event.inputs.release-type }}
42+
echo "::set-output name=version::$(poetry version --short)"
43+
44+
- name: "Bump apilytics.__version__ to new version"
45+
run: sed -i 's/^__version__ *=.*/__version__ = "${{ steps.bump.outputs.version }}"/' apilytics/__init__.py
46+
4647
- name: "Build the wheel"
4748
run: poetry build
4849

@@ -51,17 +52,29 @@ jobs:
5152
env:
5253
POETRY_PYPI_TOKEN_PYPI: ${{ secrets.PYPI_TOKEN }}
5354

55+
- name: "Update the changelog"
56+
# Find the first line that starts with `###` or `## [<number>` from the CHANGELOG and insert the new version header before it.
57+
run: |
58+
current_date="$(date -u '+%Y-%m-%d')"
59+
sed -i "0,/^\(###\|## *\[[0-9]\).*/{s//## [${{ steps.bump.outputs.version }}] - ${current_date}\n\n&/}" CHANGELOG.md
60+
61+
- name: "Extract version's changelog for release notes"
62+
# 1. Find the lines between the first `## [<number>` and the second `## [<number>`.
63+
# 2. Remove all leading and trailing newlines from the output.
64+
run: sed '1,/^## *\[[0-9]/d;/^## *\[[0-9]/Q' CHANGELOG.md | sed -e :a -e '/./,$!d;/^\n*$/{$d;N;};/\n$/ba' > release_notes.txt
65+
5466
- name: "Commit and tag the changes"
5567
uses: EndBug/add-and-commit@8c12ff729a98cfbcd3fe38b49f55eceb98a5ec02 # v7.5.0
5668
with:
57-
add: '["pyproject.toml", "CHANGELOG.md"]'
58-
message: 'Release ${{ github.event.inputs.version }}'
59-
tag: 'v${{ github.event.inputs.version }} --annotate --file <(true)'
69+
add: '["pyproject.toml", "apilytics/__init__.py", "CHANGELOG.md"]'
70+
message: 'Release ${{ steps.bump.outputs.version }}'
71+
tag: 'v${{ steps.bump.outputs.version }} --annotate --file /dev/null'
6072
default_author: github_actions
73+
pathspec_error_handling: exitImmediately
6174

6275
- name: "Create a GitHub release"
6376
uses: softprops/action-gh-release@1e07f4398721186383de40550babbdf2b84acfc5 # v1
6477
with:
65-
tag_name: v${{ github.event.inputs.version }}
66-
name: v${{ github.event.inputs.version }}
78+
tag_name: v${{ steps.bump.outputs.version }}
79+
name: v${{ steps.bump.outputs.version }}
6780
body_path: release_notes.txt

.github/workflows/ci.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ jobs:
2626

2727
linters:
2828
name: "Linters"
29-
needs: commits
30-
if: ${{ !failure() }}
3129
runs-on: ubuntu-latest
3230
steps:
3331
- name: "Checkout code"
@@ -65,8 +63,6 @@ jobs:
6563
6664
build:
6765
name: "Build"
68-
needs: commits
69-
if: ${{ !failure() }}
7066
runs-on: ubuntu-latest
7167
steps:
7268
- name: "Checkout code"
@@ -93,7 +89,6 @@ jobs:
9389
tests:
9490
name: "Tests"
9591
needs: [linters, build]
96-
if: ${{ !failure() }}
9792
strategy:
9893
fail-fast: true
9994
matrix:

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Send Apilytics version info together with metrics.
13+
1014
## [1.0.1] - 2022-01-12
1115

16+
### Fixed
17+
18+
- Improve README documentation.
19+
1220
## [1.0.0] - 2022-01-10
1321

1422
### Added

apilytics/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__version__ = "1.0.1"

apilytics/core.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import concurrent.futures
22
import json
3+
import platform
34
import time
45
import types
56
import urllib.error
67
import urllib.request
78
from typing import ClassVar, Optional, Type
89

10+
import apilytics
11+
912
__all__ = ["ApilyticsSender"]
1013

1114

@@ -32,20 +35,42 @@ class ApilyticsSender:
3235

3336
_executor: ClassVar[concurrent.futures.Executor]
3437

35-
def __init__(self, *, api_key: str, path: str, method: str) -> None:
38+
_apilytics_version_template: ClassVar[
39+
str
40+
] = f"{{integration}}/{apilytics.__version__};python/{platform.python_version()}"
41+
42+
def __init__(
43+
self,
44+
*,
45+
api_key: str,
46+
path: str,
47+
method: str,
48+
apilytics_integration: Optional[str] = None,
49+
integrated_library: Optional[str] = None,
50+
) -> None:
3651
"""
3752
Initialize the context manager with info from the HTTP request object.
3853
3954
Args:
4055
api_key: The API key for your Apilytics origin.
4156
path: Path of the user's HTTP request, e.g. "/foo/bar/123".
4257
method: Method of the user's HTTP request, e.g. "GET".
58+
apilytics_integration: Name of the Apilytics integration that's calling this,
59+
e.g. "apilytics-python-django". No need to pass this when calling from user code.
60+
integrated_library: Name and version of the integration that this is used in,
61+
e.g. "django/3.2.1". No need to pass this when calling from user code.
4362
"""
4463
self._api_key = api_key
4564
self._path = path
4665
self._method = method
4766
self._status_code: Optional[int] = None
4867

68+
self._apilytics_version = self._apilytics_version_template.format(
69+
integration=apilytics_integration or "apilytics-python-core"
70+
)
71+
if integrated_library:
72+
self._apilytics_version += f";{integrated_library}"
73+
4974
def __enter__(self) -> "ApilyticsSender":
5075
"""Start the timer, measuring how long the ``with`` block takes to execute."""
5176
self._start_time_ns = time.perf_counter_ns()
@@ -85,6 +110,7 @@ def _send_metrics(self) -> None:
85110
headers={
86111
"Content-Type": "application/json",
87112
"X-API-Key": self._api_key,
113+
"Apilytics-Version": self._apilytics_version,
88114
},
89115
)
90116
data = {

apilytics/django.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from typing import Callable
22

3+
import django
34
import django.conf
45
import django.core.exceptions
56
import django.http
@@ -42,6 +43,8 @@ def __call__(self, request: django.http.HttpRequest) -> django.http.HttpResponse
4243
api_key=self.api_key,
4344
path=request.path,
4445
method=request.method or "",
46+
apilytics_integration="apilytics-python-django",
47+
integrated_library=f"django/{django.__version__}",
4548
) as sender:
4649
response = self.get_response(request)
4750
sender.set_response_info(status_code=response.status_code)

apilytics/fastapi.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ async def dispatch(
5252
api_key=self.api_key,
5353
path=request.url.path,
5454
method=request.method,
55+
apilytics_integration="apilytics-python-fastapi",
56+
integrated_library=f"fastapi/{fastapi.__version__}",
5557
) as sender:
5658
response = await call_next(request)
5759
sender.set_response_info(status_code=response.status_code)

tests/django/test_django.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import platform
12
import unittest.mock
23

34
import django.test
45

6+
import apilytics
57
import tests.conftest
68

79
client = django.test.client.Client()
@@ -30,6 +32,7 @@ def test_middleware_should_call_apilytics_api(
3032
# urllib calls `capitalize()` on the header keys.
3133
"Content-type": "application/json",
3234
"X-api-key": "dummy-key",
35+
"Apilytics-version": f"apilytics-python-django/{apilytics.__version__};python/{platform.python_version()};django/{django.__version__}",
3336
}
3437

3538
data = tests.conftest.decode_request_data(call_kwargs["data"])

tests/fastapi/test_fastapi.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import platform
12
import unittest.mock
23

34
import fastapi.middleware
@@ -31,6 +32,7 @@ def test_middleware_should_call_apilytics_api(
3132
# urllib calls `capitalize()` on the header keys.
3233
"Content-type": "application/json",
3334
"X-api-key": "dummy-key",
35+
"Apilytics-version": f"apilytics-python-fastapi/{apilytics.__version__};python/{platform.python_version()};fastapi/{fastapi.__version__}",
3436
}
3537

3638
data = tests.conftest.decode_request_data(call_kwargs["data"])

0 commit comments

Comments
 (0)