Skip to content
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

deprecate OpenCensusInputRowParser in favor of OpenCensusProtobufInputFormat #26

Merged
merged 2 commits into from
Jan 5, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public List<? extends Module> getJacksonModules()
return Collections.singletonList(
new SimpleModule("OpenCensusProtobufInputRowParserModule")
.registerSubtypes(
new NamedType(OpenCensusProtobufInputRowParser.class, "opencensus-protobuf")
new NamedType(OpenCensusProtobufInputRowParser.class, "opencensus-protobuf"),
new NamedType(OpenCensusProtobufInputFormat.class, "opencensus-protobuf")
)
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.druid.data.input.opencensus.protobuf;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.druid.data.input.InputEntity;
import org.apache.druid.data.input.InputEntityReader;
import org.apache.druid.data.input.InputFormat;
import org.apache.druid.data.input.InputRowSchema;
import org.apache.druid.data.input.impl.ByteEntity;
import org.apache.druid.java.util.common.StringUtils;

import java.io.File;
import java.util.Objects;

public class OpenCensusProtobufInputFormat implements InputFormat
{
private static final String DEFAULT_METRIC_DIMENSION = "name";
private static final String DEFAULT_RESOURCE_PREFIX = "resource.";

private final String metricDimension;
private final String metricLabelPrefix;
private final String resourceLabelPrefix;

public OpenCensusProtobufInputFormat(
@JsonProperty("metricDimension") String metricDimension,
@JsonProperty("metricLabelPrefix") String metricLabelPrefix,
@JsonProperty("resourceLabelPrefix") String resourceLabelPrefix
)
{
this.metricDimension = metricDimension != null ? metricDimension : DEFAULT_METRIC_DIMENSION;
this.metricLabelPrefix = StringUtils.nullToEmptyNonDruidDataString(metricLabelPrefix);
this.resourceLabelPrefix = resourceLabelPrefix != null ? resourceLabelPrefix : DEFAULT_RESOURCE_PREFIX;
}

@Override
public boolean isSplittable()
{
return false;
}

@Override
public InputEntityReader createReader(InputRowSchema inputRowSchema, InputEntity source, File temporaryDirectory)
{
return new OpenCensusProtobufReader(
inputRowSchema.getDimensionsSpec(),
(ByteEntity) source,
metricDimension,
metricLabelPrefix,
resourceLabelPrefix
);
}

@JsonProperty
public String getMetricDimension()
{
return metricDimension;
}

@JsonProperty
public String getMetricLabelPrefix()
{
return metricLabelPrefix;
}

@JsonProperty
public String getResourceLabelPrefix()
{
return resourceLabelPrefix;
}

@Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (!(o instanceof OpenCensusProtobufInputFormat)) {
return false;
}
OpenCensusProtobufInputFormat that = (OpenCensusProtobufInputFormat) o;
return Objects.equals(metricDimension, that.metricDimension) && Objects.equals(
metricLabelPrefix,
that.metricLabelPrefix
) && Objects.equals(resourceLabelPrefix, that.resourceLabelPrefix);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this auto-formatted by spotless 😛?
IMO this would be a bit easier on the eyes:

Suggested change
return Objects.equals(metricDimension, that.metricDimension) && Objects.equals(
metricLabelPrefix,
that.metricLabelPrefix
) && Objects.equals(resourceLabelPrefix, that.resourceLabelPrefix);
return Objects.equals(metricDimension, that.metricDimension)
&& Objects.equals(metricLabelPrefix, that.metricLabelPrefix)
&& Objects.equals(resourceLabelPrefix, that.resourceLabelPrefix);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's all auto-generated by IntelliJ using Druid's codestyle 🤷

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

}

@Override
public int hashCode()
{
return Objects.hash(metricDimension, metricLabelPrefix, resourceLabelPrefix);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,45 +22,29 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.Timestamp;
import io.opencensus.proto.metrics.v1.LabelKey;
import io.opencensus.proto.metrics.v1.Metric;
import io.opencensus.proto.metrics.v1.Point;
import io.opencensus.proto.metrics.v1.TimeSeries;
import org.apache.druid.data.input.ByteBufferInputRowParser;
import org.apache.druid.data.input.InputRow;
import org.apache.druid.data.input.MapBasedInputRow;
import org.apache.druid.data.input.impl.DimensionsSpec;
import org.apache.druid.data.input.impl.ByteEntity;
import org.apache.druid.data.input.impl.ParseSpec;
import org.apache.druid.java.util.common.StringUtils;
import org.apache.druid.java.util.common.logger.Logger;
import org.apache.druid.java.util.common.parsers.ParseException;
import org.apache.druid.utils.CollectionUtils;

import java.nio.ByteBuffer;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
* use {@link OpenCensusProtobufInputFormat} instead
*/
@Deprecated
public class OpenCensusProtobufInputRowParser implements ByteBufferInputRowParser
{
private static final Logger LOG = new Logger(OpenCensusProtobufInputRowParser.class);

private static final String SEPARATOR = "-";
private static final String VALUE_COLUMN = "value";
private static final String DEFAULT_METRIC_DIMENSION = "name";
private static final String DEFAULT_RESOURCE_PREFIX = "";

private final ParseSpec parseSpec;
private final DimensionsSpec dimensionsSpec;

private final String metricDimension;
private final String metricLabelPrefix;
Expand All @@ -75,7 +59,6 @@ public OpenCensusProtobufInputRowParser(
)
{
this.parseSpec = parseSpec;
this.dimensionsSpec = parseSpec.getDimensionsSpec();
this.metricDimension = Strings.isNullOrEmpty(metricDimension) ? DEFAULT_METRIC_DIMENSION : metricDimension;
this.metricLabelPrefix = StringUtils.nullToEmptyNonDruidDataString(metricPrefix);
this.resourceLabelPrefix = resourcePrefix != null ? resourcePrefix : DEFAULT_RESOURCE_PREFIX;
Expand Down Expand Up @@ -117,127 +100,16 @@ public OpenCensusProtobufInputRowParser withParseSpec(ParseSpec parseSpec)
resourceLabelPrefix);
}


private interface LabelContext
{
void addRow(long millis, String metricName, Object value);
}

@Override
public List<InputRow> parseBatch(ByteBuffer input)
{
final Metric metric;
try {
metric = Metric.parseFrom(input);
}
catch (InvalidProtocolBufferException e) {
throw new ParseException(e, "Protobuf message could not be parsed");
}

// Process metric descriptor labels map keys.
List<String> descriptorLabels = new ArrayList<>(metric.getMetricDescriptor().getLabelKeysCount());
for (LabelKey s : metric.getMetricDescriptor().getLabelKeysList()) {
descriptorLabels.add(this.metricLabelPrefix + s.getKey());
}

// Process resource labels map.
Map<String, String> resourceLabelsMap = CollectionUtils.mapKeys(
metric.getResource().getLabelsMap(),
key -> this.resourceLabelPrefix + key
);

final List<String> schemaDimensions = dimensionsSpec.getDimensionNames();

final List<String> dimensions;
if (!schemaDimensions.isEmpty()) {
dimensions = schemaDimensions;
} else {
Set<String> recordDimensions = new HashSet<>(descriptorLabels);

// Add resource map key set to record dimensions.
recordDimensions.addAll(resourceLabelsMap.keySet());

// MetricDimension, VALUE dimensions will not be present in labelKeysList or Metric.Resource
// map as they are derived dimensions, which get populated while parsing data for timeSeries
// hence add them to recordDimensions.
recordDimensions.add(metricDimension);
recordDimensions.add(VALUE_COLUMN);

dimensions = Lists.newArrayList(
Sets.difference(recordDimensions, dimensionsSpec.getDimensionExclusions())
);
}

final int capacity = resourceLabelsMap.size()
+ descriptorLabels.size()
+ 2; // metric name + value columns

List<InputRow> rows = new ArrayList<>();
for (TimeSeries ts : metric.getTimeseriesList()) {
final LabelContext labelContext = (millis, metricName, value) -> {
// Add common resourceLabels.
Map<String, Object> event = new HashMap<>(capacity);
event.putAll(resourceLabelsMap);
// Add metric labels
for (int i = 0; i < metric.getMetricDescriptor().getLabelKeysCount(); i++) {
event.put(descriptorLabels.get(i), ts.getLabelValues(i).getValue());
}
// add metric name and value
event.put(metricDimension, metricName);
event.put(VALUE_COLUMN, value);
rows.add(new MapBasedInputRow(millis, dimensions, event));
};

for (Point point : ts.getPointsList()) {
addPointRows(point, metric, labelContext);
}
}
return rows;
}

private void addPointRows(Point point, Metric metric, LabelContext labelContext)
{
Timestamp timestamp = point.getTimestamp();
long millis = Instant.ofEpochSecond(timestamp.getSeconds(), timestamp.getNanos()).toEpochMilli();
String metricName = metric.getMetricDescriptor().getName();

switch (point.getValueCase()) {
case DOUBLE_VALUE:
labelContext.addRow(millis, metricName, point.getDoubleValue());
break;

case INT64_VALUE:
labelContext.addRow(millis, metricName, point.getInt64Value());
break;

case SUMMARY_VALUE:
// count
labelContext.addRow(
millis,
metricName + SEPARATOR + "count",
point.getSummaryValue().getCount().getValue()
);
// sum
labelContext.addRow(
millis,
metricName + SEPARATOR + "sum",
point.getSummaryValue().getSnapshot().getSum().getValue()
);
break;

// TODO : How to handle buckets and percentiles
case DISTRIBUTION_VALUE:
// count
labelContext.addRow(millis, metricName + SEPARATOR + "count", point.getDistributionValue().getCount());
// sum
labelContext.addRow(
millis,
metricName + SEPARATOR + "sum",
point.getDistributionValue().getSum()
);
break;
default:
}
return new OpenCensusProtobufReader(
parseSpec.getDimensionsSpec(),
new ByteEntity(input),
metricDimension,
metricLabelPrefix,
resourceLabelPrefix
).readAsList();
}

@Override
Expand Down
Loading