Search in sources :

Example 1 with MeterData

use of org.apache.skywalking.apm.network.language.agent.v3.MeterData in project skywalking-java by apache.

the class MeterSender method send.

public void send(Map<MeterId, BaseMeter> meterMap, MeterService meterService) {
    if (status == GRPCChannelStatus.CONNECTED) {
        StreamObserver<MeterData> reportStreamObserver = null;
        final GRPCStreamServiceStatus status = new GRPCStreamServiceStatus(false);
        try {
            reportStreamObserver = meterReportServiceStub.withDeadlineAfter(GRPC_UPSTREAM_TIMEOUT, TimeUnit.SECONDS).collect(new StreamObserver<Commands>() {

                @Override
                public void onNext(Commands commands) {
                }

                @Override
                public void onError(Throwable throwable) {
                    status.finished();
                    if (LOGGER.isErrorEnable()) {
                        LOGGER.error(throwable, "Send meters to collector fail with a grpc internal exception.");
                    }
                    ServiceManager.INSTANCE.findService(GRPCChannelManager.class).reportError(throwable);
                }

                @Override
                public void onCompleted() {
                    status.finished();
                }
            });
            final StreamObserver<MeterData> reporter = reportStreamObserver;
            transform(meterMap, meterData -> reporter.onNext(meterData));
        } catch (Throwable e) {
            if (!(e instanceof StatusRuntimeException)) {
                LOGGER.error(e, "Report meters to backend fail.");
                return;
            }
            final StatusRuntimeException statusRuntimeException = (StatusRuntimeException) e;
            if (statusRuntimeException.getStatus().getCode() == Status.Code.UNIMPLEMENTED) {
                LOGGER.warn("Backend doesn't support meter, it will be disabled");
                meterService.shutdown();
            }
        } finally {
            if (reportStreamObserver != null) {
                reportStreamObserver.onCompleted();
            }
            status.wait4Finish();
        }
    }
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) GRPCStreamServiceStatus(org.apache.skywalking.apm.agent.core.remote.GRPCStreamServiceStatus) GRPCChannelManager(org.apache.skywalking.apm.agent.core.remote.GRPCChannelManager) Commands(org.apache.skywalking.apm.network.common.v3.Commands) StatusRuntimeException(io.grpc.StatusRuntimeException) MeterData(org.apache.skywalking.apm.network.language.agent.v3.MeterData)

Example 2 with MeterData

use of org.apache.skywalking.apm.network.language.agent.v3.MeterData in project skywalking-java by apache.

the class MeterServiceTest method isSameWithCounter.

/**
 * Check counter message
 */
private void isSameWithCounter(MeterData meterData, boolean firstData, MeterId meterId, long count) {
    Assert.assertNotNull(meterData);
    if (firstData) {
        Assert.assertEquals(meterData.getService(), "testService");
        Assert.assertEquals(meterData.getServiceInstance(), "testServiceInstance");
        Assert.assertTrue(meterData.getTimestamp() > 0);
    } else {
        Assert.assertEquals(meterData.getService(), "");
        Assert.assertEquals(meterData.getServiceInstance(), "");
        Assert.assertTrue(meterData.getTimestamp() == 0L);
    }
    Assert.assertEquals(meterData.getMetricCase(), MeterData.MetricCase.SINGLEVALUE);
    Assert.assertNotNull(meterData.getSingleValue());
    final MeterSingleValue singleValue = meterData.getSingleValue();
    Assert.assertEquals(singleValue.getName(), meterId.getName());
    Assert.assertEquals(singleValue.getLabelsList(), meterId.transformTags());
    Assert.assertEquals(singleValue.getValue(), count, 0.0);
}
Also used : MeterSingleValue(org.apache.skywalking.apm.network.language.agent.v3.MeterSingleValue)

Example 3 with MeterData

use of org.apache.skywalking.apm.network.language.agent.v3.MeterData in project incubator-skywalking by apache.

the class MeterServiceHandler method handle.

@Override
public void handle(final ConsumerRecord<String, Bytes> record) {
    try (HistogramMetrics.Timer ignored = histogramBatch.createTimer()) {
        MeterDataCollection meterDataCollection = MeterDataCollection.parseFrom(record.value().get());
        MeterProcessor processor = processService.createProcessor();
        meterDataCollection.getMeterDataList().forEach(meterData -> {
            try (HistogramMetrics.Timer ignored2 = histogram.createTimer()) {
                processor.read(meterData);
            } catch (Exception e) {
                errorCounter.inc();
                log.error(e.getMessage(), e);
            }
        });
        processor.process();
    } catch (Exception e) {
        log.error("handle record failed", e);
    }
}
Also used : MeterDataCollection(org.apache.skywalking.apm.network.language.agent.v3.MeterDataCollection) HistogramMetrics(org.apache.skywalking.oap.server.telemetry.api.HistogramMetrics) MeterProcessor(org.apache.skywalking.oap.server.analyzer.provider.meter.process.MeterProcessor)

Example 4 with MeterData

use of org.apache.skywalking.apm.network.language.agent.v3.MeterData in project skywalking by apache.

the class MeterServiceHandler method handle.

