Skip to content
This repository was archived by the owner on Dec 23, 2023. It is now read-only.

Commit e8f0cb3

Browse files
authored
Merge pull request #67 from dinooliva/factory-methods
Adds factory methods to TagKey and TagValue
2 parents 3c3abf9 + 424ee17 commit e8f0cb3

File tree

15 files changed

+93
-72
lines changed

15 files changed

+93
-72
lines changed

core/java/com/google/instrumentation/stats/RpcConstants.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
import com.google.instrumentation.stats.MeasurementDescriptor.BasicUnit;
1717
import com.google.instrumentation.stats.MeasurementDescriptor.MeasurementUnit;
18+
import com.google.instrumentation.stats.ViewDescriptor.DistributionViewDescriptor;
19+
import com.google.instrumentation.stats.ViewDescriptor.IntervalViewDescriptor;
1820

1921
import java.util.Arrays;
2022
import java.util.List;
@@ -26,9 +28,9 @@ public final class RpcConstants {
2628
/**
2729
* Census defined tag keys.
2830
*/
29-
public static final TagKey RPC_STATUS = new TagKey("/rpc/status");
30-
public static final TagKey RPC_CLIENT_METHOD = new TagKey("/rpc/client_method");
31-
public static final TagKey RPC_SERVER_METHOD = new TagKey("/rpc/server_method");
31+
public static final TagKey RPC_STATUS = TagKey.create("/rpc/status");
32+
public static final TagKey RPC_CLIENT_METHOD = TagKey.create("/rpc/client_method");
33+
public static final TagKey RPC_SERVER_METHOD = TagKey.create("/rpc/server_method");
3234

3335
// Constants used to define the following MeasurementDescriptors.
3436
private static final List<BasicUnit> bytes = Arrays.asList(new BasicUnit[] { BasicUnit.BYTES });

core/java/com/google/instrumentation/stats/Tag.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
public final class Tag {
2020
/**
21-
* Constructs a new {@link Tag} from the give key and value.
21+
* Constructs a new {@link Tag} from the given key and value.
2222
*/
2323
public static Tag create(TagKey key, TagValue value) {
2424
return new Tag(key, value);

core/java/com/google/instrumentation/stats/TagKey.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,16 @@
2121
public final class TagKey {
2222
public static final int MAX_LENGTH = StringUtil.MAX_LENGTH;
2323

24-
public TagKey(String key) {
25-
this.key = StringUtil.sanitize(key);
24+
/**
25+
* Constructs a new {@link TagKey} from the given string. The string will be sanitize such that:
26+
* <ol>
27+
* <li>length is restricted to {@link MAX_LENGTH}, strings longer than that will be truncated.
28+
* <li>characters are restricted to printable ascii characters, non-printable characters will be
29+
* replaced by an underscore '_'.
30+
* </ol>
31+
*/
32+
public static TagKey create(String key) {
33+
return new TagKey(key);
2634
}
2735

2836
@Override
@@ -41,4 +49,8 @@ public String toString() {
4149
}
4250

4351
private final String key;
52+
53+
private TagKey(String key) {
54+
this.key = StringUtil.sanitize(key);
55+
}
4456
}

core/java/com/google/instrumentation/stats/TagValue.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,16 @@
2121
public final class TagValue {
2222
public static final int MAX_LENGTH = StringUtil.MAX_LENGTH;
2323

24-
public TagValue(String value) {
25-
this.value = StringUtil.sanitize(value);
24+
/**
25+
* Constructs a new {@link TagValue} from the given string. The string will be sanitize such that:
26+
* <ol>
27+
* <li>length is restricted to {@link MAX_LENGTH}, strings longer than that will be truncated.
28+
* <li>characters are restricted to printable ascii characters, non-printable characters will be
29+
* replaced by an underscore '_'.
30+
* </ol>
31+
*/
32+
public static TagValue create(String value) {
33+
return new TagValue(value);
2634
}
2735

2836
@Override
@@ -41,4 +49,8 @@ public String toString() {
4149
}
4250

4351
private final String value;
52+
53+
private TagValue(String value) {
54+
this.value = StringUtil.sanitize(value);
55+
}
4456
}

core/javatests/com/google/instrumentation/stats/DistributionAggregationTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ public void testDistributionAggregationWithBuckets() {
6767
}
6868
}
6969

70-
private static final TagKey K1 = new TagKey("k1");
71-
private static final TagKey K2 = new TagKey("k2");
70+
private static final TagKey K1 = TagKey.create("k1");
71+
private static final TagKey K2 = TagKey.create("k2");
7272

73-
private static final TagValue V1 = new TagValue("v1");
74-
private static final TagValue V2 = new TagValue("v2");
73+
private static final TagValue V1 = TagValue.create("v1");
74+
private static final TagValue V2 = TagValue.create("v2");
7575

7676
private static final List<Tag> TAGS = Arrays.asList(Tag.create(K1, V1), Tag.create(K2, V2));
7777
}

core/javatests/com/google/instrumentation/stats/IntervalAggregationTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ public void testInterval() {
5555
assertThat(interval.getSum()).isWithin(0.00000001).of(1000.0);
5656
}
5757

58-
private static final TagKey K1 = new TagKey("k1");
59-
private static final TagKey K2 = new TagKey("k2");
58+
private static final TagKey K1 = TagKey.create("k1");
59+
private static final TagKey K2 = TagKey.create("k2");
6060

61-
private static final TagValue V1 = new TagValue("v1");
62-
private static final TagValue V2 = new TagValue("v2");
61+
private static final TagValue V1 = TagValue.create("v1");
62+
private static final TagValue V2 = TagValue.create("v2");
6363

6464
private static final List<Tag> TAGS = Arrays.asList(Tag.create(K1, V1), Tag.create(K2, V2));
6565
}

core/javatests/com/google/instrumentation/stats/MeasurementDescriptorTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ public void testComponents() {
5050
"Foo",
5151
"The description of Foo",
5252
MeasurementUnit.create(
53-
6,
54-
Arrays.asList(new BasicUnit[] { BasicUnit.BITS }),
55-
Arrays.asList(new BasicUnit[] { BasicUnit.SECONDS })));
53+
6, Arrays.asList(BasicUnit.BITS), Arrays.asList(BasicUnit.SECONDS)));
5654
assertThat(measurement.getName()).isEqualTo("Foo");
5755
assertThat(measurement.getDescription()).isEqualTo("The description of Foo");
5856
assertThat(measurement.getUnit().getPower10()).isEqualTo(6);
@@ -66,6 +64,6 @@ private static final MeasurementDescriptor makeSimpleDescriptor(String name) {
6664
return MeasurementDescriptor.create(
6765
name,
6866
name + " description",
69-
MeasurementUnit.create(1, Arrays.asList(new BasicUnit[] { BasicUnit.SCALAR })));
67+
MeasurementUnit.create(1, Arrays.asList(BasicUnit.SCALAR)));
7068
}
7169
}

