Compare commits
1 Commits
dev
...
andrej-pat
| Author | SHA1 | Date | |
|---|---|---|---|
| ff6032cb88 |
9
.gitignore
vendored
9
.gitignore
vendored
@@ -1,9 +0,0 @@
|
|||||||
__pycache__/
|
|
||||||
*.pyc
|
|
||||||
*.pyo
|
|
||||||
.venv/
|
|
||||||
venv/
|
|
||||||
.env
|
|
||||||
*.egg-info/
|
|
||||||
dist/
|
|
||||||
.DS_Store
|
|
||||||
25
Dockerfile
25
Dockerfile
@@ -1,25 +0,0 @@
|
|||||||
# ---- Build stage (install deps) ----
|
|
||||||
FROM python:3.12-slim AS build
|
|
||||||
WORKDIR /app
|
|
||||||
|
|
||||||
RUN pip install --upgrade pip
|
|
||||||
COPY requirements.txt .
|
|
||||||
RUN pip install --no-cache-dir --prefix=/install -r requirements.txt
|
|
||||||
|
|
||||||
# ---- Runtime stage ----
|
|
||||||
FROM python:3.12-slim
|
|
||||||
WORKDIR /app
|
|
||||||
|
|
||||||
# Non-root user
|
|
||||||
RUN addgroup --system appgroup && adduser --system --ingroup appgroup appuser
|
|
||||||
USER appuser
|
|
||||||
|
|
||||||
COPY --from=build /install /usr/local
|
|
||||||
COPY app/ ./app/
|
|
||||||
|
|
||||||
EXPOSE 8000
|
|
||||||
|
|
||||||
HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \
|
|
||||||
CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8000/health')" || exit 1
|
|
||||||
|
|
||||||
CMD ["opentelemetry-instrument", "uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
from app.main import app # noqa: F401 — re-export for uvicorn entrypoint
|
|
||||||
90
app/main.py
90
app/main.py
@@ -1,90 +0,0 @@
|
|||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
from threading import Lock
|
|
||||||
from typing import Optional
|
|
||||||
|
|
||||||
from fastapi import FastAPI, HTTPException
|
|
||||||
from prometheus_fastapi_instrumentator import Instrumentator
|
|
||||||
from pydantic import BaseModel
|
|
||||||
|
|
||||||
app = FastAPI(
|
|
||||||
title="sonar-test-pyt",
|
|
||||||
description="sonar-test-pyt",
|
|
||||||
version="0.1.0",
|
|
||||||
)
|
|
||||||
|
|
||||||
# Expose /metrics for Prometheus scraping
|
|
||||||
Instrumentator().instrument(app).expose(app)
|
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
# Domain model
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
class ItemRequest(BaseModel):
|
|
||||||
name: str
|
|
||||||
description: str = ""
|
|
||||||
|
|
||||||
|
|
||||||
class Item(BaseModel):
|
|
||||||
id: int
|
|
||||||
name: str
|
|
||||||
description: str
|
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
# In-memory store (thread-safe)
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
_store: dict[int, Item] = {}
|
|
||||||
_counter = 0
|
|
||||||
_lock = Lock()
|
|
||||||
|
|
||||||
|
|
||||||
def _next_id() -> int:
|
|
||||||
global _counter
|
|
||||||
with _lock:
|
|
||||||
_counter += 1
|
|
||||||
return _counter
|
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
# Routes
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
@app.get("/health", tags=["observability"])
|
|
||||||
def health() -> dict:
|
|
||||||
return {"status": "UP"}
|
|
||||||
|
|
||||||
|
|
||||||
@app.get("/api/items", response_model=list[Item], tags=["items"])
|
|
||||||
def list_items():
|
|
||||||
return list(_store.values())
|
|
||||||
|
|
||||||
|
|
||||||
@app.post("/api/items", response_model=Item, status_code=201, tags=["items"])
|
|
||||||
def create_item(body: ItemRequest):
|
|
||||||
item = Item(id=_next_id(), name=body.name, description=body.description)
|
|
||||||
_store[item.id] = item
|
|
||||||
return item
|
|
||||||
|
|
||||||
|
|
||||||
@app.get("/api/items/{item_id}", response_model=Item, tags=["items"])
|
|
||||||
def get_item(item_id: int):
|
|
||||||
item = _store.get(item_id)
|
|
||||||
if item is None:
|
|
||||||
raise HTTPException(status_code=404, detail="Item not found")
|
|
||||||
return item
|
|
||||||
|
|
||||||
|
|
||||||
@app.put("/api/items/{item_id}", response_model=Item, tags=["items"])
|
|
||||||
def update_item(item_id: int, body: ItemRequest):
|
|
||||||
item = _store.get(item_id)
|
|
||||||
if item is None:
|
|
||||||
raise HTTPException(status_code=404, detail="Item not found")
|
|
||||||
updated = Item(id=item_id, name=body.name, description=body.description)
|
|
||||||
_store[item_id] = updated
|
|
||||||
return updated
|
|
||||||
|
|
||||||
|
|
||||||
@app.delete("/api/items/{item_id}", tags=["items"])
|
|
||||||
def delete_item(item_id: int):
|
|
||||||
_store.pop(item_id, None)
|
|
||||||
return {"deleted": item_id}
|
|
||||||
@@ -1,52 +0,0 @@
|
|||||||
apiVersion: v1
|
|
||||||
kind: ConfigMap
|
|
||||||
metadata:
|
|
||||||
name: k6-test-sonar-test-pyt
|
|
||||||
namespace: dev
|
|
||||||
labels:
|
|
||||||
app: sonar-test-pyt
|
|
||||||
app.kubernetes.io/managed-by: backstage
|
|
||||||
app.kubernetes.io/component: load-testing
|
|
||||||
data:
|
|
||||||
K6_OUT: opentelemetry
|
|
||||||
K6_OTEL_GRPC_EXPORTER_INSECURE: 'true'
|
|
||||||
K6_OTEL_GRPC_EXPORTER_ENDPOINT: otel-collector.monitoring.svc.cluster.local:4317
|
|
||||||
K6_OTEL_METRIC_PREFIX: k6_
|
|
||||||
K6_OTEL_FLUSH_INTERVAL: '1000'
|
|
||||||
K6_OTEL_EXPORT_INTERVAL: '5000'
|
|
||||||
K6_OTEL_SERVICE_NAME: k6-sonar-test-pyt
|
|
||||||
load-test.js: "import http from 'k6/http';\nimport { check, sleep, group } from\
|
|
||||||
\ 'k6';\n\nconst vus = parseInt(__ENV.TEST_VUS || '10');\nconst duration = __ENV.TEST_DURATION\
|
|
||||||
\ || '30s';\nconst targetUrl = __ENV.TARGET_URL || 'http://sonar-test-pyt.dev.svc.cluster.local:8000';\n\
|
|
||||||
\nexport const options = {\n scenarios: {\n load_test: {\n executor:\
|
|
||||||
\ 'ramping-vus',\n startVUs: 0,\n stages: [\n { duration: '10s',\
|
|
||||||
\ target: vus },\n { duration: duration, target: vus },\n { duration:\
|
|
||||||
\ '5s', target: 0 },\n ],\n },\n },\n thresholds: {\n http_req_duration:\
|
|
||||||
\ ['p(95)<500'],\n http_req_failed: ['rate<0.01'],\n },\n};\n\nhttp.setResponseCallback(http.expectedStatuses({\
|
|
||||||
\ min: 200, max: 399 }));\n\nexport default function () {\n group('Observability\
|
|
||||||
\ API', () => {\n const res = http.get(`${targetUrl}/health`);\n check(res,\
|
|
||||||
\ {\n 'status is 200': (r) => r.status === 200,\n 'response time < 500ms':\
|
|
||||||
\ (r) => r.timings.duration < 500,\n 'body contains status': (r) => r.json().status\
|
|
||||||
\ === 'UP',\n });\n });\n\n sleep(0.5);\n\n group('Items API', () => {\n\
|
|
||||||
\ const listRes = http.get(`${targetUrl}/api/items`);\n check(listRes, {\n\
|
|
||||||
\ 'status is 200': (r) => r.status === 200,\n 'response time < 500ms':\
|
|
||||||
\ (r) => r.timings.duration < 500,\n 'body is an array': (r) => Array.isArray(r.json()),\n\
|
|
||||||
\ });\n\n const createRes = http.post(\n `${targetUrl}/api/items`,\n\
|
|
||||||
\ JSON.stringify({ name: 'Test Item', description: 'A test item description'\
|
|
||||||
\ }),\n { headers: { 'Content-Type': 'application/json' } }\n );\n \
|
|
||||||
\ check(createRes, {\n 'status is 201': (r) => r.status === 201,\n 'response\
|
|
||||||
\ time < 500ms': (r) => r.timings.duration < 500,\n 'body contains id': (r)\
|
|
||||||
\ => r.json().id !== undefined,\n });\n\n const itemId = createRes.json().id;\n\
|
|
||||||
\n const getRes = http.get(`${targetUrl}/api/items/${itemId}`);\n check(getRes,\
|
|
||||||
\ {\n 'status is 200': (r) => r.status === 200,\n 'response time < 500ms':\
|
|
||||||
\ (r) => r.timings.duration < 500,\n 'body contains correct id': (r) => r.json().id\
|
|
||||||
\ === itemId,\n });\n\n const updateRes = http.put(\n `${targetUrl}/api/items/${itemId}`,\n\
|
|
||||||
\ JSON.stringify({ name: 'Updated Item', description: 'Updated description'\
|
|
||||||
\ }),\n { headers: { 'Content-Type': 'application/json' } }\n );\n \
|
|
||||||
\ check(updateRes, {\n 'status is 200': (r) => r.status === 200,\n 'response\
|
|
||||||
\ time < 500ms': (r) => r.timings.duration < 500,\n 'body contains updated\
|
|
||||||
\ name': (r) => r.json().name === 'Updated Item',\n });\n\n const deleteRes\
|
|
||||||
\ = http.del(`${targetUrl}/api/items/${itemId}`);\n check(deleteRes, {\n \
|
|
||||||
\ 'status is 200': (r) => r.status === 200,\n 'response time < 500ms':\
|
|
||||||
\ (r) => r.timings.duration < 500,\n 'body contains deleted id': (r) => r.json().deleted\
|
|
||||||
\ === itemId,\n });\n });\n\n sleep(0.5);\n}"
|
|
||||||
@@ -1,88 +0,0 @@
|
|||||||
import http from 'k6/http';
|
|
||||||
import { check, sleep, group } from 'k6';
|
|
||||||
|
|
||||||
const vus = parseInt(__ENV.TEST_VUS || '10');
|
|
||||||
const duration = __ENV.TEST_DURATION || '30s';
|
|
||||||
const targetUrl = __ENV.TARGET_URL || 'http://sonar-test-pyt.dev.svc.cluster.local:8000';
|
|
||||||
|
|
||||||
export const options = {
|
|
||||||
scenarios: {
|
|
||||||
load_test: {
|
|
||||||
executor: 'ramping-vus',
|
|
||||||
startVUs: 0,
|
|
||||||
stages: [
|
|
||||||
{ duration: '10s', target: vus },
|
|
||||||
{ duration: duration, target: vus },
|
|
||||||
{ duration: '5s', target: 0 },
|
|
||||||
],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
thresholds: {
|
|
||||||
http_req_duration: ['p(95)<500'],
|
|
||||||
http_req_failed: ['rate<0.01'],
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
http.setResponseCallback(http.expectedStatuses({ min: 200, max: 399 }));
|
|
||||||
|
|
||||||
export default function () {
|
|
||||||
group('Observability API', () => {
|
|
||||||
const res = http.get(`${targetUrl}/health`);
|
|
||||||
check(res, {
|
|
||||||
'status is 200': (r) => r.status === 200,
|
|
||||||
'response time < 500ms': (r) => r.timings.duration < 500,
|
|
||||||
'body contains status': (r) => r.json().status === 'UP',
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
sleep(0.5);
|
|
||||||
|
|
||||||
group('Items API', () => {
|
|
||||||
const listRes = http.get(`${targetUrl}/api/items`);
|
|
||||||
check(listRes, {
|
|
||||||
'status is 200': (r) => r.status === 200,
|
|
||||||
'response time < 500ms': (r) => r.timings.duration < 500,
|
|
||||||
'body is an array': (r) => Array.isArray(r.json()),
|
|
||||||
});
|
|
||||||
|
|
||||||
const createRes = http.post(
|
|
||||||
`${targetUrl}/api/items`,
|
|
||||||
JSON.stringify({ name: 'Test Item', description: 'A test item description' }),
|
|
||||||
{ headers: { 'Content-Type': 'application/json' } }
|
|
||||||
);
|
|
||||||
check(createRes, {
|
|
||||||
'status is 201': (r) => r.status === 201,
|
|
||||||
'response time < 500ms': (r) => r.timings.duration < 500,
|
|
||||||
'body contains id': (r) => r.json().id !== undefined,
|
|
||||||
});
|
|
||||||
|
|
||||||
const itemId = createRes.json().id;
|
|
||||||
|
|
||||||
const getRes = http.get(`${targetUrl}/api/items/${itemId}`);
|
|
||||||
check(getRes, {
|
|
||||||
'status is 200': (r) => r.status === 200,
|
|
||||||
'response time < 500ms': (r) => r.timings.duration < 500,
|
|
||||||
'body contains correct id': (r) => r.json().id === itemId,
|
|
||||||
});
|
|
||||||
|
|
||||||
const updateRes = http.put(
|
|
||||||
`${targetUrl}/api/items/${itemId}`,
|
|
||||||
JSON.stringify({ name: 'Updated Item', description: 'Updated description' }),
|
|
||||||
{ headers: { 'Content-Type': 'application/json' } }
|
|
||||||
);
|
|
||||||
check(updateRes, {
|
|
||||||
'status is 200': (r) => r.status === 200,
|
|
||||||
'response time < 500ms': (r) => r.timings.duration < 500,
|
|
||||||
'body contains updated name': (r) => r.json().name === 'Updated Item',
|
|
||||||
});
|
|
||||||
|
|
||||||
const deleteRes = http.del(`${targetUrl}/api/items/${itemId}`);
|
|
||||||
check(deleteRes, {
|
|
||||||
'status is 200': (r) => r.status === 200,
|
|
||||||
'response time < 500ms': (r) => r.timings.duration < 500,
|
|
||||||
'body contains deleted id': (r) => r.json().deleted === itemId,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
sleep(0.5);
|
|
||||||
}
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
apiVersion: k6.io/v1alpha1
|
|
||||||
kind: TestRun
|
|
||||||
metadata:
|
|
||||||
name: k6-sonar-test-pyt
|
|
||||||
namespace: dev
|
|
||||||
labels:
|
|
||||||
app: sonar-test-pyt
|
|
||||||
backstage.io/component: sonar-test-pyt
|
|
||||||
app.kubernetes.io/managed-by: backstage
|
|
||||||
app.kubernetes.io/component: load-testing
|
|
||||||
spec:
|
|
||||||
parallelism: 1
|
|
||||||
script:
|
|
||||||
configMap:
|
|
||||||
name: k6-test-sonar-test-pyt
|
|
||||||
file: load-test.js
|
|
||||||
runner:
|
|
||||||
image: grafana/k6:latest
|
|
||||||
envFrom:
|
|
||||||
- configMapRef:
|
|
||||||
name: k6-test-sonar-test-pyt
|
|
||||||
env:
|
|
||||||
- name: K6_OTEL_SERVICE_NAME
|
|
||||||
value: k6-sonar-test-pyt
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
fastapi==0.111.0
|
|
||||||
uvicorn[standard]==0.29.0
|
|
||||||
prometheus-fastapi-instrumentator==7.0.0
|
|
||||||
opentelemetry-distro[otlp]>=0.51b0
|
|
||||||
opentelemetry-instrumentation-fastapi>=0.51b0
|
|
||||||
pytest>=8.0
|
|
||||||
httpx>=0.27
|
|
||||||
pytest-asyncio>=0.23
|
|
||||||
pytest-cov>=5.0
|
|
||||||
Reference in New Issue
Block a user