Skip to content

Commit 7a9948c

Browse files
authored
Merge pull request #6902 from psiinon/postman/stats
postman: add statistics
2 parents ab4c317 + d8d1366 commit 7a9948c

File tree

5 files changed

+37
-2
lines changed

5 files changed

+37
-2
lines changed

addOns/callhome/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ All notable changes to this add-on will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
55

66
## Unreleased
7-
7+
### Added
8+
- Postman stats to telemetry.
89

910
## [0.17.0] - 2025-11-04
1011
### Added

addOns/callhome/src/main/java/org/zaproxy/addon/callhome/ExtensionCallHome.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ public boolean test(Entry<String, Long> t) {
321321
|| key.startsWith("stats.network.")
322322
|| key.startsWith("stats.oast.")
323323
|| key.startsWith("stats.openapi.")
324+
|| key.startsWith("stats.postman.")
324325
|| key.startsWith("stats.quickstart.")
325326
|| key.startsWith("stats.reports.")
326327
|| key.startsWith("stats.script.")

addOns/postman/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ All notable changes to this add-on will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
55

66
## Unreleased
7-
7+
### Added
8+
- Statistics.
89

910
## [0.7.0] - 2025-09-02
1011
### Changed

addOns/postman/src/main/java/org/zaproxy/addon/postman/PostmanParser.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import org.zaproxy.addon.postman.models.PostmanCollection;
5555
import org.zaproxy.addon.postman.models.Request;
5656
import org.zaproxy.addon.postman.models.Request.Url;
57+
import org.zaproxy.zap.utils.Stats;
5758

5859
public class PostmanParser {
5960

@@ -88,6 +89,7 @@ public boolean importFromFile(
8889
}
8990

9091
String collectionJson = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
92+
Stats.incCounter("stats.postman.import.file");
9193
return importCollection(collectionJson, variables, initViaUi);
9294
}
9395

@@ -106,6 +108,7 @@ public boolean importFromUrl(final String url, final String variables, final boo
106108
}
107109

108110
String collectionJson = requestor.getResponseBody(uri);
111+
Stats.incCounter("stats.postman.import.url");
109112
return importCollection(collectionJson, variables, initViaUi);
110113
}
111114

@@ -121,6 +124,7 @@ List<HttpMessage> getHttpMessages(
121124
postmanCollection.getItem(), httpMessages, errors, postmanCollection.getVariable());
122125
if (httpMessages.isEmpty()) {
123126
errors.add(Constant.messages.getString("postman.import.error.noItem"));
127+
Stats.incCounter("stats.postman.error.nomsgs");
124128
}
125129
return httpMessages;
126130
}
@@ -135,6 +139,10 @@ public boolean importCollection(
135139

136140
outputErrors(errors, initViaUi);
137141

142+
if (!errors.isEmpty()) {
143+
Stats.incCounter("stats.postman.errors", errors.size());
144+
}
145+
138146
return errors.isEmpty();
139147
}
140148

@@ -273,6 +281,7 @@ static HttpMessage extractHttpMessage(
273281
IMPORT_FORMAT_ERROR,
274282
item.getName(),
275283
Constant.messages.getString("postman.import.errorMsg.reqNotPresent")));
284+
Stats.incCounter("stats.postman.error.noreq");
276285
return null;
277286
}
278287

@@ -283,6 +292,7 @@ static HttpMessage extractHttpMessage(
283292
IMPORT_FORMAT_ERROR,
284293
item.getName(),
285294
Constant.messages.getString("postman.import.errorMsg.urlNotPresent")));
295+
Stats.incCounter("stats.postman.error.nourl");
286296
return null;
287297
}
288298

@@ -306,6 +316,7 @@ static HttpMessage extractHttpMessage(
306316
IMPORT_FORMAT_ERROR,
307317
item.getName(),
308318
Constant.messages.getString("postman.import.errorMsg.rawInvalid")));
319+
Stats.incCounter("stats.postman.error.exception");
309320
return null;
310321
}
311322

