use of io.opencensus.common.Timestamp in project instrumentation-java by census-instrumentation.
the class TracezZPageHandler method emitSingleSpan.
// Emits the internal html for a single {@link SpanData}.
@SuppressWarnings("deprecation")
private static void emitSingleSpan(Formatter formatter, SpanData span) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(TimeUnit.SECONDS.toMillis(span.getStartTimestamp().getSeconds()));
long microsField = TimeUnit.NANOSECONDS.toMicros(span.getStartTimestamp().getNanos());
String elapsedSecondsStr = span.getEndTimestamp() != null ? String.format("%13.6f", durationToNanos(span.getEndTimestamp().subtractTimestamp(span.getStartTimestamp())) * 1.0e-9) : String.format("%13s", " ");
SpanContext spanContext = span.getContext();
formatter.format("<b>%04d/%02d/%02d-%02d:%02d:%02d.%06d %s TraceId: <b style=\"color:%s;\">%s</b> " + "SpanId: %s ParentSpanId: %s</b>%n", calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND), microsField, elapsedSecondsStr, spanContext.getTraceOptions().isSampled() ? SAMPLED_TRACE_ID_COLOR : NOT_SAMPLED_TRACE_ID_COLOR, BaseEncoding.base16().lowerCase().encode(spanContext.getTraceId().getBytes()), BaseEncoding.base16().lowerCase().encode(spanContext.getSpanId().getBytes()), BaseEncoding.base16().lowerCase().encode(span.getParentSpanId() == null ? SpanId.INVALID.getBytes() : span.getParentSpanId().getBytes()));
int lastEntryDayOfYear = calendar.get(Calendar.DAY_OF_YEAR);
Timestamp lastTimestampNanos = span.getStartTimestamp();
TimedEvents<Annotation> annotations = span.getAnnotations();
TimedEvents<io.opencensus.trace.NetworkEvent> networkEvents = span.getNetworkEvents();
List<TimedEvent<?>> timedEvents = new ArrayList<TimedEvent<?>>(annotations.getEvents());
timedEvents.addAll(networkEvents.getEvents());
Collections.sort(timedEvents, new TimedEventComparator());
for (TimedEvent<?> event : timedEvents) {
// Special printing so that durations smaller than one second
// are left padded with blanks instead of '0' characters.
// E.g.,
// Number Printout
// ---------------------------------
// 0.000534 . 534
// 1.000534 1.000534
long deltaMicros = TimeUnit.NANOSECONDS.toMicros(durationToNanos(event.getTimestamp().subtractTimestamp(lastTimestampNanos)));
String deltaString;
if (deltaMicros >= 1000000) {
deltaString = String.format("%.6f", (deltaMicros / 1000000.0));
} else {
deltaString = String.format(".%6d", deltaMicros);
}
calendar.setTimeInMillis(TimeUnit.SECONDS.toMillis(event.getTimestamp().getSeconds()) + TimeUnit.NANOSECONDS.toMillis(event.getTimestamp().getNanos()));
microsField = TimeUnit.NANOSECONDS.toMicros(event.getTimestamp().getNanos());
int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR);
if (dayOfYear == lastEntryDayOfYear) {
formatter.format("%11s", "");
} else {
formatter.format("%04d/%02d/%02d-", calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH));
lastEntryDayOfYear = dayOfYear;
}
formatter.format("%02d:%02d:%02d.%06d %13s ... %s%n", calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND), microsField, deltaString, htmlEscaper().escape(event.getEvent() instanceof Annotation ? renderAnnotation((Annotation) event.getEvent()) : renderNetworkEvents((io.opencensus.trace.NetworkEvent) castNonNull(event.getEvent()))));
lastTimestampNanos = event.getTimestamp();
}
Status status = span.getStatus();
if (status != null) {
formatter.format("%44s %s%n", "", htmlEscaper().escape(renderStatus(status)));
}
formatter.format("%44s %s%n", "", htmlEscaper().escape(renderAttributes(span.getAttributes().getAttributeMap())));
}
use of io.opencensus.common.Timestamp in project instrumentation-java by census-instrumentation.
the class DerivedDoubleCumulativeImplTest method createTimeSeries_WithObjFunction.
@Test
public void createTimeSeries_WithObjFunction() {
derivedDoubleCumulative.createTimeSeries(LABEL_VALUES, new QueueManager(), queueManagerFunction);
testClock.advanceTime(ONE_MINUTE);
Timestamp endTime = testClock.now();
Metric metric = derivedDoubleCumulative.getMetric(testClock);
assertThat(metric).isNotNull();
assertThat(metric).isEqualTo(Metric.createWithOneTimeSeries(METRIC_DESCRIPTOR, TimeSeries.createWithOnePoint(LABEL_VALUES, Point.create(Value.doubleValue(2.5), endTime), START_TIME)));
}
use of io.opencensus.common.Timestamp in project instrumentation-java by census-instrumentation.
the class DerivedDoubleCumulativeImplTest method multipleMetrics_GetMetric.
@Test
public void multipleMetrics_GetMetric() {
derivedDoubleCumulative.createTimeSeries(LABEL_VALUES, null, doubleFunction);
derivedDoubleCumulative.createTimeSeries(LABEL_VALUES_1, new QueueManager(), queueManagerFunction);
List<TimeSeries> expectedTimeSeriesList = new ArrayList<TimeSeries>();
testClock.advanceTime(ONE_MINUTE);
Timestamp endTime = testClock.now();
expectedTimeSeriesList.add(TimeSeries.createWithOnePoint(LABEL_VALUES, Point.create(Value.doubleValue(5.5), endTime), START_TIME));
expectedTimeSeriesList.add(TimeSeries.createWithOnePoint(LABEL_VALUES_1, Point.create(Value.doubleValue(2.5), endTime), START_TIME));
Metric metric = derivedDoubleCumulative.getMetric(testClock);
assertThat(metric).isNotNull();
assertThat(metric.getMetricDescriptor()).isEqualTo(METRIC_DESCRIPTOR);
assertThat(metric.getTimeSeriesList().size()).isEqualTo(2);
assertThat(metric.getTimeSeriesList()).containsExactlyElementsIn(expectedTimeSeriesList);
assertThat(metric.getTimeSeriesList().get(0).getLabelValues().size()).isEqualTo(1);
assertThat(metric.getTimeSeriesList().get(0).getLabelValues().get(0)).isEqualTo(LabelValue.create("value"));
assertThat(metric.getTimeSeriesList().get(1).getLabelValues().size()).isEqualTo(1);
assertThat(metric.getTimeSeriesList().get(1).getLabelValues().get(0)).isEqualTo(LabelValue.create("value1"));
}
use of io.opencensus.common.Timestamp in project instrumentation-java by census-instrumentation.
the class DerivedLongCumulativeImplTest method withConstantLabels.
@Test
public void withConstantLabels() {
List<LabelKey> labelKeys = Arrays.asList(LabelKey.create("key1", "desc"), LabelKey.create("key2", "desc"));
List<LabelValue> labelValues = Arrays.asList(LabelValue.create("value1"), LabelValue.create("value2"));
LabelKey constantKey = LabelKey.create("constant_key", "desc");
LabelValue constantValue = LabelValue.create("constant_value");
Map<LabelKey, LabelValue> constantLabels = Collections.<LabelKey, LabelValue>singletonMap(constantKey, constantValue);
DerivedLongCumulativeImpl derivedLongCumulative2 = new DerivedLongCumulativeImpl(METRIC_NAME, METRIC_DESCRIPTION, METRIC_UNIT, labelKeys, constantLabels, START_TIME);
derivedLongCumulative2.createTimeSeries(labelValues, new QueueManager(), queueManagerFunction);
testClock.advanceTime(ONE_MINUTE);
Timestamp endTime = testClock.now();
List<LabelKey> allKeys = new ArrayList<>(labelKeys);
allKeys.add(constantKey);
MetricDescriptor expectedDescriptor = MetricDescriptor.create(METRIC_NAME, METRIC_DESCRIPTION, METRIC_UNIT, Type.CUMULATIVE_INT64, allKeys);
List<LabelValue> allValues = new ArrayList<>(labelValues);
allValues.add(constantValue);
TimeSeries expectedTimeSeries = TimeSeries.createWithOnePoint(allValues, Point.create(Value.longValue(3), endTime), START_TIME);
Metric metric = derivedLongCumulative2.getMetric(testClock);
assertThat(metric).isNotNull();
assertThat(metric.getMetricDescriptor()).isEqualTo(expectedDescriptor);
assertThat(metric.getTimeSeriesList()).containsExactly(expectedTimeSeries);
derivedLongCumulative2.removeTimeSeries(labelValues);
Metric metric2 = derivedLongCumulative2.getMetric(testClock);
assertThat(metric2).isNull();
}
use of io.opencensus.common.Timestamp in project instrumentation-java by census-instrumentation.
the class DerivedLongCumulativeImplTest method multipleMetrics_GetMetric.
@Test
public void multipleMetrics_GetMetric() {
derivedLongCumulative.createTimeSeries(LABEL_VALUES, null, longFunction);
derivedLongCumulative.createTimeSeries(LABEL_VALUES_1, new QueueManager(), queueManagerFunction);
List<TimeSeries> expectedTimeSeriesList = new ArrayList<TimeSeries>();
testClock.advanceTime(ONE_MINUTE);
Timestamp endTime = testClock.now();
expectedTimeSeriesList.add(TimeSeries.createWithOnePoint(LABEL_VALUES, Point.create(Value.longValue(15), endTime), START_TIME));
expectedTimeSeriesList.add(TimeSeries.createWithOnePoint(LABEL_VALUES_1, Point.create(Value.longValue(3), endTime), START_TIME));
Metric metric = derivedLongCumulative.getMetric(testClock);
assertThat(metric).isNotNull();
assertThat(metric.getMetricDescriptor()).isEqualTo(METRIC_DESCRIPTOR);
assertThat(metric.getTimeSeriesList().size()).isEqualTo(2);
assertThat(metric.getTimeSeriesList()).containsExactlyElementsIn(expectedTimeSeriesList);
assertThat(metric.getTimeSeriesList().get(0).getLabelValues().size()).isEqualTo(1);
assertThat(metric.getTimeSeriesList().get(0).getLabelValues().get(0)).isEqualTo(LabelValue.create("value"));
assertThat(metric.getTimeSeriesList().get(1).getLabelValues().size()).isEqualTo(1);
assertThat(metric.getTimeSeriesList().get(1).getLabelValues().get(0)).isEqualTo(LabelValue.create("value1"));
}
Aggregations