Search in sources :

Example 26 with Scope

use of io.opencensus.common.Scope in project beam by apache.

the class DataChangeRecordAction method run.

/**
 * This is the main processing function for a {@link DataChangeRecord}. It returns an {@link
 * Optional} of {@link ProcessContinuation} to indicate if the calling function should stop or
 * not. If the {@link Optional} returned is empty, it means that the calling function can continue
 * with the processing. If an {@link Optional} of {@link ProcessContinuation#stop()} is returned,
 * it means that this function was unable to claim the timestamp of the {@link DataChangeRecord},
 * so the caller should stop.
 *
 * <p>When processing the {@link DataChangeRecord} the following procedure is applied:
 *
 * <ol>
 *   <li>We try to cliam the data change record commit timestamp. If it is not possible, we stop
 *       here and return.
 *   <li>We emit the data change record through the {@link OutputReceiver}.
 *   <li>We update the watermark to the data change record commit timestamp.
 * </ol>
 *
 * @param partition the current partition being processed
 * @param record the change stream data record received
 * @param tracker the restriction tracker of the {@link
 *     org.apache.beam.sdk.io.gcp.spanner.changestreams.dofn.ReadChangeStreamPartitionDoFn} SDF
 * @param outputReceiver the output receiver of the {@link
 *     org.apache.beam.sdk.io.gcp.spanner.changestreams.dofn.ReadChangeStreamPartitionDoFn} SDF
 * @param watermarkEstimator the watermark estimator of the {@link
 *     org.apache.beam.sdk.io.gcp.spanner.changestreams.dofn.ReadChangeStreamPartitionDoFn} SDF
 * @return {@link Optional#empty()} if the caller can continue processing more records. A non
 *     empty {@link Optional} with {@link ProcessContinuation#stop()} if this function was unable
 *     to claim the {@link ChildPartitionsRecord} timestamp
 */
@VisibleForTesting
public Optional<ProcessContinuation> run(PartitionMetadata partition, DataChangeRecord record, RestrictionTracker<OffsetRange, Long> tracker, OutputReceiver<DataChangeRecord> outputReceiver, ManualWatermarkEstimator<Instant> watermarkEstimator) {
    try (Scope scope = TRACER.spanBuilder("DataChangeRecordAction").setRecordEvents(true).startScopedSpan()) {
        TRACER.getCurrentSpan().putAttribute(PARTITION_ID_ATTRIBUTE_LABEL, AttributeValue.stringAttributeValue(partition.getPartitionToken()));
        final String token = partition.getPartitionToken();
        LOG.debug("[" + token + "] Processing data record " + record.getCommitTimestamp());
        final Timestamp commitTimestamp = record.getCommitTimestamp();
        final Instant commitInstant = new Instant(commitTimestamp.toSqlTimestamp().getTime());
        final long commitMicros = TimestampConverter.timestampToMicros(commitTimestamp);
        if (!tracker.tryClaim(commitMicros)) {
            LOG.debug("[" + token + "] Could not claim queryChangeStream(" + commitTimestamp + "), stopping");
            return Optional.of(ProcessContinuation.stop());
        }
        outputReceiver.outputWithTimestamp(record, commitInstant);
        watermarkEstimator.setWatermark(commitInstant);
        LOG.debug("[" + token + "] Data record action completed successfully");
        return Optional.empty();
    }
}
Also used : Scope(io.opencensus.common.Scope) Instant(org.joda.time.Instant) Timestamp(com.google.cloud.Timestamp) VisibleForTesting(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.annotations.VisibleForTesting)

Example 27 with Scope

use of io.opencensus.common.Scope in project beam by apache.

the class HeartbeatRecordAction method run.

/**
 * This is the main processing function for a {@link HeartbeatRecord}. It returns an {@link
 * Optional} of {@link ProcessContinuation} to indicate if the calling function should stop or
 * not. If the {@link Optional} returned is empty, it means that the calling function can continue
 * with the processing. If an {@link Optional} of {@link ProcessContinuation#stop()} is returned,
 * it means that this function was unable to claim the timestamp of the {@link HeartbeatRecord},
 * so the caller should stop.
 *
 * <p>When processing the {@link HeartbeatRecord} the following procedure is applied:
 *
 * <ol>
 *   <li>We try to claim the heartbeat record timestamp. If it is not possible, we stop here and
 *       return.
 *   <li>We update the necessary metrics.
 *   <li>We update the watermark to the heartbeat record timestamp.
 * </ol>
 */