@@ -322,6 +333,7 @@ static HttpMessage extractHttpMessage(
322333
}
323334
}
324335
}
336+
Stats.incCounter("stats.postman.messages");
325337

326338
Body body = request.getBody();
327339
if (body == null || body.isDisabled()) {
@@ -423,6 +435,7 @@ static HttpMessage extractHttpMessage(
423435
IMPORT_WARNING,
424436
item.getName(),
425437
e1.getClass().getName() + ": " + e1.getMessage()));
438+
Stats.incCounter("stats.postman.error.badfile");
426439
}
427440
} else if (mode.equals(Body.GRAPHQL)) {
428441
if (body.getGraphQl() == null) {
@@ -503,6 +516,7 @@ private static String generateMultiPartBody(
503516
IMPORT_WARNING,
504517
itemName,
505518
"Could not read file: " + e.getMessage()));
519+
Stats.incCounter("stats.postman.error.badfile");
506520
return "";
507521
}
508522
} else {
@@ -528,6 +542,7 @@ private static String getFileContentType(String value, List<String> errors, Stri
528542
+ e.getClass().getName()
529543
+ ": "
530544
+ e.getMessage()));
545+
Stats.incCounter("stats.postman.error.badfiletype");
531546
return "";
532547
}
533548
}

addOns/postman/src/test/java/org/zaproxy/addon/postman/PostmanParserUnitTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,21 @@
5454
import org.zaproxy.addon.postman.models.KeyValueData;
5555
import org.zaproxy.addon.postman.models.PostmanCollection;
5656
import org.zaproxy.addon.postman.models.Request;
57+
import org.zaproxy.zap.extension.stats.InMemoryStats;
5758
import org.zaproxy.zap.testutils.TestUtils;
59+
import org.zaproxy.zap.utils.Stats;
5860

5961
class PostmanParserUnitTest extends TestUtils {
6062

63+
private InMemoryStats stats;
64+
6165
@BeforeEach
6266
void setup() throws Exception {
6367
setUpZap();
6468
startServer();
6569
mockMessages(new ExtensionPostman());
70+
stats = new InMemoryStats();
71+
Stats.addListener(stats);
6672
}
6773

6874
@AfterEach
@@ -287,6 +293,9 @@ void shouldExtractHttpMessagesFromItems(List<AbstractItem> items, int numberOfit
287293
PostmanParser.extractHttpMessages(items, httpMessages);
288294

289295
assertEquals(numberOfitems, httpMessages.size());
296+
assertEquals(
297+
stats.getStats("").get("stats.postman.messages"),
298+
httpMessages.size() == 0 ? null : (long) httpMessages.size());
290299
}
291300

292301
// The 'Content-Type' header gets set according to the mode of the request body, but if it's
@@ -351,6 +360,13 @@ void shouldHandleRequestBodyModes(Body body, String contentType, String stringBo
351360
assertEquals(
352361
stringBody,
353362
new String(httpMessage.getRequestBody().getContent(), StandardCharsets.UTF_8));
363+
assertEquals(stats.getStats("").get("stats.postman.messages"), 1);
364+
}
365+
366+
private long getStatsErrorCount() {
367+
return stats.getStats("stats.postman.error.").values().stream()
368+
.mapToLong(Long::longValue)
369+
.sum();
354370
}
355371

356372
@ParameterizedTest
@@ -366,6 +382,7 @@ void shouldGiveErrors(String collectionJson, List<String> expectedErrors)
366382
for (int i = 0; i < errors.size(); i++) {
367383
assertEquals(expectedErrors.get(i), errors.get(i));
368384
}
385+
assertEquals(getStatsErrorCount(), errors.size());
369386
}
370387

371388
@MethodSource("variablesTestData")

0 commit comments

Comments
 (0)