Implement TimedEvent in SDK. (#354)
* Implement Event in SDK. * Make EventSdk package-private. * EventSdk -> TimedEvent.
This commit is contained in:
parent
2afd4d3be5
commit
e76f543b00
|
@ -0,0 +1,92 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2019, 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.trace;
|
||||||
|
|
||||||
|
import com.google.auto.value.AutoValue;
|
||||||
|
import io.opentelemetry.trace.AttributeValue;
|
||||||
|
import io.opentelemetry.trace.Event;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Map;
|
||||||
|
import javax.annotation.concurrent.Immutable;
|
||||||
|
|
||||||
|
/** Timed event that uses nanoTime to represent the Timestamp. */
|
||||||
|
@Immutable
|
||||||
|
abstract class TimedEvent {
|
||||||
|
|
||||||
|
private static final Map<String, AttributeValue> EMPTY_ATTRIBUTES =
|
||||||
|
Collections.unmodifiableMap(Collections.<String, AttributeValue>emptyMap());
|
||||||
|
|
||||||
|
abstract long getNanotime();
|
||||||
|
|
||||||
|
abstract String getName();
|
||||||
|
|
||||||
|
abstract Map<String, AttributeValue> getAttributes();
|
||||||
|
|
||||||
|
TimedEvent() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an {@link TimedEvent} with the given time, name and empty attributes.
|
||||||
|
*
|
||||||
|
* @param nanoTime time in nanos.
|
||||||
|
* @param name the name of this {@code TimedEvent}.
|
||||||
|
* @return an {@code TimedEvent}.
|
||||||
|
*/
|
||||||
|
static TimedEvent create(long nanoTime, String name) {
|
||||||
|
return create(nanoTime, name, EMPTY_ATTRIBUTES);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an {@link TimedEvent} with the given time, name and attributes.
|
||||||
|
*
|
||||||
|
* @param nanoTime time in nanos.
|
||||||
|
* @param name the name of this {@code TimedEvent}.
|
||||||
|
* @param attributes the attributes of this {@code TimedEvent}.
|
||||||
|
* @return an {@code TimedEvent}.
|
||||||
|
*/
|
||||||
|
static TimedEvent create(long nanoTime, String name, Map<String, AttributeValue> attributes) {
|
||||||
|
return new AutoValue_TimedEvent_RawTimedEvent(nanoTime, name, attributes);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an {@link TimedEvent} with the given time and event.
|
||||||
|
*
|
||||||
|
* @param nanoTime time in nanos.
|
||||||
|
* @param event the event.
|
||||||
|
* @return an {@code TimedEvent}.
|
||||||
|
*/
|
||||||
|
static TimedEvent create(long nanoTime, Event event) {
|
||||||
|
return new AutoValue_TimedEvent_TimedEventWithEvent(nanoTime, event);
|
||||||
|
}
|
||||||
|
|
||||||
|
@AutoValue
|
||||||
|
abstract static class TimedEventWithEvent extends TimedEvent {
|
||||||
|
abstract Event getEvent();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
String getName() {
|
||||||
|
return getEvent().getName();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
Map<String, AttributeValue> getAttributes() {
|
||||||
|
return getEvent().getAttributes();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@AutoValue
|
||||||
|
abstract static class RawTimedEvent extends TimedEvent {}
|
||||||
|
}
|
|
@ -0,0 +1,75 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2019, 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.trace;
|
||||||
|
|
||||||
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
|
import io.opentelemetry.trace.AttributeValue;
|
||||||
|
import io.opentelemetry.trace.Event;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Map;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.junit.runners.JUnit4;
|
||||||
|
|
||||||
|
/** Unit tests for {@link TimedEvent}. */
|
||||||
|
@RunWith(JUnit4.class)
|
||||||
|
public class TimedEventTest {
|
||||||
|
|
||||||
|
private static final String NAME = "event";
|
||||||
|
private static final String NAME_2 = "event2";
|
||||||
|
private static final Map<String, AttributeValue> ATTRIBUTES =
|
||||||
|
Collections.singletonMap("attribute", AttributeValue.stringAttributeValue("value"));
|
||||||
|
private static final Map<String, AttributeValue> ATTRIBUTES_2 =
|
||||||
|
Collections.singletonMap("attribute2", AttributeValue.stringAttributeValue("value2"));
|
||||||
|
private static final Event EVENT =
|
||||||
|
new Event() {
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return NAME_2;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, AttributeValue> getAttributes() {
|
||||||
|
return ATTRIBUTES_2;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void rawTimedEventWithName() {
|
||||||
|
TimedEvent event = TimedEvent.create(1000, NAME);
|
||||||
|
assertThat(event.getNanotime()).isEqualTo(1000);
|
||||||
|
assertThat(event.getName()).isEqualTo(NAME);
|
||||||
|
assertThat(event.getAttributes()).isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void rawTimedEventWithNameAndAttributes() {
|
||||||
|
TimedEvent event = TimedEvent.create(2000, NAME, ATTRIBUTES);
|
||||||
|
assertThat(event.getNanotime()).isEqualTo(2000);
|
||||||
|
assertThat(event.getName()).isEqualTo(NAME);
|
||||||
|
assertThat(event.getAttributes()).isEqualTo(ATTRIBUTES);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void timedEventWithEvent() {
|
||||||
|
TimedEvent event = TimedEvent.create(3000, EVENT);
|
||||||
|
assertThat(event.getNanotime()).isEqualTo(3000);
|
||||||
|
assertThat(event.getName()).isEqualTo(NAME_2);
|
||||||
|
assertThat(event.getAttributes()).isEqualTo(ATTRIBUTES_2);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue