forked from spotify/ffwd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a protobuf serializer that is more space and cpu efficient.
- Loading branch information
Showing
6 changed files
with
182 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
core/src/main/java/com/spotify/ffwd/serializer/Spotify100ProtoSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/*- | ||
* -\-\- | ||
* FastForward Core | ||
* -- | ||
* Copyright (C) 2016 - 2018 Spotify AB | ||
* -- | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* -/-/- | ||
*/ | ||
|
||
package com.spotify.ffwd.serializer; | ||
|
||
import com.spotify.ffwd.model.Event; | ||
import com.spotify.ffwd.model.Metric; | ||
import com.spotify.proto.Spotify100; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
public class Spotify100ProtoSerializer implements Serializer { | ||
|
||
@Override | ||
public byte[] serialize(final Event event) throws Exception { | ||
log.debug("Serializing events is not supported"); | ||
return new byte[0]; | ||
} | ||
|
||
@Override | ||
public byte[] serialize(final Metric metric) throws Exception { | ||
return Spotify100.Metric.newBuilder() | ||
.setKey(metric.getKey()) | ||
.setTime(metric.getTime().getTime()) | ||
.setValue(metric.getValue()) | ||
.putAllTags(metric.getTags()) | ||
.putAllResource(metric.getResource()) | ||
.build() | ||
.toByteArray(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
syntax = "proto3"; | ||
|
||
package spotify.metric; | ||
option java_package = "com.spotify.proto"; | ||
|
||
option optimize_for = SPEED; | ||
|
||
message Metric { | ||
// key of metric. | ||
string key = 1; | ||
|
||
// time in ms when metric was generated. | ||
int64 time = 2; | ||
|
||
// value of metric. | ||
double value = 3; | ||
|
||
// tags associated to a metric, used to be referred to as attributes. | ||
map<string, string> tags = 4; | ||
|
||
// resource "tags" associated to metric. | ||
map<string, string> resource = 5; | ||
} | ||
|
||
message Message { | ||
Metric metric = 1; | ||
} |
71 changes: 71 additions & 0 deletions
71
core/src/test/java/com/spotify/ffwd/serializer/TestSpotify100ProtoSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/*- | ||
* -\-\- | ||
* FastForward Core | ||
* -- | ||
* Copyright (C) 2016 - 2018 Spotify AB | ||
* -- | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* -/-/- | ||
*/ | ||
|
||
package com.spotify.ffwd.serializer; | ||
|
||
import static org.hamcrest.CoreMatchers.any; | ||
import static org.junit.Assert.assertThat; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import com.google.common.collect.ImmutableSet; | ||
import com.spotify.ffwd.model.Metric; | ||
import java.time.Instant; | ||
import java.util.Date; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class TestSpotify100ProtoSerializer { | ||
private Spotify100ProtoSerializer spotify100ProtoSerializer; | ||
private Metric metric; | ||
|
||
@Before | ||
public void setup() { | ||
spotify100ProtoSerializer = new Spotify100ProtoSerializer(); | ||
} | ||
|
||
@Test | ||
public void testSerializeMetric() throws Exception { | ||
metric = new Metric("", | ||
0.0, | ||
Date.from(Instant.ofEpochSecond(1542812184)), | ||
ImmutableSet.of(), | ||
ImmutableMap.of("a", "b"), | ||
ImmutableMap.of(), | ||
""); | ||
|
||
final byte[] serialize = spotify100ProtoSerializer.serialize(metric); | ||
|
||
assertThat(serialize, any(byte[].class)); | ||
} | ||
|
||
@Test(expected = NullPointerException.class) | ||
public void testNullKeyException() throws Exception { | ||
metric = new Metric(null, | ||
0.0, | ||
Date.from(Instant.ofEpochSecond(1542812184)), | ||
ImmutableSet.of(), | ||
ImmutableMap.of("a", "b"), | ||
ImmutableMap.of(), | ||
""); | ||
|
||
spotify100ProtoSerializer.serialize(metric); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters