Search in sources :

Example 36 with Timestamp

use of io.opencensus.common.Timestamp in project instrumentation-java by census-instrumentation.

the class StackdriverExportUtilsTest method convertTimestamp.

@Test
public void convertTimestamp() {
    Timestamp censusTimestamp1 = Timestamp.create(100, 3000);
    assertThat(StackdriverExportUtils.convertTimestamp(censusTimestamp1)).isEqualTo(com.google.protobuf.Timestamp.newBuilder().setSeconds(100).setNanos(3000).build());
    // Stackdriver doesn't allow negative values, instead it will replace the negative values
    // by returning a default instance.
    Timestamp censusTimestamp2 = Timestamp.create(-100, 3000);
    assertThat(StackdriverExportUtils.convertTimestamp(censusTimestamp2)).isEqualTo(com.google.protobuf.Timestamp.newBuilder().build());
}
Also used : Timestamp(io.opencensus.common.Timestamp) Test(org.junit.Test)

Example 37 with Timestamp

use of io.opencensus.common.Timestamp in project instrumentation-java by census-instrumentation.

the class InstanaExporterHandler method convertToJson.

static String convertToJson(Collection<SpanData> spanDataList) {
    StringBuilder sb = new StringBuilder();
    sb.append('[');
    for (final SpanData span : spanDataList) {
        final SpanContext spanContext = span.getContext();
        final SpanId parentSpanId = span.getParentSpanId();
        final Timestamp startTimestamp = span.getStartTimestamp();
        final Timestamp endTimestamp = span.getEndTimestamp();
        final Status status = span.getStatus();
        if (status == null || endTimestamp == null) {
            continue;
        }
        if (sb.length() > 1) {
            sb.append(',');
        }
        sb.append('{');
        sb.append("\"spanId\":\"").append(encodeSpanId(spanContext.getSpanId())).append("\",");
        sb.append("\"traceId\":\"").append(encodeTraceId(spanContext.getTraceId())).append("\",");
        if (parentSpanId != null) {
            sb.append("\"parentId\":\"").append(encodeSpanId(parentSpanId)).append("\",");
        }
        sb.append("\"timestamp\":").append(toMillis(startTimestamp)).append(',');
        sb.append("\"duration\":").append(toMillis(startTimestamp, endTimestamp)).append(',');
        sb.append("\"name\":\"").append(toSpanName(span)).append("\",");
        sb.append("\"type\":\"").append(toSpanType(span)).append('"');
        if (!status.isOk()) {
            sb.append(",\"error\":").append("true");
        }
        Map<String, AttributeValue> attributeMap = span.getAttributes().getAttributeMap();
        if (attributeMap.size() > 0) {
            StringBuilder dataSb = new StringBuilder();
            dataSb.append('{');
            for (Entry<String, AttributeValue> entry : attributeMap.entrySet()) {
                if (dataSb.length() > 1) {
                    dataSb.append(',');
                }
                dataSb.append("\"").append(entry.getKey()).append("\":\"").append(attributeValueToString(entry.getValue())).append("\"");
            }
            dataSb.append('}');
            sb.append(",\"data\":").append(dataSb);
        }
        sb.append('}');
    }
    sb.append(']');
    return sb.toString();
}
Also used : Status(io.opencensus.trace.Status) AttributeValue(io.opencensus.trace.AttributeValue) SpanContext(io.opencensus.trace.SpanContext) SpanData(io.opencensus.trace.export.SpanData) Timestamp(io.opencensus.common.Timestamp) SpanId(io.opencensus.trace.SpanId)

Example 38 with Timestamp

use of io.opencensus.common.Timestamp in project instrumentation-java by census-instrumentation.

the class OcAgentNodeUtils method getNodeInfo.

// Creates a Node with information from the OpenCensus library and environment variables.
static Node getNodeInfo(String serviceName) {
    String jvmName = ManagementFactory.getRuntimeMXBean().getName();
    Timestamp censusTimestamp = Timestamp.fromMillis(System.currentTimeMillis());
    return Node.newBuilder().setIdentifier(getProcessIdentifier(jvmName, censusTimestamp)).setLibraryInfo(getLibraryInfo(OpenCensusLibraryInformation.VERSION)).setServiceInfo(getServiceInfo(serviceName)).build();
}
Also used : Timestamp(io.opencensus.common.Timestamp)

Example 39 with Timestamp