core/javatests/com/google/instrumentation/stats/MeasurementMapTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public void testSize() {
9393
}
9494

9595
private static final MeasurementUnit simpleMeasurementUnit =
96-
MeasurementUnit.create(1, Arrays.asList(new BasicUnit[] { BasicUnit.SCALAR }));
96+
MeasurementUnit.create(1, Arrays.asList(BasicUnit.SCALAR));
9797
private static final MeasurementDescriptor M1 = makeSimpleMeasurement("m1");
9898
private static final MeasurementDescriptor M2 = makeSimpleMeasurement("m2");
9999
private static final MeasurementDescriptor M3 = makeSimpleMeasurement("m3");

core/javatests/com/google/instrumentation/stats/StatsContextTest.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,22 @@ public class StatsContextTest {
3333
RpcConstants.RPC_SERVER_RESPONSE_BYTES, RpcConstants.RPC_SERVER_SERVER_LATENCY
3434
};
3535

36-
private static final TagKey K_EMPTY = new TagKey("");
37-
private static final TagKey K1 = new TagKey("k1");
38-
private static final TagKey K2 = new TagKey("k2");
39-
private static final TagKey K3 = new TagKey("k3");
40-
private static final TagKey K4 = new TagKey("k4");
41-
private static final TagKey K10 = new TagKey("k10");
42-
43-
private static final TagValue V_EMPTY = new TagValue("");
44-
private static final TagValue V1 = new TagValue("v1");
45-
private static final TagValue V2 = new TagValue("v2");
46-
private static final TagValue V3 = new TagValue("v3");
47-
private static final TagValue V4 = new TagValue("v4");
48-
private static final TagValue V10 = new TagValue("v10");
49-
private static final TagValue V20 = new TagValue("v20");
50-
private static final TagValue V30 = new TagValue("v30");
51-
private static final TagValue V100 = new TagValue("v100");
36+
private static final TagKey K_EMPTY = TagKey.create("");
37+
private static final TagKey K1 = TagKey.create("k1");
38+
private static final TagKey K2 = TagKey.create("k2");
39+
private static final TagKey K3 = TagKey.create("k3");
40+
private static final TagKey K4 = TagKey.create("k4");
41+
private static final TagKey K10 = TagKey.create("k10");
42+
43+
private static final TagValue V_EMPTY = TagValue.create("");
44+
private static final TagValue V1 = TagValue.create("v1");
45+
private static final TagValue V2 = TagValue.create("v2");
46+
private static final TagValue V3 = TagValue.create("v3");
47+
private static final TagValue V4 = TagValue.create("v4");
48+
private static final TagValue V10 = TagValue.create("v10");
49+
private static final TagValue V20 = TagValue.create("v20");
50+
private static final TagValue V30 = TagValue.create("v30");
51+
private static final TagValue V100 = TagValue.create("v100");
5252

