Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json parser string length metrics are tagged by format #2571

Merged
merged 2 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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