Skip to content

Commit ad92280

Browse files
Add Streaming Token Tracking (#208)
Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
1 parent 6643093 commit ad92280

42 files changed

Lines changed: 6352 additions & 2335 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/copilot-instructions.md

Lines changed: 104 additions & 5 deletions
Large diffs are not rendered by default.

.github/json.instructions.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,12 @@ applyTo: '**/*.json'
1010
- If $schema is defined, ensure that all changes are validated.
1111
- Use two spaces for indentation. Do not use tabs.
1212
- Sort properties alphabetically.
13+
14+
## $schema URLs
15+
16+
- Always reference JSON schemas from `raw.githubusercontent.com`, never from `github.com/.../blob/...`. The `blob/` URL serves an HTML preview page, not the schema JSON, so editors and validators cannot fetch or validate against it.
17+
- Use the `refs/heads/<branch>` form (or a pinned tag/commit) so the URL is unambiguous and stable.
18+
- Example — Azure Monitor Workbook:
19+
-`https://raw.githubusercontent.com/Microsoft/Application-Insights-Workbooks/refs/heads/master/schema/workbook.json`
20+
-`https://github.com/Microsoft/Application-Insights-Workbooks/blob/master/schema/workbook.json`
21+
- When adding or updating a `$schema` value in any JSON file, convert any `github.com/<org>/<repo>/blob/<ref>/<path>` URL to `raw.githubusercontent.com/<org>/<repo>/refs/heads/<ref>/<path>` (or the appropriate `refs/tags/<tag>` form for pinned versions).

.github/python.instructions.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
description: 'Python coding conventions and guidelines'
3-
applyTo: '**/*.py'
3+
applyTo: '**/*.py,**/*.ipynb'
44
---
55

66
# Copilot Instructions (Python)
@@ -18,9 +18,10 @@ This ensures all code changes comply with the project's linting standards from t
1818
## Ruff Expectations
1919

2020
- Use explicit imports (avoid `from module import *`), especially in notebooks, to prevent `F403/F405`.
21-
- Keep lines within the configured length limit (see `pyproject.toml`), and wrap long strings or calls.
21+
- Keep lines within the configured length limit (**150 characters** per `pyproject.toml`), and wrap long strings or calls. **Inline comments on assignments are the most commonly missed line-length offender** — when `var = value # comment` would exceed 150 chars, move the comment to its own line(s) above the assignment.
2222
- Avoid f-strings without placeholders (e.g., `F541`).
2323
- **Ruff gap:** `PLC0415` (`import-outside-toplevel`) only flags imports inside functions and classes. It does **not** flag imports inside module-level `if` / `else` / `try` blocks. Ruff will not catch those, so the top-of-file import rule below must be enforced manually.
24+
- **Notebook cells follow the same rules** as `.py` files except for `F821` and `F401` (suppressed via per-file-ignores because notebook cells share kernel state). Line-length, quote style, and import placement still apply. After editing notebook cells, run `get_errors` and resolve any ruff `compileError` entries; ignore only the `"X" is not defined` cross-cell false positives.
2425

2526
## Goals
2627

.github/skills/sample-creator/SKILL.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ Assert that the deployment was successful by making calls to the deployed APIs.
189189

190190
### Cell 6: Python - Verification
191191

192+
Use `ApimRequests` and `ApimTesting` for structured test verification with verbose logging. If the sample also needs **traffic generation loops** (multi-caller simulation, load generation, etc.), add separate cells that use `requests.Session()` instead — see the "Testing and Traffic Generation" section in `copilot-instructions.md` for the session pattern.
193+
192194
```python
193195
from apimrequests import ApimRequests
194196
from apimtesting import ApimTesting
@@ -213,6 +215,8 @@ print_ok('All done!')
213215

214216
## Step 5: Create main.bicep
215217

218+
> **Always reuse infrastructure-provided resources.** The infrastructure deployment already creates an APIM service, a Log Analytics workspace, and an Application Insights component (the latter wired to APIM as the `apim-logger`). Sample `main.bicep` files must consume these via `existing` resource references — **do not** redeploy them. Only deploy a sample-local copy of one of these resources when the sample has a documented reason that satisfies one of the exceptions in `.github/copilot-instructions.md` ("Always reuse infrastructure-provided resources"). When wiring API-level diagnostics, omit the `apimLoggerName` parameter so APIs inherit the infrastructure logger automatically.
219+
216220
Use this template:
217221

218222
```bicep

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ labs-in-progress/
3030
.env*
3131
!.env-example
3232

33+
# Local-only ground-truth / sample-run artefacts (gitignored). Used by samples to
34+
# persist run data such as request counts so users can cross-check workbook output.
35+
*.local.json
36+
3337
# Coverage data and reports
3438
.coverage
3539
.coverage.*

AGENTS.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ This repository provides resources to deploy Azure API Management infrastructure
1010
- **Samples**: Real-world policy examples that can be deployed to any supported infrastructure
1111
- **Shared Resources**: Reusable Bicep modules, Python helpers, and policy templates
1212

13+
## Accessibility Baseline
14+
15+
All repository contributions should treat accessibility as a first-class quality requirement.
16+
17+
- Use WCAG 2.0 AA as the default baseline for user-facing outputs, including documentation, web content, workbook/dashboard visuals, and presentation materials.
18+
- Do not rely on color alone to convey meaning; pair color cues with labels, text, icons, or structure.
19+
- Preserve or improve contrast and readability when editing existing assets.
20+
1321
## Repository Structure
1422

1523
```text
@@ -120,6 +128,7 @@ Skills provide templates, patterns, and step-by-step workflows.
120128
- **Folder**: kebab-case (e.g., `oauth-validation`, `rate-limiting`)
121129
- **API prefix**: Short, unique, with trailing hyphen (e.g., `oauth-`, `rl-`)
122130
- **Policy files**: Descriptive, kebab-case (e.g., `token-validation.xml`)
131+
- **Workbook files**: Azure Monitor Workbook JSON files use the `<name>.workbook.json` suffix (e.g., `costing.workbook.json`). One workbook per file, stored in the sample folder, loaded into Bicep via `loadJsonContent('<name>.workbook.json')`, and pushed back to Azure via a sample-local `update-workbook.ps1`. See the *Azure Monitor Workbook File Convention* section in `.github/copilot-instructions.md`.
123132
- **Admin APIs**: Samples needing admin/operational endpoints use path `{api_prefix}admin` with `subscriptionRequired=True` and kebab-case operation paths (e.g., `/load-cache`)
124133

125134
### Infrastructure Constants

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ It's quick and easy to get started!
6767
| [AuthX][sample-authx] | Authentication and role-based authorization in a mock HR API. | All infrastructures |
6868
| [AuthX Pro][sample-authx-pro] | Authentication and role-based authorization in a mock product with multiple APIs and policy fragments. | All infrastructures |
6969
| [Azure Maps][sample-azure-maps] | Proxying calls to Azure Maps with APIM policies. | All infrastructures |
70-
| [Costing][sample-costing] | Track and allocate API costs per business unit using APIM subscriptions, Entra ID application tracking, and AI Gateway token/PTU tracking via Log Analytics and Cost Management. | All infrastructures |
70+
| [Costing][sample-costing] | Track and allocate API costs per business unit using APIM subscriptions, Entra ID application tracking, and AI Gateway token/PTU tracking including streaming (SSE) token usage, which is not simple to capture correctly in APIM. | All infrastructures |
7171
| [Dynamic CORS][sample-dynamic-cors] | Dynamic per-API CORS origin validation using custom policy fragments and a maintainable origin mapping. | All infrastructures |
7272
| [Egress Control][sample-egress-control] | Control APIM outbound internet traffic by routing it through a Network Virtual Appliance (NVA) in a hub/spoke topology. | appgw-apim, appgw-apim-pe |
7373
| [General][sample-general] | Basic demo of APIM sample setup and policy usage. | All infrastructures |

assets/APIM-Samples-Slide-Deck.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,8 +1117,8 @@ <h4>Azure Maps</h4>
11171117
<p>Proxying calls to Azure Maps through APIM policies.</p>
11181118
</div>
11191119
<div class="arch-card">
1120-
<h4>Costing &amp; Showback</h4>
1121-
<p>Track API costs per business unit via subscriptions, Entra ID apps &amp; AI Gateway tokens.</p>
1120+
<h4>Costing</h4>
1121+
<p>Track API costs per business unit via subscriptions, Entra ID apps, and AI Gateway tokens, including streaming token usage (not simple to capture correctly in APIM).</p>
11221122
</div>
11231123
<div class="arch-card">
11241124
<h4>Dynamic CORS</h4>

docs/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ <h3>Azure Maps</h3>
447447

448448
<a class="sample-card" href="https://github.com/Azure-Samples/Apim-Samples/tree/main/samples/costing" target="_blank" rel="noopener">
449449
<h3>Costing</h3>
450-
<p>Track and allocate API costs per business unit using subscriptions, Entra ID application tracking, and AI Gateway token/PTU tracking via Log Analytics and Cost Management.</p>
450+
<p>Track and allocate API costs per business unit using subscriptions, Entra ID application tracking, and AI Gateway token/PTU tracking including streaming (SSE) token usage, which is not simple to capture correctly in APIM.</p>
451451
<span class="infra-tag">All infrastructures</span>
452452
</a>
453453

0 commit comments

Comments
 (0)