Skip to content

Commit

Permalink
Merge branch 'master' into array-span-attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
arminru authored Jan 31, 2020
2 parents 852a82b + c6dedcb commit fc7fd73
Show file tree
Hide file tree
Showing 23 changed files with 302 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private static boolean isPrintableChar(char ch) {
return ch >= ' ' && ch <= '~';
}

public static boolean isNullOrBlank(String value) {
public static boolean isNullOrEmpty(String value) {
return value == null || value.length() == 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public enum Type {
*
* @param stringValue The new value.
* @return an {@code AttributeValue} with a string value.
* @throws NullPointerException if {@code stringValue} is {@code null}.
* @since 0.1.0
*/
public static AttributeValue stringAttributeValue(String stringValue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ public class HttpTraceContext implements HttpTextFormat<SpanContext> {
private static final char TRACESTATE_ENTRY_DELIMITER = ',';
private static final Pattern TRACESTATE_ENTRY_DELIMITER_SPLIT_PATTERN =
Pattern.compile("[ \t]*" + TRACESTATE_ENTRY_DELIMITER + "[ \t]*");
static final SpanContext INVALID_SPAN_CONTEXT =
SpanContext.create(
TraceId.getInvalid(),
SpanId.getInvalid(),
TraceFlags.getDefault(),
Tracestate.getDefault());

@Override
public List<String> fields() {
Expand Down Expand Up @@ -110,7 +116,7 @@ public <C> void inject(SpanContext spanContext, C carrier, Setter<C> setter) {
TraceFlags traceFlags;
String traceparent = getter.get(carrier, TRACEPARENT);
if (traceparent == null) {
throw new IllegalArgumentException("Traceparent not present");
return INVALID_SPAN_CONTEXT;
}
try {
// TODO(bdrutu): Do we need to verify that version is hex and that for the version
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ public void isPrintableString() {

@Test
public void isNullOrEmpty() throws Exception {
assertThat(StringUtils.isNullOrBlank("")).isTrue();
assertThat(StringUtils.isNullOrBlank(null)).isTrue();
assertThat(StringUtils.isNullOrBlank("hello")).isFalse();
assertThat(StringUtils.isNullOrEmpty("")).isTrue();
assertThat(StringUtils.isNullOrEmpty(null)).isTrue();
assertThat(StringUtils.isNullOrEmpty("hello")).isFalse();
assertThat(StringUtils.isNullOrEmpty(" ")).isFalse();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -304,4 +304,11 @@ public void headerNames() {
assertThat(TRACEPARENT).isEqualTo("traceparent");
assertThat(TRACESTATE).isEqualTo("tracestate");
}

@Test
public void extract_emptyCarrier() {
Map<String, String> emptyHeaders = new HashMap<>();
assertThat(httpTraceContext.extract(emptyHeaders, getter))
.isEqualTo(HttpTraceContext.INVALID_SPAN_CONTEXT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ interface Setter<C> {
* @param carrier holds propagation fields. For example, an outgoing message or http request.
* @param getter invoked for each propagation key to get.
* @param <C> carrier of propagation fields, such as an http request.
* @return the extracted value, never {@code null}.
* @return the extracted value or an invalid span context if getter returned {@code null}, never
* {@code null}.
* @since 0.1.0
*/
<C> V extract(C carrier, Getter<C> getter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nullable;

final class Propagation extends BaseShimObject {
Propagation(TelemetryInfo telemetryInfo) {
Expand All @@ -38,6 +39,7 @@ public void injectTextFormat(SpanContextShim contextShim, TextMapInject carrier)
.inject(contextShim.getCorrelationContext(), carrier, TextMapSetter.INSTANCE);
}

@Nullable
public SpanContextShim extractTextFormat(TextMapExtract carrier) {
Map<String, String> carrierMap = new HashMap<String, String>();
for (Map.Entry<String, String> entry : carrier) {
Expand All @@ -48,7 +50,9 @@ public SpanContextShim extractTextFormat(TextMapExtract carrier) {
tracer().getHttpTextFormat().extract(carrierMap, TextMapGetter.INSTANCE);
io.opentelemetry.correlationcontext.CorrelationContext distContext =
contextManager().getHttpTextFormat().extract(carrierMap, TextMapGetter.INSTANCE);

if (!context.isValid()) {
return null;
}
return new SpanContextShim(telemetryInfo, context, distContext);
}

Expand Down
60 changes: 60 additions & 0 deletions sdk/src/main/java/io/opentelemetry/sdk/metrics/Aggregator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2020, OpenTelemetry Authors
*
* 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 io.opentelemetry.sdk.metrics;

import javax.annotation.concurrent.ThreadSafe;

/** Aggregator represents the base class for all the available aggregations. */
@ThreadSafe
interface Aggregator<T extends Aggregator<?>> {

/**
* Merge aggregated values between the current instance and the given {@code aggregator}.
*
* @param aggregator value to merge with.
*/
void merge(T aggregator);

/**
* LongAggregator represents the base class for all the available aggregations that work with long
* values.
*/
@ThreadSafe
interface LongAggregator<T extends LongAggregator<?>> extends Aggregator<T> {

/**
* Updates the current aggregator with a newly recorded value.
*
* @param value the new {@code long} value to be added.
*/
void update(long value);
}

/**
* DoubleAggregator represents the base class for all the available aggregations that work with
* double values.
*/
@ThreadSafe
interface DoubleAggregator<T extends DoubleAggregator<?>> extends Aggregator<T> {
/**
* Updates the current aggregator with a newly recorded value.
*
* @param value the new {@code double} value to be added.
*/
void update(double value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
import java.util.List;
import java.util.Map;

class SdkDoubleCounter extends BaseInstrument<BoundDoubleCounter> implements DoubleCounter {
final class DoubleCounterSdk extends BaseInstrument<BoundDoubleCounter> implements DoubleCounter {

private final boolean monotonic;

private SdkDoubleCounter(
private DoubleCounterSdk(
String name,
String description,
Map<String, String> constantLabels,
Expand All @@ -43,22 +43,22 @@ public void add(double delta, LabelSet labelSet) {

@Override
BoundDoubleCounter createBoundInstrument(LabelSet labelSet) {
return new SdkBoundDoubleCounter(labelSet, monotonic);
return new Bound(labelSet, monotonic);
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof SdkDoubleCounter)) {
if (!(o instanceof DoubleCounterSdk)) {
return false;
}
if (!super.equals(o)) {
return false;
}

SdkDoubleCounter that = (SdkDoubleCounter) o;
DoubleCounterSdk that = (DoubleCounterSdk) o;

return monotonic == that.monotonic;
}
Expand All @@ -70,12 +70,11 @@ public int hashCode() {
return result;
}

private static class SdkBoundDoubleCounter extends BaseBoundInstrument
implements BoundDoubleCounter {
private static final class Bound extends BaseBoundInstrument implements BoundDoubleCounter {

private final boolean monotonic;

SdkBoundDoubleCounter(LabelSet labels, boolean monotonic) {
Bound(LabelSet labels, boolean monotonic) {
super(labels);
this.monotonic = monotonic;
}
Expand All @@ -89,7 +88,7 @@ public void add(double delta) {
}
}

static class Builder extends AbstractCounterBuilder<DoubleCounter.Builder, DoubleCounter>
static final class Builder extends AbstractCounterBuilder<DoubleCounter.Builder, DoubleCounter>
implements DoubleCounter.Builder {

private Builder(String name) {
Expand All @@ -107,7 +106,7 @@ Builder getThis() {

@Override
public DoubleCounter build() {
return new SdkDoubleCounter(
return new DoubleCounterSdk(
getName(), getDescription(), getConstantLabels(), getLabelKeys(), isMonotonic());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
import java.util.List;
import java.util.Map;

class SdkDoubleMeasure extends BaseInstrument<BoundDoubleMeasure> implements DoubleMeasure {
final class DoubleMeasureSdk extends BaseInstrument<BoundDoubleMeasure> implements DoubleMeasure {

private final boolean absolute;

private SdkDoubleMeasure(
private DoubleMeasureSdk(
String name,
String description,
Map<String, String> constantLabels,
Expand All @@ -43,22 +43,22 @@ public void record(double value, LabelSet labelSet) {

@Override
BoundDoubleMeasure createBoundInstrument(LabelSet labelSet) {
return new SdkBoundDoubleMeasure(labelSet, this.absolute);
return new Bound(labelSet, this.absolute);
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof SdkDoubleMeasure)) {
if (!(o instanceof DoubleMeasureSdk)) {
return false;
}
if (!super.equals(o)) {
return false;
}

SdkDoubleMeasure that = (SdkDoubleMeasure) o;
DoubleMeasureSdk that = (DoubleMeasureSdk) o;

return absolute == that.absolute;
}
Expand All @@ -70,12 +70,11 @@ public int hashCode() {
return result;
}

private static class SdkBoundDoubleMeasure extends BaseBoundInstrument
implements BoundDoubleMeasure {
private static final class Bound extends BaseBoundInstrument implements BoundDoubleMeasure {

private final boolean absolute;

SdkBoundDoubleMeasure(LabelSet labels, boolean absolute) {
Bound(LabelSet labels, boolean absolute) {
super(labels);
this.absolute = absolute;
}
Expand All @@ -89,7 +88,7 @@ public void record(double value) {
}
}

static class Builder extends AbstractMeasureBuilder<DoubleMeasure.Builder, DoubleMeasure>
static final class Builder extends AbstractMeasureBuilder<DoubleMeasure.Builder, DoubleMeasure>
implements DoubleMeasure.Builder {

private Builder(String name) {
Expand All @@ -107,7 +106,7 @@ Builder getThis() {

@Override
public DoubleMeasure build() {
return new SdkDoubleMeasure(
return new DoubleMeasureSdk(
getName(), getDescription(), getConstantLabels(), getLabelKeys(), isAbsolute());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2020, OpenTelemetry Authors
*
* 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 io.opentelemetry.sdk.metrics;

import com.google.common.util.concurrent.AtomicDouble;

final class DoubleSumAggregator implements Aggregator.DoubleAggregator<DoubleSumAggregator> {
// TODO: Change to use DoubleAdder when changed to java8.
private final AtomicDouble value;

DoubleSumAggregator() {
this.value = new AtomicDouble();
}

@Override
public void merge(DoubleSumAggregator other) {
this.value.addAndGet(other.value.get());
}

@Override
public void update(double value) {
this.value.addAndGet(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@
import java.util.Map;

@AutoValue
abstract class SdkLabelSet implements LabelSet {
abstract class LabelSetSdk implements LabelSet {
private static final LabelSet EMPTY =
new AutoValue_SdkLabelSet(Collections.<String, String>emptyMap());
new AutoValue_LabelSetSdk(Collections.<String, String>emptyMap());

static LabelSet create(Map<String, String> labels) {
if (labels == null || labels.isEmpty()) {
return EMPTY;
}
return new AutoValue_SdkLabelSet(unmodifiableMap(labels));
return new AutoValue_LabelSetSdk(unmodifiableMap(labels));
}

static LabelSet create(String... keyValuePairs) {
Expand All @@ -49,7 +49,7 @@ static LabelSet create(String... keyValuePairs) {
String key = keyValuePairs[i];
data.put(key, keyValuePairs[++i]);
}
return new AutoValue_SdkLabelSet(Collections.unmodifiableMap(data));
return new AutoValue_LabelSetSdk(Collections.unmodifiableMap(data));
}

abstract Map<String, String> getLabels();
Expand Down
Loading

0 comments on commit fc7fd73

Please sign in to comment.