@VisibleForTesting
public Optional<ProcessContinuation> run(PartitionMetadata partition, HeartbeatRecord record, RestrictionTracker<OffsetRange, Long> tracker, ManualWatermarkEstimator<Instant> watermarkEstimator) {
    try (Scope scope = TRACER.spanBuilder("HeartbeatRecordAction").setRecordEvents(true).startScopedSpan()) {
        TRACER.getCurrentSpan().putAttribute(PARTITION_ID_ATTRIBUTE_LABEL, AttributeValue.stringAttributeValue(partition.getPartitionToken()));
        final String token = partition.getPartitionToken();
        LOG.debug("[" + token + "] Processing heartbeat record " + record);
        final Timestamp timestamp = record.getTimestamp();
        final Instant timestampInstant = new Instant(timestamp.toSqlTimestamp().getTime());
        final long timestampMicros = TimestampConverter.timestampToMicros(timestamp);
        if (!tracker.tryClaim(timestampMicros)) {
            LOG.debug("[" + token + "] Could not claim queryChangeStream(" + timestamp + "), stopping");
            return Optional.of(ProcessContinuation.stop());
        }
        metrics.incHeartbeatRecordCount();
        watermarkEstimator.setWatermark(timestampInstant);
        LOG.debug("[" + token + "] Heartbeat record action completed successfully");
        return Optional.empty();
    }
}
Also used : Scope(io.opencensus.common.Scope) Instant(org.joda.time.Instant) Timestamp(com.google.cloud.Timestamp) VisibleForTesting(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.annotations.VisibleForTesting)

Example 28 with Scope

use of io.opencensus.common.Scope in project ignite by apache.

the class OpenCensusMetricExporterSpi method export.

/**
 * {@inheritDoc}
 */
@Override
public void export() {
    StatsRecorder recorder = Stats.getStatsRecorder();
    try (Scope globalScope = tagScope()) {
        MeasureMap mmap = recorder.newMeasureMap();
        mreg.forEach(mreg -> {
            if (filter != null && !filter.test(mreg))
                return;
            mreg.forEach(metric -> {
                if (metric instanceof LongMetric || metric instanceof IntMetric || metric instanceof BooleanMetric || (metric instanceof ObjectMetric && ((ObjectMetric) metric).type() == Date.class) || (metric instanceof ObjectMetric && ((ObjectMetric) metric).type() == OffsetDateTime.class)) {
                    long val;
                    if (metric instanceof LongMetric)
                        val = ((LongMetric) metric).value();
                    else if (metric instanceof IntMetric)
                        val = ((IntMetric) metric).value();
                    else if (metric instanceof BooleanMetric)
                        val = ((BooleanMetric) metric).value() ? 1 : 0;
                    else if (metric instanceof ObjectMetric && ((ObjectMetric) metric).type() == Date.class)
                        val = ((ObjectMetric<Date>) metric).value().getTime();
                    else
                        val = ((ObjectMetric<OffsetDateTime>) metric).value().toInstant().toEpochMilli();
                    if (val < 0) {
                        if (log.isDebugEnabled())
                            log.debug("OpenCensus doesn't support negative values. Skip record of " + metric.name());
                        return;
                    }
                    MeasureLong msr = (MeasureLong) measures.computeIfAbsent(metric.name(), k -> createMeasure(metric, CREATE_LONG));
                    mmap.put(msr, val);
                } else if (metric instanceof DoubleMetric) {
                    double val = ((DoubleMetric) metric).value();
                    if (val < 0) {
                        if (log.isDebugEnabled())
                            log.debug("OpenCensus doesn't support negative values. Skip record of " + metric.name());
                        return;
                    }
                    MeasureDouble msr = (MeasureDouble) measures.computeIfAbsent(metric.name(), k -> createMeasure(metric, CREATE_DOUBLE));
                    mmap.put(msr, val);
                } else if (metric instanceof HistogramMetric) {
                    String[] names = histogramBucketNames((HistogramMetric) metric);
                    long[] vals = ((HistogramMetric) metric).value();
                    assert names.length == vals.length;
                    for (int i = 0; i < vals.length; i++) {
                        String name = names[i];
                        MeasureLong msr = (MeasureLong) measures.computeIfAbsent(name, k -> createMeasureLong(name, metric.description()));
                        mmap.put(msr, vals[i]);
                    }
                } else if (log.isDebugEnabled()) {
                    log.debug(metric.name() + "[" + metric.getClass() + "] not supported by Opencensus exporter");
                }
            });
        });
        mmap.record();
    }
}
Also used : StatsRecorder(io.opencensus.stats.StatsRecorder) TagContextBuilder(io.opencensus.tags.TagContextBuilder) StatsRecorder(io.opencensus.stats.StatsRecorder) IgniteSpiContext(org.apache.ignite.spi.IgniteSpiContext) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) Metric(org.apache.ignite.spi.metric.Metric) Stats(io.opencensus.stats.Stats) Date(java.util.Date) HashMap(java.util.HashMap) IgniteEx(org.apache.ignite.internal.IgniteEx) Function(java.util.function.Function) ObjectMetric(org.apache.ignite.spi.metric.ObjectMetric) ArrayList(java.util.ArrayList) PushMetricsExporterAdapter(org.apache.ignite.internal.processors.metric.PushMetricsExporterAdapter) Measure(io.opencensus.stats.Measure) MeasureDouble(io.opencensus.stats.Measure.MeasureDouble) MeasureMap(io.opencensus.stats.MeasureMap) Map(java.util.Map) TagMetadata(io.opencensus.tags.TagMetadata) TagValue(io.opencensus.tags.TagValue) View(io.opencensus.stats.View) ReadOnlyMetricManager(org.apache.ignite.spi.metric.ReadOnlyMetricManager) Tags(io.opencensus.tags.Tags) BooleanMetric(org.apache.ignite.spi.metric.BooleanMetric) HistogramMetric(org.apache.ignite.spi.metric.HistogramMetric) IntMetric(org.apache.ignite.spi.metric.IntMetric) Scope(io.opencensus.common.Scope) Name(io.opencensus.stats.View.Name) MetricRegistry(org.apache.ignite.internal.processors.metric.MetricRegistry) LastValue(io.opencensus.stats.Aggregation.LastValue) MeasureLong(io.opencensus.stats.Measure.MeasureLong) T2(org.apache.ignite.internal.util.typedef.T2) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) OffsetDateTime(java.time.OffsetDateTime) TagKey(io.opencensus.tags.TagKey) MetricUtils(org.apache.ignite.internal.processors.metric.impl.MetricUtils) UNLIMITED_PROPAGATION(io.opencensus.tags.TagMetadata.TagTtl.UNLIMITED_PROPAGATION) GridMetricManager(org.apache.ignite.internal.processors.metric.GridMetricManager) LongMetric(org.apache.ignite.spi.metric.LongMetric) DoubleMetric(org.apache.ignite.spi.metric.DoubleMetric) BooleanMetric(org.apache.ignite.spi.metric.BooleanMetric) HistogramMetric(org.apache.ignite.spi.metric.HistogramMetric) LongMetric(org.apache.ignite.spi.metric.LongMetric) MeasureDouble(io.opencensus.stats.Measure.MeasureDouble) MeasureMap(io.opencensus.stats.MeasureMap) Scope(io.opencensus.common.Scope) OffsetDateTime(java.time.OffsetDateTime) ObjectMetric(org.apache.ignite.spi.metric.ObjectMetric) MeasureLong(io.opencensus.stats.Measure.MeasureLong) DoubleMetric(org.apache.ignite.spi.metric.DoubleMetric) IntMetric(org.apache.ignite.spi.metric.IntMetric)