@Override
public void handle(final ConsumerRecord<String, Bytes> record) {
    try (HistogramMetrics.Timer ignored = histogramBatch.createTimer()) {
        MeterDataCollection meterDataCollection = MeterDataCollection.parseFrom(record.value().get());
        MeterProcessor processor = processService.createProcessor();
        meterDataCollection.getMeterDataList().forEach(meterData -> {
            try (HistogramMetrics.Timer ignored2 = histogram.createTimer()) {
                processor.read(meterData);
            } catch (Exception e) {
                errorCounter.inc();
                log.error(e.getMessage(), e);
            }
        });
        processor.process();
    } catch (Exception e) {
        log.error("handle record failed", e);
    }
}
Also used : MeterDataCollection(org.apache.skywalking.apm.network.language.agent.v3.MeterDataCollection) HistogramMetrics(org.apache.skywalking.oap.server.telemetry.api.HistogramMetrics) MeterProcessor(org.apache.skywalking.oap.server.analyzer.provider.meter.process.MeterProcessor)

Example 5 with MeterData

use of org.apache.skywalking.apm.network.language.agent.v3.MeterData in project skywalking by apache.

the class MeterProcessor method read.

public void read(MeterData data) {
    // Parse and save meter
    switch(data.getMetricCase()) {
        case SINGLEVALUE:
            MeterSingleValue single = data.getSingleValue();
            meters.computeIfAbsent(single.getName(), k -> new ArrayList<>()).add(SampleBuilder.builder().name(single.getName()).labels(single.getLabelsList().stream().collect(toImmutableMap(Label::getName, Label::getValue))).value(single.getValue()).build());
            break;
        case HISTOGRAM:
            MeterHistogram histogram = data.getHistogram();
            Map<String, String> baseLabel = histogram.getLabelsList().stream().collect(Collectors.toMap(Label::getName, Label::getValue));
            meters.computeIfAbsent(histogram.getName(), k -> new ArrayList<>()).addAll(histogram.getValuesList().stream().map(v -> SampleBuilder.builder().name(histogram.getName()).labels(ImmutableMap.<String, String>builder().putAll(baseLabel).put("le", parseHistogramBucket(v)).build()).value(v.getCount()).build()).collect(Collectors.toList()));
            break;
        default:
            return;
    }
    // Agent info
    if (StringUtil.isNotEmpty(data.getService())) {
        service = data.getService();
    }
    if (StringUtil.isNotEmpty(data.getServiceInstance())) {
        serviceInstance = data.getServiceInstance();
    }
    if (data.getTimestamp() > 0) {
        timestamp = data.getTimestamp();
    }
}
Also used : SampleFamilyBuilder(org.apache.skywalking.oap.meter.analyzer.dsl.SampleFamilyBuilder) MeterSingleValue(org.apache.skywalking.apm.network.language.agent.v3.MeterSingleValue) Label(org.apache.skywalking.apm.network.language.agent.v3.Label) CollectionUtils(org.apache.skywalking.oap.server.library.util.CollectionUtils) ImmutableMap(com.google.common.collect.ImmutableMap) MeterBucketValue(org.apache.skywalking.apm.network.language.agent.v3.MeterBucketValue) HashMap(java.util.HashMap) StringUtils(org.apache.commons.lang3.StringUtils) HistogramType(org.apache.skywalking.oap.meter.analyzer.dsl.HistogramType) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) TimeUnit(java.util.concurrent.TimeUnit) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) MeterHistogram(org.apache.skywalking.apm.network.language.agent.v3.MeterHistogram) MetricConvert(org.apache.skywalking.oap.meter.analyzer.MetricConvert) MeterData(org.apache.skywalking.apm.network.language.agent.v3.MeterData) Sample(org.apache.skywalking.oap.meter.analyzer.dsl.Sample) Map(java.util.Map) StringUtil(org.apache.skywalking.oap.server.library.util.StringUtil) MeterHistogram(org.apache.skywalking.apm.network.language.agent.v3.MeterHistogram) MeterSingleValue(org.apache.skywalking.apm.network.language.agent.v3.MeterSingleValue) ArrayList(java.util.ArrayList)

Aggregations

MeterData (org.apache.skywalking.apm.network.language.agent.v3.MeterData)5 MeterProcessor (org.apache.skywalking.oap.server.analyzer.provider.meter.process.MeterProcessor)4 HistogramMetrics (org.apache.skywalking.oap.server.telemetry.api.HistogramMetrics)4 StreamObserver (io.grpc.stub.StreamObserver)3 MeterBucketValue (org.apache.skywalking.apm.network.language.agent.v3.MeterBucketValue)3 MeterHistogram (org.apache.skywalking.apm.network.language.agent.v3.MeterHistogram)3 MeterSingleValue (org.apache.skywalking.apm.network.language.agent.v3.MeterSingleValue)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 TimeUnit (java.util.concurrent.TimeUnit)2 Collectors (java.util.stream.Collectors)2 Slf4j (lombok.extern.slf4j.Slf4j)2 StringUtils (org.apache.commons.lang3.StringUtils)2 Label (org.apache.skywalking.apm.network.language.agent.v3.Label)2 MeterDataCollection (org.apache.skywalking.apm.network.language.agent.v3.MeterDataCollection)2 MetricConvert (org.apache.skywalking.oap.meter.analyzer.MetricConvert)2