-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Add support for StringSet metric in Java SDK to track set of unique s… #31789
Changes from 5 commits
b733f96
44a3901
e16da38
2dd784b
67ef5bd
0ca3bff
ef55463
e43e691
6f15be1
adcf3df
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,8 +19,12 @@ | |
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.HashSet; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import org.apache.beam.sdk.coders.Coder; | ||
import org.apache.beam.sdk.coders.DoubleCoder; | ||
import org.apache.beam.sdk.coders.StringUtf8Coder; | ||
import org.apache.beam.sdk.coders.VarLongCoder; | ||
import org.apache.beam.sdk.util.ByteStringOutputStream; | ||
import org.apache.beam.vendor.grpc.v1p60p1.com.google.protobuf.ByteString; | ||
|
@@ -30,6 +34,7 @@ | |
public class MonitoringInfoEncodings { | ||
private static final Coder<Long> VARINT_CODER = VarLongCoder.of(); | ||
private static final Coder<Double> DOUBLE_CODER = DoubleCoder.of(); | ||
private static final Coder<String> STRING_CODER = StringUtf8Coder.of(); | ||
|
||
/** Encodes to {@link MonitoringInfoConstants.TypeUrns#DISTRIBUTION_INT64_TYPE}. */ | ||
public static ByteString encodeInt64Distribution(DistributionData data) { | ||
|
@@ -98,6 +103,36 @@ public static GaugeData decodeInt64Gauge(ByteString payload) { | |
} | ||
} | ||
|
||
/** Encodes to {@link MonitoringInfoConstants.TypeUrns#SET_STRING_TYPE}. */ | ||
public static ByteString encodeStringSet(StringSetData data) { | ||
try (ByteStringOutputStream output = new ByteStringOutputStream()) { | ||
// encode the length of set | ||
STRING_CODER.encode(String.valueOf(data.stringSet().size()), output); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is nice!!! Thank you. (I will work on this and other review comments tomorrow. Didn't realize the difference between "Add single comment" and "start review" in previous comment) |
||
// encode all elements | ||
for (String s : data.stringSet()) { | ||
STRING_CODER.encode(s, output); | ||
} | ||
return output.toByteString(); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
/** Decodes from {@link MonitoringInfoConstants.TypeUrns#SET_STRING_TYPE}. */ | ||
public static StringSetData decodeStringSet(ByteString payload) { | ||
Set<String> elements = new HashSet<>(); | ||
try (InputStream input = payload.newInput()) { | ||
int size = Integer.parseInt(Objects.requireNonNull(STRING_CODER.decode(input))); | ||
while (size > 0) { | ||
elements.add(Objects.requireNonNull(STRING_CODER.decode(input))); | ||
size--; | ||
} | ||
return StringSetData.create(elements); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
/** Encodes to {@link MonitoringInfoConstants.TypeUrns#SUM_INT64_TYPE}. */ | ||
public static ByteString encodeInt64Counter(long value) { | ||
ByteStringOutputStream output = new ByteStringOutputStream(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: space after period.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.