Example 29 with Scope

use of io.opencensus.common.Scope in project ignite by apache.

the class TimeLimitedHandler method export.

/**
 * {@inheritDoc}
 */
@Override
public void export(final Collection<SpanData> spanDataList) {
    final Scope exportScope = newExportScope();
    try {
        TimeLimiter timeLimiter = SimpleTimeLimiter.create(Executors.newSingleThreadExecutor());
        timeLimiter.callWithTimeout(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                timeLimitedExport(spanDataList);
                return null;
            }
        }, deadline.toMillis(), TimeUnit.MILLISECONDS);
    } catch (TimeoutException e) {
        handleException(e, "Timeout when exporting traces: " + e);
    } catch (InterruptedException e) {
        handleException(e, "Interrupted when exporting traces: " + e);
    } catch (Exception e) {
        handleException(e, "Failed to export traces: " + e);
    } finally {
        exportScope.close();
    }
}
Also used : SimpleTimeLimiter(com.google.common.util.concurrent.SimpleTimeLimiter) TimeLimiter(com.google.common.util.concurrent.TimeLimiter) Scope(io.opencensus.common.Scope) TimeoutException(java.util.concurrent.TimeoutException) TimeoutException(java.util.concurrent.TimeoutException)

Example 30 with Scope

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

the class TagContextBenchmark method scopeTagContext.

/**
 * Open and close a tag context scope.
 */
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public Scope scopeTagContext(Data data) {
    Scope scope = data.tagger.withTagContext(data.tagContext);
    scope.close();
    return scope;
}
Also used : Scope(io.opencensus.common.Scope) BenchmarkMode(org.openjdk.jmh.annotations.BenchmarkMode) OutputTimeUnit(org.openjdk.jmh.annotations.OutputTimeUnit)

Aggregations

Scope (io.opencensus.common.Scope)48 Test (org.junit.Test)18 Span (io.opencensus.trace.Span)8 Timestamp (com.google.cloud.Timestamp)5 TagContext (io.opencensus.tags.TagContext)5 SpanBuilder (io.opencensus.trace.SpanBuilder)5 VisibleForTesting (org.apache.beam.vendor.guava.v26_0_jre.com.google.common.annotations.VisibleForTesting)5 ArrayList (java.util.ArrayList)4 Instant (org.joda.time.Instant)4 HttpRequestContext (io.opencensus.contrib.http.HttpRequestContext)3 SimpleTimeLimiter (com.google.common.util.concurrent.SimpleTimeLimiter)2 TimeLimiter (com.google.common.util.concurrent.TimeLimiter)2 ByteString (com.google.protobuf.ByteString)2 LongPoint (io.opencensus.metrics.LongGauge.LongPoint)2 MeasureMap (io.opencensus.stats.MeasureMap)2 TagContextBuilder (io.opencensus.tags.TagContextBuilder)2 AttributeValue (io.opencensus.trace.AttributeValue)2 Tracer (io.opencensus.trace.Tracer)2 TraceConfig (io.opencensus.trace.config.TraceConfig)2 HashMap (java.util.HashMap)2