-
-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ Refactor MetricNameAndLabels and MetricLabel to improve metrics co…
…llection performance
- Loading branch information
Showing
7 changed files
with
38 additions
and
59 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,39 +20,34 @@ | |
|
||
package org.restheart.metrics; | ||
|
||
import java.util.ArrayDeque; | ||
import java.util.Deque; | ||
import org.bson.BsonDocument; | ||
import org.bson.json.JsonWriterSettings; | ||
import org.restheart.utils.BsonUtils; | ||
import static org.restheart.utils.BsonUtils.document; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* record for metric labels that can be serialized/deserialized to/from string | ||
* | ||
* @author Andrea Di Cesare {@literal <[email protected]>} | ||
*/ | ||
public record MetricLabel(String name, String value) { | ||
private static JsonWriterSettings jsonWriterSettings = JsonWriterSettings.builder().indent(false).build(); | ||
public static String SEPARATOR = "="; | ||
|
||
public BsonDocument bson() { | ||
return document().put("n", name).put("v", value).get(); | ||
public MetricLabel(String name, String value) { | ||
this.name = name.replaceAll("=", "_").replaceAll("\\.", "_"); | ||
this.value = value.replaceAll("\\.", "_"); | ||
} | ||
|
||
public static MetricLabel fromJson(BsonDocument raw) { | ||
return new MetricLabel(raw.getString("n").getValue(), raw.getString("v").getValue()); | ||
} | ||
|
||
public String toString() { | ||
return BsonUtils.minify(bson().toJson(jsonWriterSettings)); | ||
return name.concat(SEPARATOR).concat(value); | ||
} | ||
|
||
public static MetricLabel from(String raw) { | ||
return fromJson(BsonUtils.parse(raw).asDocument()); | ||
var sepIdx = raw.indexOf(SEPARATOR); | ||
return new MetricLabel(raw.substring(0, sepIdx), raw.substring(sepIdx+1)); | ||
} | ||
|
||
public static Deque<MetricLabel> from(MetricLabel... labels) { | ||
var ret = new ArrayDeque<MetricLabel>(); | ||
public static List<MetricLabel> collect(MetricLabel... labels) { | ||
var ret = new ArrayList<MetricLabel>(); | ||
for (var label: labels) { | ||
ret.add(label); | ||
} | ||
|
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 |
---|---|---|
|
@@ -19,51 +19,40 @@ | |
*/ | ||
package org.restheart.metrics; | ||
|
||
import org.bson.BsonDocument; | ||
import org.bson.json.JsonWriterSettings; | ||
import org.restheart.utils.BsonUtils; | ||
|
||
import static org.restheart.utils.BsonUtils.document; | ||
|
||
import java.util.ArrayDeque; | ||
import java.util.Deque; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import static org.restheart.utils.BsonUtils.array; | ||
|
||
/** | ||
* record for metric name and labels that can be serialized/deserialized to/from string | ||
* | ||
* @author Andrea Di Cesare {@literal <[email protected]>} | ||
*/ | ||
public record MetricNameAndLabels(String name, Deque<MetricLabel> lables) { | ||
private static JsonWriterSettings jsonWriterSettings = JsonWriterSettings.builder().indent(false).build(); | ||
|
||
public BsonDocument bson() { | ||
var _labels = array(); | ||
var ret = document().put("l", _labels).put("n", name()); | ||
public record MetricNameAndLabels(String name, List<MetricLabel> lables) { | ||
public static String SEPARATOR = "."; | ||
private static String SEPARATOR_REGEX = "\\."; | ||
|
||
lables().stream().map(MetricLabel::bson).forEachOrdered(_labels::add); | ||
|
||
return ret.get(); | ||
public MetricNameAndLabels(String name, List<MetricLabel> lables) { | ||
this.name = name.replaceAll("SEPARATOR_REGEX", "_"); | ||
this.lables = lables; | ||
} | ||
|
||
public static MetricNameAndLabels fromJson(BsonDocument raw) { | ||
var _labels = raw.getArray("l").stream() | ||
.map(v -> v.asDocument()) | ||
.map(d -> MetricLabel.fromJson(d)) | ||
.collect(Collectors.toList()); | ||
public static MetricNameAndLabels from(String raw) { | ||
var name = raw.substring(0, raw.indexOf(".")); | ||
|
||
var labels = new ArrayDeque<MetricLabel>(_labels); | ||
var labels = Arrays.stream(raw.split(SEPARATOR_REGEX)) | ||
.skip(1) | ||
.map(l -> MetricLabel.from(l)) | ||
.collect(Collectors.toList()); | ||
|
||
return new MetricNameAndLabels(raw.getString("n").getValue(), labels); | ||
return new MetricNameAndLabels(name, labels); | ||
} | ||
|
||
public String toString() { | ||
return BsonUtils.minify(bson().toJson(jsonWriterSettings)); | ||
} | ||
var sb = new StringBuilder(); | ||
sb.append(name).append(SEPARATOR); | ||
|
||
public static MetricNameAndLabels fromString(String raw) { | ||
return fromJson(BsonUtils.parse(raw).asDocument()); | ||
sb.append(lables.stream().map(l -> l.toString()).collect(Collectors.joining(SEPARATOR))); | ||
return sb.toString(); | ||
} | ||
} |
File renamed without changes.
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
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