Skip to content

Commit 28b04bc

Browse files
Release v0.1.4
Amp-Thread-ID: https://ampcode.com/threads/T-019e70e6-2c85-756c-8cfb-ac16708fb9ca Co-authored-by: Amp <amp@ampcode.com>
1 parent bd105c6 commit 28b04bc

6 files changed

Lines changed: 74 additions & 11 deletions

File tree

AGENTS.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ uv run python -m unittest discover -s tests
6464
```sh
6565
set -euo pipefail
6666

67-
VERSION=0.1.2
67+
VERSION=0.1.4
6868
BRANCH="release-v${VERSION}"
6969

7070
git fetch origin --tags --prune
@@ -116,7 +116,7 @@ rm -rf /tmp/src-py-lib-release-check
116116
```sh
117117
set -euo pipefail
118118

119-
VERSION=0.1.2
119+
VERSION=0.1.4
120120
BRANCH="release-v${VERSION}"
121121
GH_REPO="sourcegraph/src-py-lib"
122122

@@ -140,7 +140,7 @@ gh pr merge "${BRANCH}" --repo "${GH_REPO}" --squash --delete-branch
140140
```sh
141141
set -euo pipefail
142142

143-
VERSION=0.1.2
143+
VERSION=0.1.4
144144

145145
git fetch origin --tags --prune
146146
git switch main
@@ -154,7 +154,7 @@ git push origin "v${VERSION}"
154154
```sh
155155
set -euo pipefail
156156

157-
VERSION=0.1.2
157+
VERSION=0.1.4
158158
GH_REPO="sourcegraph/src-py-lib"
159159

160160
RUN_ID="$(

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ dev = [
1010

1111
[project]
1212
name = "src-py-lib"
13-
version = "0.1.3"
13+
version = "0.1.4"
1414
description = "Reusable libraries for Sourcegraph projects"
1515
readme = "README.md"
1616
requires-python = ">=3.11"

src/src_py_lib/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ def _script_name() -> str:
176176
"load_json_cache",
177177
"load_json_subset",
178178
"logging",
179+
"logging_context",
179180
"logging_settings_from_config",
180181
"log",
181182
"log_context",

src/src_py_lib/clients/sourcegraph.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import queue
99
import time
1010
from collections.abc import Iterable, Iterator, Mapping, Sequence
11+
from concurrent.futures import ThreadPoolExecutor, as_completed
1112
from dataclasses import dataclass, field
1213
from typing import Final, cast
1314
from urllib.parse import urlsplit
@@ -19,6 +20,7 @@
1920
from src_py_lib.utils.logging import (
2021
current_trace_context,
2122
new_trace_context,
23+
submit_with_log_context,
2224
trace_context_from_traceparent,
2325
traceparent_header,
2426
)
@@ -244,13 +246,27 @@ def stream_jaeger_trace_summaries(
244246
traces: Iterable[SourcegraphTrace] | None = None,
245247
*,
246248
retry_delays_seconds: Sequence[float] = JAEGER_TRACE_RETRY_DELAYS_SECONDS,
249+
parallelism: int = 8,
247250
) -> Iterator[SourcegraphJaegerTraceSummary]:
248251
"""Yield compact Jaeger/debug summaries for traced Sourcegraph requests."""
249-
for trace in self.drain_traces() if traces is None else traces:
250-
yield self.fetch_jaeger_trace_summary(
251-
trace,
252-
retry_delays_seconds=retry_delays_seconds,
253-
)
252+
if parallelism < 1:
253+
raise ValueError("parallelism must be at least 1")
254+
pending_traces = list(self.drain_traces() if traces is None else traces)
255+
with ThreadPoolExecutor(
256+
max_workers=parallelism,
257+
thread_name_prefix="SourcegraphJaegerTrace",
258+
) as executor:
259+
futures = [
260+
submit_with_log_context(
261+
executor,
262+
self.fetch_jaeger_trace_summary,
263+
trace,
264+
retry_delays_seconds=retry_delays_seconds,
265+
)
266+
for trace in pending_traces
267+
]
268+
for future in as_completed(futures):
269+
yield future.result()
254270

255271
def fetch_jaeger_trace_summary(
256272
self,

tests/test_logging_http_clients.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import logging
99
import subprocess
1010
import tempfile
11+
import threading
1112
import unittest
1213
from collections.abc import Mapping
1314
from contextlib import redirect_stderr, redirect_stdout
@@ -36,6 +37,7 @@
3637
from src_py_lib.clients.sourcegraph import (
3738
SourcegraphClient,
3839
SourcegraphClientConfig,
40+
SourcegraphTrace,
3941
decode_external_service_id,
4042
decode_repository_id,
4143
encode_repository_id,
@@ -1535,6 +1537,50 @@ def handler(request: httpx.Request) -> httpx.Response:
15351537
self.assertEqual(summaries[0].graphql_operations[0]["operation"], "Viewer")
15361538
self.assertEqual(summaries[0].errored_spans[0]["description"], "boom")
15371539

1540+
def test_sourcegraph_streams_jaeger_summaries_in_parallel(self) -> None:
1541+
trace_ids = ("1" * 32, "2" * 32, "3" * 32)
1542+
requested_trace_ids: list[str] = []
1543+
first_batch_barrier = threading.Barrier(2, timeout=1)
1544+
1545+
def handler(request: httpx.Request) -> httpx.Response:
1546+
trace_id = request.url.path.rsplit("/", 1)[-1]
1547+
requested_trace_ids.append(trace_id)
1548+
if trace_id in trace_ids[:2]:
1549+
first_batch_barrier.wait()
1550+
return httpx.Response(
1551+
200,
1552+
json={
1553+
"data": [
1554+
{
1555+
"spans": [
1556+
{
1557+
"operationName": f"trace {trace_id[0]}",
1558+
"duration": 1_000,
1559+
"tags": [],
1560+
}
1561+
]
1562+
}
1563+
]
1564+
},
1565+
)
1566+
1567+
client = SourcegraphClient(
1568+
"https://sourcegraph.example.com/",
1569+
"token",
1570+
http=HTTPClient(max_attempts=1, transport=httpx.MockTransport(handler)),
1571+
)
1572+
1573+
summaries = list(
1574+
client.stream_jaeger_trace_summaries(
1575+
[SourcegraphTrace(trace_id) for trace_id in trace_ids],
1576+
retry_delays_seconds=(0,),
1577+
parallelism=2,
1578+
)
1579+
)
1580+
1581+
self.assertCountEqual(requested_trace_ids, trace_ids)
1582+
self.assertCountEqual([summary.trace.trace_id for summary in summaries], trace_ids)
1583+
15381584
def test_graphql_client_paginates_cursor_results(self) -> None:
15391585
http = RecordingHTTP(
15401586
[

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)