Spotify100ProtoSerializer implements Serializer { @Override public byte[] serializeMetrics(final Collection<com.spotify.ffwd.model.v2.Metric> metrics, WriteCache writeCache) throws Exception { final Spotify100.Batch.Builder batch = Spotify100.Batch.newBuilder(); for (com.spotify.ffwd.model.v2.Metric metric : metrics) { if (!writeCache.checkCacheOrSet(metric)) { batch.addMetric(convertToSpotify100Metric(metric)); } } return Snappy.compress(batch.build().toByteArray()); }  @Override byte[] serialize(final Metric metric); @Override byte[] serialize(Collection<Metric> metrics, WriteCache writeCache); @Override byte[] serializeMetrics(final Collection<com.spotify.ffwd.model.v2.Metric> metrics,
                                 WriteCache writeCache);  }
@Test public void testSerializedMetricBatchingV2() throws Exception { com.spotify.ffwd.model.v2.Metric metric1 = create(key,distributionDoubleValue,time,tags,resource); com.spotify.ffwd.model.v2.Metric metric2 = create(key,distributionBytStringValue,time,tags,resource); com.spotify.ffwd.model.v2.Metric metric3 = create(key,Value.DoubleValue.create(40),time,tags,resource); List<com.spotify.ffwd.model.v2.Metric> batchIn = ImmutableList.of(metric1,metric2,metric3); final byte [] bytes = serializer.serializeMetrics( batchIn, writeCache ); Spotify100.Batch spotify100BatchOut = deserialize(bytes); assertThat( batchIn.size(), is(spotify100BatchOut.getMetricList().size())); }
@Test public void testSerializedMetricWithDoubleValue() throws Exception { String key ="testV2"; Map<String,String> tags = ImmutableMap.of("a", "b"); Map<String,String> resource = ImmutableMap.of("res1","res2"); Value value = Value.DoubleValue.create(0.02); long time = System.currentTimeMillis(); com.spotify.ffwd.model.v2.Metric metric1 = create(key,value,time,tags,resource); final byte [] bytes = serializer.serializeMetrics(ImmutableList.of(metric1), writeCache ); Spotify100.Batch spotify100BatchOut = deserialize(bytes); assertThat( value.getValue(), is(spotify100BatchOut.getMetricList() .get(0).getDistributionTypeValue().getDoubleValue())); assertThat( key, is(spotify100BatchOut.getMetricList() .get(0).getKey())); assertThat( tags, is(spotify100BatchOut.getMetricList() .get(0).getTagsMap())); assertThat( resource, is(spotify100BatchOut.getMetricList() .get(0).getResourceMap())); assertThat( time, is(spotify100BatchOut.getMetricList() .get(0).getTime())); }
@Test public void testSerializedMetricWithDistributionValue() throws Exception { String key ="testV2"; Value value = Value.DistributionValue.create((ByteString.copyFromUtf8("ABCDEFG"))); com.spotify.ffwd.model.v2.Metric metric1 = create(key,value,time,tags,resource); final byte [] bytes = serializer.serializeMetrics(ImmutableList.of(metric1), writeCache ); Spotify100.Batch spotify100BatchOut = deserialize(bytes); assertThat( value.getValue(), is(spotify100BatchOut.getMetricList() .get(0).getDistributionTypeValue().getDistributionValue())); assertThat( key, is(spotify100BatchOut.getMetricList() .get(0).getKey())); assertThat( tags, is(spotify100BatchOut.getMetricList() .get(0).getTagsMap())); assertThat( resource, is(spotify100BatchOut.getMetricList() .get(0).getResourceMap())); assertThat( time, is(spotify100BatchOut.getMetricList() .get(0).getTime())); }
@Test public void testSerializeMetrics() throws Exception { metric1 = new Metric("", 0.0, Date.from(Instant.ofEpochSecond(1542812184)), ImmutableSet.of(), ImmutableMap.of("tag-a", "foo"), ImmutableMap.of("resource-a", "bar"), ""); final Metric metric2 = new Metric("", 1.0, Date.from(Instant.ofEpochSecond(1542812190)), ImmutableSet.of(), ImmutableMap.of("tag-a", "foo"), ImmutableMap.of("resource-a", "bar"), ""); final byte[] serialize = serializer.serialize(ImmutableList.of(metric1, metric2), writeCache); assertThat(serialize, is( new byte[]{93, -120, 10, 40, 16, -64, -37, -106, -74, -13, 44, 34, 12, 10, 5, 116, 97, 103, 45, 97, 18, 3, 102, 111, 111, 42, 17, 10, 10, 114, 101, 115, 111, 117, 114, 99, 101, 1, 19, 52, 98, 97, 114, 10, 49, 16, -80, -118, -105, -74, -13, 44, 25, 0, 5, 1, 4, -16, 63, -126, 51, 0}) ); }