upload-pack: add tracing for fetches

Information on how users are accessing hosted repositories can be helpful to server operators. For example, being able to broadly differentiate between fetches and initial clones; the use of shallow repository features; or partial clone filters. a29263c (fetch-pack: add tracing for negotiation rounds, 2022-08-02) added some information on have counts to fetch-pack itself to help diagnose negotiation; but from a git-upload-pack (server) perspective, there's no means of accessing such information without using GIT_TRACE_PACKET to examine the protocol packets. Improve this by emitting a Trace2 JSON event from upload-pack with summary information on the contents of a fetch request. * haves, wants, and want-ref counts can help determine (broadly) between fetches and clones, and the use of single-branch, etc. * shallow clone depth, tip counts, and deepening options. * any partial clone filter type. Signed-off-by: Robert Coup <robert@coup.net.nz> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Robert Coup committed Oct 17, 2023 at 21:12 UTC b8f58c200cd3493ccf3c1669aa3c34927c884018
2 files changed +55 -11
t/t5500-fetch-pack.sh
+27 -11
@@ -132,13 +132,18 @@ test_expect_success 'single branch object count' '
132 '
133
134 test_expect_success 'single given branch clone' '
135 - git clone --single-branch --branch A "file://$(pwd)/." branch-a &&
136 - test_must_fail git --git-dir=branch-a/.git rev-parse origin/B
135 + GIT_TRACE2_EVENT="$(pwd)/branch-a/trace2_event" \
136 + git clone --single-branch --branch A "file://$(pwd)/." branch-a &&
137 + test_must_fail git --git-dir=branch-a/.git rev-parse origin/B &&
138 + grep \"fetch-info\".*\"haves\":0 branch-a/trace2_event &&
139 + grep \"fetch-info\".*\"wants\":1 branch-a/trace2_event
140 '
141
142 test_expect_success 'clone shallow depth 1' '
140 - git clone --no-single-branch --depth 1 "file://$(pwd)/." shallow0 &&
141 - test "$(git --git-dir=shallow0/.git rev-list --count HEAD)" = 1
143 + GIT_TRACE2_EVENT="$(pwd)/shallow0/trace2_event" \
144 + git clone --no-single-branch --depth 1 "file://$(pwd)/." shallow0 &&
145 + test "$(git --git-dir=shallow0/.git rev-list --count HEAD)" = 1 &&
146 + grep \"fetch-info\".*\"depth\":1 shallow0/trace2_event
147 '
148
149 test_expect_success 'clone shallow depth 1 with fsck' '
@@ -235,7 +240,10 @@ test_expect_success 'add two more (part 2)' '
240 test_expect_success 'deepening pull in shallow repo' '
241 (
242 cd shallow &&
238 - git pull --depth 4 .. B
243 + GIT_TRACE2_EVENT="$(pwd)/trace2_event" \
244 + git pull --depth 4 .. B &&
245 + grep \"fetch-info\".*\"depth\":4 trace2_event &&
246 + grep \"fetch-info\".*\"shallows\":2 trace2_event
247 )
248 '
249
@@ -306,9 +314,12 @@ test_expect_success 'fetch --depth --no-shallow' '
314 test_expect_success 'turn shallow to complete repository' '
315 (
316 cd shallow &&
309 - git fetch --unshallow &&
317 + GIT_TRACE2_EVENT="$(pwd)/trace2_event" \
318 + git fetch --unshallow &&
319 ! test -f .git/shallow &&
311 - git fsck --full
320 + git fsck --full &&
321 + grep \"fetch-info\".*\"shallows\":2 trace2_event &&
322 + grep \"fetch-info\".*\"depth\":2147483647 trace2_event
323 )
324 '
325
@@ -826,13 +837,15 @@ test_expect_success 'clone shallow since ...' '
837 '
838
839 test_expect_success 'fetch shallow since ...' '
829 - git -C shallow11 fetch --shallow-since "200000000 +0700" origin &&
840 + GIT_TRACE2_EVENT=$(pwd)/shallow11/trace2_event \
841 + git -C shallow11 fetch --shallow-since "200000000 +0700" origin &&
842 git -C shallow11 log --pretty=tformat:%s origin/main >actual &&
843 cat >expected <<-\EOF &&
844 three
845 two
846 EOF
835 - test_cmp expected actual
847 + test_cmp expected actual &&
848 + grep \"fetch-info\".*\"deepen-since\":true shallow11/trace2_event
849 '
850
851 test_expect_success 'clone shallow since selects no commits' '
@@ -987,13 +1000,16 @@ test_expect_success 'filtering by size' '
1000 test_config -C server uploadpack.allowfilter 1 &&
1001
1002 test_create_repo client &&
990 - git -C client fetch-pack --filter=blob:limit=0 ../server HEAD &&
1003 + GIT_TRACE2_EVENT=$(pwd)/client/trace2_event \
1004 + git -C client fetch-pack --filter=blob:limit=0 ../server HEAD &&
1005
1006 # Ensure that object is not inadvertently fetched
1007 commit=$(git -C server rev-parse HEAD) &&
1008 blob=$(git hash-object server/one.t) &&
1009 git -C client rev-list --objects --missing=allow-any "$commit" >oids &&
996 - ! grep "$blob" oids
1010 + ! grep "$blob" oids &&
1011 +
1012 + grep \"fetch-info\".*\"filter\":\"blob:limit\" client/trace2_event
1013 '
1014
1015 test_expect_success 'filtering by size has no effect if support for it is not advertised' '
upload-pack.c
+28
@@ -33,6 +33,7 @@
33 #include "commit-reach.h"
34 #include "shallow.h"
35 #include "write-or-die.h"
36 +#include "json-writer.h"
37
38 /* Remember to update object flag allocation in object.h */
39 #define THEY_HAVE (1u << 11)
@@ -1552,6 +1553,30 @@ static int parse_have(const char *line, struct oid_array *haves)
1553 return 0;
1554 }
1555
1556 +static void trace2_fetch_info(struct upload_pack_data *data)
1557 +{
1558 + struct json_writer jw = JSON_WRITER_INIT;
1559 +
1560 + jw_object_begin(&jw, 0);
1561 + jw_object_intmax(&jw, "haves", data->haves.nr);
1562 + jw_object_intmax(&jw, "wants", data->want_obj.nr);
1563 + jw_object_intmax(&jw, "want-refs", data->wanted_refs.nr);
1564 + jw_object_intmax(&jw, "depth", data->depth);
1565 + jw_object_intmax(&jw, "shallows", data->shallows.nr);
1566 + jw_object_bool(&jw, "deepen-since", data->deepen_since);
1567 + jw_object_intmax(&jw, "deepen-not", data->deepen_not.nr);
1568 + jw_object_bool(&jw, "deepen-relative", data->deepen_relative);
1569 + if (data->filter_options.choice)
1570 + jw_object_string(&jw, "filter", list_object_filter_config_name(data->filter_options.choice));
1571 + else
1572 + jw_object_null(&jw, "filter");
1573 + jw_end(&jw);
1574 +
1575 + trace2_data_json("upload-pack", the_repository, "fetch-info", &jw);
1576 +
1577 + jw_release(&jw);
1578 +}
1579 +
1580 static void process_args(struct packet_reader *request,
1581 struct upload_pack_data *data)
1582 {
@@ -1640,6 +1665,9 @@ static void process_args(struct packet_reader *request,
1665
1666 if (request->status != PACKET_READ_FLUSH)
1667 die(_("expected flush after fetch arguments"));
1668 +
1669 + if (trace2_is_enabled())
1670 + trace2_fetch_info(data);
1671 }
1672
1673 static int process_haves(struct upload_pack_data *data, struct oid_array *common)