use of io.opencensus.common.Timestamp in project instrumentation-java by census-instrumentation.

the class ViewManagerImplTest method settingStateToDisabledWillClearStats.

@SuppressWarnings("deprecation")
private void settingStateToDisabledWillClearStats(View view) {
    Timestamp timestamp1 = Timestamp.create(1, 0);
    clock.setTime(timestamp1);
    viewManager.registerView(view);
    statsRecorder.newMeasureMap().put(MEASURE_DOUBLE, 1.1).record(tagger.emptyBuilder().put(KEY, VALUE).build());
    StatsTestUtil.assertAggregationMapEquals(viewManager.getView(view.getName()).getAggregationMap(), ImmutableMap.of(Arrays.asList(VALUE), StatsTestUtil.createAggregationData(view.getAggregation(), view.getMeasure(), 1.1)), EPSILON);
    Timestamp timestamp2 = Timestamp.create(2, 0);
    clock.setTime(timestamp2);
    // This will clear stats.
    statsComponent.setState(StatsCollectionState.DISABLED);
    assertThat(viewManager.getView(view.getName())).isEqualTo(createEmptyViewData(view));
    Timestamp timestamp3 = Timestamp.create(3, 0);
    clock.setTime(timestamp3);
    statsComponent.setState(StatsCollectionState.ENABLED);
    Timestamp timestamp4 = Timestamp.create(4, 0);
    clock.setTime(timestamp4);
    // This ViewData does not have any stats, but it should not be an empty ViewData, since it has
    // non-zero TimeStamps.
    ViewData viewData = viewManager.getView(view.getName());
    assertThat(viewData.getAggregationMap()).isEmpty();
    AggregationWindowData windowData = viewData.getWindowData();
    if (windowData instanceof CumulativeData) {
        assertThat(windowData).isEqualTo(CumulativeData.create(timestamp3, timestamp4));
    } else {
        assertThat(windowData).isEqualTo(IntervalData.create(timestamp4));
    }
}
Also used : StatsTestUtil.createEmptyViewData(io.opencensus.implcore.stats.StatsTestUtil.createEmptyViewData) ViewData(io.opencensus.stats.ViewData) AggregationWindowData(io.opencensus.stats.ViewData.AggregationWindowData) Timestamp(io.opencensus.common.Timestamp) CumulativeData(io.opencensus.stats.ViewData.AggregationWindowData.CumulativeData)

Example 40 with Timestamp

use of io.opencensus.common.Timestamp in project instrumentation-java by census-instrumentation.

the class IntervalBucketTest method preventCallingGetFractionOnFutureBuckets.

@Test
public void preventCallingGetFractionOnFutureBuckets() {
    IntervalBucket bucket = new IntervalBucket(START, MINUTE, MEAN, MEASURE_DOUBLE);
    Timestamp thirtySecondsBeforeStart = Timestamp.create(30, 0);
    thrown.expect(IllegalArgumentException.class);
    bucket.getFraction(thirtySecondsBeforeStart);
}
Also used : Timestamp(io.opencensus.common.Timestamp) Test(org.junit.Test)

Aggregations

Timestamp (io.opencensus.common.Timestamp)40 Test (org.junit.Test)29 Metric (io.opencensus.metrics.export.Metric)19 ArrayList (java.util.ArrayList)12 TimeSeries (io.opencensus.metrics.export.TimeSeries)8 SpanContext (io.opencensus.trace.SpanContext)6 DoublePoint (io.opencensus.metrics.DoubleCumulative.DoublePoint)4 LabelKey (io.opencensus.metrics.LabelKey)4 LabelValue (io.opencensus.metrics.LabelValue)4 LongPoint (io.opencensus.metrics.LongCumulative.LongPoint)4 MetricDescriptor (io.opencensus.metrics.export.MetricDescriptor)4 AggregationWindowData (io.opencensus.stats.ViewData.AggregationWindowData)4 View (io.opencensus.stats.View)3 ViewData (io.opencensus.stats.ViewData)3 AttributeValue (io.opencensus.trace.AttributeValue)3 SpanId (io.opencensus.trace.SpanId)3 Status (io.opencensus.trace.Status)3 SpanData (io.opencensus.trace.export.SpanData)3 CurrentState (io.opencensus.implcore.internal.CurrentState)2 CumulativeData (io.opencensus.stats.ViewData.AggregationWindowData.CumulativeData)2