Skip to content

Commit

Permalink
Json parser string length metrics are tagged by format (#2571)
Browse files Browse the repository at this point in the history
Json parser string length metrics are tagged by format
  • Loading branch information
carterkozak authored Mar 23, 2023
1 parent 5e2aee3 commit b448fcd
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 9 deletions.
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-2571.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: Json parser string length metrics are tagged by format
links:
- https://github.com/palantir/conjure-java-runtime/pull/2571
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ final class InstrumentedJsonFactory extends JsonFactory {
private final ParserInstrumentation instrumentation;

InstrumentedJsonFactory() {
this.instrumentation = new ParserInstrumentation();
this.instrumentation = new ParserInstrumentation(getFormatName());
}

private InstrumentedJsonFactory(JsonFactoryBuilder builder, ParserInstrumentation instrumentation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ final class InstrumentedSmileFactory extends SmileFactory {
private final ParserInstrumentation instrumentation;

InstrumentedSmileFactory() {
instrumentation = new ParserInstrumentation();
instrumentation = new ParserInstrumentation(getFormatName());
}

private InstrumentedSmileFactory(SmileFactoryBuilder builder) {
this(builder, new ParserInstrumentation());
this(builder, new ParserInstrumentation(FORMAT_NAME_SMILE));
}

private InstrumentedSmileFactory(SmileFactoryBuilder builder, ParserInstrumentation instrumentation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ final class ParserInstrumentation {

// Using the shared metric registry singleton to avoid API churn in methods that use this instrumentation.
@SuppressWarnings("deprecation")
ParserInstrumentation() {
ParserInstrumentation(String format) {
creationStackTrace = new SafeRuntimeException("Stream factory created here");
parsedStringLength = JsonParserMetrics.of(SharedTaggedMetricRegistries.getSingleton())
.stringLength();
.stringLength(format);
}

/** Returns the input, recording length of the value. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ namespaces:
metrics:
string.length:
type: histogram
tags:
- format
docs: Histogram describing the length of strings parsed from input.
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@

import com.codahale.metrics.Histogram;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.exc.InputCoercionException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.dataformat.smile.SmileFactory;
import com.palantir.logsafe.Preconditions;
import com.palantir.tritium.metrics.registry.SharedTaggedMetricRegistries;
import com.palantir.tritium.metrics.registry.TaggedMetricRegistry;
Expand Down Expand Up @@ -319,7 +321,7 @@ public void testStringMetrics_json() throws IOException {
TaggedMetricRegistry registry = SharedTaggedMetricRegistries.getSingleton();
// Unregister all metrics
registry.forEachMetric((name, _value) -> registry.remove(name));
Histogram stringLength = JsonParserMetrics.of(registry).stringLength();
Histogram stringLength = JsonParserMetrics.of(registry).stringLength(JsonFactory.FORMAT_NAME_JSON);
assertThat(stringLength.getSnapshot().size()).isZero();
// Length must exceed the minimum threshold for metrics (64 characters)
String expected = "Hello, World!".repeat(10);
Expand All @@ -334,7 +336,7 @@ public void testStringMetricsNotRecordedWhenValuesAreSmall_json() throws IOExcep
TaggedMetricRegistry registry = SharedTaggedMetricRegistries.getSingleton();
// Unregister all metrics
registry.forEachMetric((name, _value) -> registry.remove(name));
Histogram stringLength = JsonParserMetrics.of(registry).stringLength();
Histogram stringLength = JsonParserMetrics.of(registry).stringLength(JsonFactory.FORMAT_NAME_JSON);
assertThat(stringLength.getSnapshot().size()).isZero();
String expected = "Hello, World!";
String value = ObjectMappers.newServerJsonMapper().readValue("\"" + expected + "\"", String.class);
Expand All @@ -347,7 +349,7 @@ public void testStringMetrics_smile() throws IOException {
TaggedMetricRegistry registry = SharedTaggedMetricRegistries.getSingleton();
// Unregister all metrics
registry.forEachMetric((name, _value) -> registry.remove(name));
Histogram stringLength = JsonParserMetrics.of(registry).stringLength();
Histogram stringLength = JsonParserMetrics.of(registry).stringLength(SmileFactory.FORMAT_NAME_SMILE);
assertThat(stringLength.getSnapshot().size()).isZero();
// Length must exceed the minimum threshold for metrics (64 characters)
String expected = "Hello, World!".repeat(10);
Expand All @@ -363,7 +365,7 @@ public void testStringMetricsNotRecordedWhenValuesAreSmall_smile() throws IOExce
TaggedMetricRegistry registry = SharedTaggedMetricRegistries.getSingleton();
// Unregister all metrics
registry.forEachMetric((name, _value) -> registry.remove(name));
Histogram stringLength = JsonParserMetrics.of(registry).stringLength();
Histogram stringLength = JsonParserMetrics.of(registry).stringLength(SmileFactory.FORMAT_NAME_SMILE);
assertThat(stringLength.getSnapshot().size()).isZero();
String expected = "Hello, World!";
String value = ObjectMappers.newServerSmileMapper()
Expand Down

0 comments on commit b448fcd

Please sign in to comment.