5353
@Test
5454
public void testWith() {

core/javatests/com/google/instrumentation/stats/TagKeyTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,28 @@ public void testKeyMaxLength() {
3232
char[] truncKey = new char[TagKey.MAX_LENGTH + 10];
3333
Arrays.fill(key, 'k');
3434
Arrays.fill(truncKey, 'k');
35-
assertThat(new TagKey(new String(key)).toString())
36-
.isEqualTo(new TagKey(new String(truncKey)).toString());
35+
assertThat(TagKey.create(new String(key)).toString())
36+
.isEqualTo(TagKey.create(new String(truncKey)).toString());
3737
}
3838

3939
@Test
4040
public void testKeyBadChar() {
4141
String key = "\2ab\3cd";
42-
assertThat(new TagKey(key).toString())
42+
assertThat(TagKey.create(key).toString())
4343
.isEqualTo(StringUtil.UNPRINTABLE_CHAR_SUBSTITUTE + "ab"
4444
+ StringUtil.UNPRINTABLE_CHAR_SUBSTITUTE + "cd");
4545
}
4646

4747
@Test
4848
public void testEquals() {
4949
new EqualsTester()
50-
.addEqualityGroup(new TagKey("foo"), new TagKey("foo"))
51-
.addEqualityGroup(new TagKey("bar"))
50+
.addEqualityGroup(TagKey.create("foo"), TagKey.create("foo"))
51+
.addEqualityGroup(TagKey.create("bar"))
5252
.testEquals();
5353
}
5454

5555
@Test
5656
public void testToString() {
57-
assertThat(new TagKey("foo").toString()).isEqualTo("foo");
57+
assertThat(TagKey.create("foo").toString()).isEqualTo("foo");
5858
}
5959
}

0 commit comments

Comments
 (0)