|
| 1 | +/** |
| 2 | + * Copyright 2016 Inscope Metrics, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.arpnetworking.metrics.mad.parsers; |
| 17 | + |
| 18 | +import com.arpnetworking.metrics.common.parsers.Parser; |
| 19 | +import com.arpnetworking.metrics.common.parsers.exceptions.ParsingException; |
| 20 | +import com.arpnetworking.metrics.mad.model.DefaultMetric; |
| 21 | +import com.arpnetworking.metrics.mad.model.DefaultRecord; |
| 22 | +import com.arpnetworking.metrics.mad.model.HttpRequest; |
| 23 | +import com.arpnetworking.metrics.mad.model.Metric; |
| 24 | +import com.arpnetworking.metrics.mad.model.Record; |
| 25 | +import com.arpnetworking.tsdcore.model.MetricType; |
| 26 | +import com.arpnetworking.tsdcore.model.Quantity; |
| 27 | +import com.arpnetworking.tsdcore.model.Unit; |
| 28 | +import com.google.common.collect.ImmutableMap; |
| 29 | +import com.google.common.collect.Lists; |
| 30 | +import com.google.protobuf.InvalidProtocolBufferException; |
| 31 | +import com.inscopemetrics.client.protocol.ClientV1; |
| 32 | +import net.sf.oval.exception.ConstraintsViolatedException; |
| 33 | +import org.joda.time.DateTime; |
| 34 | + |
| 35 | +import java.nio.ByteBuffer; |
| 36 | +import java.util.List; |
| 37 | +import java.util.UUID; |
| 38 | +import javax.annotation.Nullable; |
| 39 | + |
| 40 | +/** |
| 41 | + * Parses the Inscope Metrics protobuf binary protocol into records. |
| 42 | + * |
| 43 | + * @author Brandon Arp (brandon dot arp at inscopemetrics dot com) |
| 44 | + */ |
| 45 | +public class ProtobufToRecordParser implements Parser<List<Record>, HttpRequest> { |
| 46 | + /** |
| 47 | + * {@inheritDoc} |
| 48 | + */ |
| 49 | + @Override |
| 50 | + public List<Record> parse(final HttpRequest data) throws ParsingException { |
| 51 | + try { |
| 52 | + final ClientV1.RecordSet request = ClientV1.RecordSet.parseFrom(data.getBody()); |
| 53 | + final List<Record> records = Lists.newArrayList(); |
| 54 | + for (final ClientV1.Record record : request.getRecordsList()) { |
| 55 | + final ByteBuffer byteBuffer = ByteBuffer.wrap(record.getId().toByteArray()); |
| 56 | + final Long high = byteBuffer.getLong(); |
| 57 | + final Long low = byteBuffer.getLong(); |
| 58 | + final DefaultRecord.Builder builder = new DefaultRecord.Builder() |
| 59 | + .setId(new UUID(high, low).toString()) |
| 60 | + .setTime(new DateTime(record.getEndMillisSinceEpoch())) |
| 61 | + .setAnnotations(buildAnnotations(record)) |
| 62 | + .setMetrics(buildMetrics(record)); |
| 63 | + |
| 64 | + records.add(builder.build()); |
| 65 | + } |
| 66 | + return records; |
| 67 | + } catch (final InvalidProtocolBufferException e) { |
| 68 | + throw new ParsingException("Could not create Request message from data", data.getBody(), e); |
| 69 | + } catch (final ConstraintsViolatedException | IllegalArgumentException e) { |
| 70 | + throw new ParsingException("Could not build record", data.getBody(), e); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private ImmutableMap<String, ? extends Metric> buildMetrics(final ClientV1.Record record) { |
| 75 | + final ImmutableMap.Builder<String, Metric> metrics = ImmutableMap.builder(); |
| 76 | + processEntries(metrics, record.getCountersList(), MetricType.COUNTER); |
| 77 | + processEntries(metrics, record.getTimersList(), MetricType.TIMER); |
| 78 | + processEntries(metrics, record.getGaugesList(), MetricType.GAUGE); |
| 79 | + return metrics.build(); |
| 80 | + } |
| 81 | + |
| 82 | + private void processEntries( |
| 83 | + final ImmutableMap.Builder<String, Metric> metrics, |
| 84 | + final List<ClientV1.MetricEntry> entries, |
| 85 | + final MetricType metricType) { |
| 86 | + for (final ClientV1.MetricEntry metricEntry : entries) { |
| 87 | + final DefaultMetric.Builder metricBuilder = new DefaultMetric.Builder() |
| 88 | + .setType(metricType); |
| 89 | + final List<Quantity> quantities = Lists.newArrayListWithExpectedSize(metricEntry.getSamplesCount()); |
| 90 | + for (final ClientV1.DoubleQuantity quantity : metricEntry.getSamplesList()) { |
| 91 | + quantities.add( |
| 92 | + new Quantity.Builder() |
| 93 | + .setUnit(baseUnit(quantity.getUnit())) |
| 94 | + .setValue(quantity.getValue()) |
| 95 | + .build()); |
| 96 | + } |
| 97 | + |
| 98 | + metricBuilder.setValues(quantities); |
| 99 | + metrics.put(metricEntry.getName(), metricBuilder.build()); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + @Nullable |
| 104 | + private Unit baseUnit(final ClientV1.CompoundUnit compoundUnit) { |
| 105 | + if (!compoundUnit.getNumeratorList().isEmpty()) { |
| 106 | + final ClientV1.Unit selectedUnit = compoundUnit.getNumerator(0); |
| 107 | + if (ClientV1.Unit.Type.UNRECOGNIZED.equals(selectedUnit.getType())) { |
| 108 | + return null; |
| 109 | + } |
| 110 | + final String unitName; |
| 111 | + if (!ClientV1.Unit.Scale.UNIT.equals(selectedUnit.getScale()) |
| 112 | + && !ClientV1.Unit.Scale.UNRECOGNIZED.equals(selectedUnit.getScale())) { |
| 113 | + unitName = selectedUnit.getScale().name() + selectedUnit.getType().name(); |
| 114 | + } else { |
| 115 | + unitName = selectedUnit.getType().name(); |
| 116 | + } |
| 117 | + |
| 118 | + return Unit.valueOf(unitName); |
| 119 | + } |
| 120 | + return null; |
| 121 | + } |
| 122 | + |
| 123 | + private ImmutableMap<String, String> buildAnnotations(final ClientV1.Record record) { |
| 124 | + final ImmutableMap.Builder<String, String> annotations = ImmutableMap.builder(); |
| 125 | + for (final ClientV1.AnnotationEntry annotationEntry : record.getAnnotationsList()) { |
| 126 | + annotations.put(annotationEntry.getName(), annotationEntry.getValue()); |
| 127 | + } |
| 128 | + for (final ClientV1.DimensionEntry dimensionEntry : record.getDimensionsList()) { |
| 129 | + annotations.put(dimensionEntry.getName(), dimensionEntry.getValue()); |
| 130 | + } |
| 131 | + return annotations.build(); |
| 132 | + } |
| 133 | +} |
0 commit comments