Search in sources :

Example 6 with MonitoringInfo

use of org.apache.beam.model.pipeline.v1.MetricsApi.MonitoringInfo in project beam by apache.

the class PCollectionConsumerRegistryTest method singleConsumer.

@Test
public void singleConsumer() throws Exception {
    final String pCollectionA = "pCollectionA";
    final String pTransformIdA = "pTransformIdA";
    MetricsContainerStepMap metricsContainerRegistry = new MetricsContainerStepMap();
    PCollectionConsumerRegistry consumers = new PCollectionConsumerRegistry(metricsContainerRegistry, mock(ExecutionStateTracker.class));
    FnDataReceiver<WindowedValue<String>> consumerA1 = mock(FnDataReceiver.class);
    consumers.register(pCollectionA, pTransformIdA, consumerA1, StringUtf8Coder.of());
    FnDataReceiver<WindowedValue<String>> wrapperConsumer = (FnDataReceiver<WindowedValue<String>>) (FnDataReceiver) consumers.getMultiplexingConsumer(pCollectionA);
    String elementValue = "elem";
    WindowedValue<String> element = valueInGlobalWindow(elementValue);
    int numElements = 10;
    for (int i = 0; i < numElements; i++) {
        wrapperConsumer.accept(element);
    }
    // Check that the underlying consumers are each invoked per element.
    verify(consumerA1, times(numElements)).accept(element);
    assertThat(consumers.keySet(), contains(pCollectionA));
    List<MonitoringInfo> expected = new ArrayList<>();
    SimpleMonitoringInfoBuilder builder = new SimpleMonitoringInfoBuilder();
    builder.setUrn(MonitoringInfoConstants.Urns.ELEMENT_COUNT);
    builder.setLabel(MonitoringInfoConstants.Labels.PCOLLECTION, pCollectionA);
    builder.setInt64SumValue(numElements);
    expected.add(builder.build());
    long elementByteSize = StringUtf8Coder.of().getEncodedElementByteSize(elementValue);
    builder = new SimpleMonitoringInfoBuilder();
    builder.setUrn(Urns.SAMPLED_BYTE_SIZE);
    builder.setLabel(MonitoringInfoConstants.Labels.PCOLLECTION, pCollectionA);
    builder.setInt64DistributionValue(DistributionData.create(numElements * elementByteSize, numElements, elementByteSize, elementByteSize));
    expected.add(builder.build());
    // Clear the timestamp before comparison.
    Iterable<MonitoringInfo> result = Iterables.filter(metricsContainerRegistry.getMonitoringInfos(), monitoringInfo -> monitoringInfo.containsLabels(Labels.PCOLLECTION));
    assertThat(result, containsInAnyOrder(expected.toArray()));
}
Also used : MetricsContainerStepMap(org.apache.beam.runners.core.metrics.MetricsContainerStepMap) FnDataReceiver(org.apache.beam.sdk.fn.data.FnDataReceiver) MonitoringInfo(org.apache.beam.model.pipeline.v1.MetricsApi.MonitoringInfo) ArrayList(java.util.ArrayList) SimpleMonitoringInfoBuilder(org.apache.beam.runners.core.metrics.SimpleMonitoringInfoBuilder) ExecutionStateTracker(org.apache.beam.runners.core.metrics.ExecutionStateTracker) WindowedValue(org.apache.beam.sdk.util.WindowedValue) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 7 with MonitoringInfo

use of org.apache.beam.model.pipeline.v1.MetricsApi.MonitoringInfo in project beam by apache.

the class SimpleMonitoringInfoBuilderTest method testUserDistribution.

@Test
public void testUserDistribution() throws Exception {
    SimpleMonitoringInfoBuilder builder = new SimpleMonitoringInfoBuilder();
    builder.setUrn(MonitoringInfoConstants.Urns.USER_DISTRIBUTION_INT64);
    builder.setLabel(MonitoringInfoConstants.Labels.NAME, "myName");
    builder.setLabel(MonitoringInfoConstants.Labels.NAMESPACE, "myNamespace");
    builder.setLabel(MonitoringInfoConstants.Labels.PTRANSFORM, "myStep");
    assertNull(builder.build());
    builder.setInt64DistributionValue(DistributionData.create(10, 2, 1, 9));
    // Pass now that the spec is fully met.
    MonitoringInfo monitoringInfo = builder.build();
    assertTrue(monitoringInfo != null);
    assertEquals(MonitoringInfoConstants.Urns.USER_DISTRIBUTION_INT64, monitoringInfo.getUrn());
    assertEquals("myName", monitoringInfo.getLabelsOrDefault(MonitoringInfoConstants.Labels.NAME, ""));
    assertEquals("myNamespace", monitoringInfo.getLabelsOrDefault(MonitoringInfoConstants.Labels.NAMESPACE, ""));
    assertEquals(MonitoringInfoConstants.TypeUrns.DISTRIBUTION_INT64_TYPE, monitoringInfo.getType());
    DistributionData data = MonitoringInfoEncodings.decodeInt64Distribution(monitoringInfo.getPayload());
    assertEquals(10L, data.sum());
    assertEquals(2L, data.count());
    assertEquals(9L, data.max());
    assertEquals(1L, data.min());
}
Also used : MonitoringInfo(org.apache.beam.model.pipeline.v1.MetricsApi.MonitoringInfo) Test(org.junit.Test)

Example 8 with MonitoringInfo

use of org.apache.beam.model.pipeline.v1.MetricsApi.MonitoringInfo in project beam by apache.

the class SpecMonitoringInfoValidatorTest method validateReturnsNoErrorOnValidMonitoringInfo.

@Test
public void validateReturnsNoErrorOnValidMonitoringInfo() {
    MonitoringInfo testInput = MonitoringInfo.newBuilder().setUrn(Urns.USER_SUM_INT64).putLabels(MonitoringInfoConstants.Labels.NAME, "anyCounter").putLabels(MonitoringInfoConstants.Labels.NAMESPACE, "").putLabels(MonitoringInfoConstants.Labels.PTRANSFORM, "anyString").setType(TypeUrns.SUM_INT64_TYPE).build();
    assertFalse(new SpecMonitoringInfoValidator().validate(testInput).isPresent());
    testInput = MonitoringInfo.newBuilder().setUrn(Urns.USER_DISTRIBUTION_INT64).putLabels(MonitoringInfoConstants.Labels.NAME, "anyDistribution").putLabels(MonitoringInfoConstants.Labels.NAMESPACE, "namespace").putLabels(MonitoringInfoConstants.Labels.PTRANSFORM, "anyString").setType(TypeUrns.DISTRIBUTION_INT64_TYPE).putLabels("dummy", "value").build();
    assertFalse(new SpecMonitoringInfoValidator().validate(testInput).isPresent());
    testInput = MonitoringInfo.newBuilder().setUrn(MonitoringInfoConstants.Urns.ELEMENT_COUNT).setType(TypeUrns.SUM_INT64_TYPE).putLabels(MonitoringInfoConstants.Labels.PTRANSFORM, "value").putLabels(MonitoringInfoConstants.Labels.PCOLLECTION, "anotherValue").build();
    assertFalse(new SpecMonitoringInfoValidator().validate(testInput).isPresent());
}
Also used : MonitoringInfo(org.apache.beam.model.pipeline.v1.MetricsApi.MonitoringInfo) Test(org.junit.Test)

Example 9 with MonitoringInfo

use of org.apache.beam.model.pipeline.v1.MetricsApi.MonitoringInfo in project beam by apache.

the class UserMonitoringInfoToCounterUpdateTransformerTest method testTransformThrowsIfMonitoringInfoWithWrongUrnPrefixReceived.

@Test
public void testTransformThrowsIfMonitoringInfoWithWrongUrnPrefixReceived() {
    Map<String, DataflowStepContext> stepContextMapping = new HashMap<>();
    MonitoringInfo monitoringInfo = MonitoringInfo.newBuilder().setUrn(Urns.ELEMENT_COUNT).setType(TypeUrns.SUM_INT64_TYPE).build();
    UserMonitoringInfoToCounterUpdateTransformer testObject = new UserMonitoringInfoToCounterUpdateTransformer(mockSpecValidator, stepContextMapping);
    when(mockSpecValidator.validate(any())).thenReturn(Optional.empty());
    exception.expect(RuntimeException.class);
    testObject.transform(monitoringInfo);
}
Also used : MonitoringInfo(org.apache.beam.model.pipeline.v1.MetricsApi.MonitoringInfo) HashMap(java.util.HashMap) DataflowStepContext(org.apache.beam.runners.dataflow.worker.DataflowExecutionContext.DataflowStepContext) Test(org.junit.Test)

Example 10 with MonitoringInfo

use of org.apache.beam.model.pipeline.v1.MetricsApi.MonitoringInfo in project beam by apache.

the class UserMonitoringInfoToCounterUpdateTransformerTest method testTransformReturnsValidCounterUpdateWhenValidUserMonitoringInfoReceived.

@Test
public void testTransformReturnsValidCounterUpdateWhenValidUserMonitoringInfoReceived() throws Exception {
    Map<String, DataflowStepContext> stepContextMapping = new HashMap<>();
    NameContext nc = NameContext.create("anyStageName", "anyOriginalName", "anySystemName", "anyUserName");
    DataflowStepContext dsc = mock(DataflowStepContext.class);
    when(dsc.getNameContext()).thenReturn(nc);
    stepContextMapping.put("anyValue", dsc);
    MonitoringInfo monitoringInfo = MonitoringInfo.newBuilder().setUrn(Urns.USER_SUM_INT64).setType(TypeUrns.SUM_INT64_TYPE).putLabels(MonitoringInfoConstants.Labels.NAME, "anyName").putLabels(MonitoringInfoConstants.Labels.NAMESPACE, "anyNamespace").putLabels(MonitoringInfoConstants.Labels.PTRANSFORM, "anyValue").setPayload(encodeInt64Counter(1L)).build();
    UserMonitoringInfoToCounterUpdateTransformer testObject = new UserMonitoringInfoToCounterUpdateTransformer(mockSpecValidator, stepContextMapping);
    when(mockSpecValidator.validate(any())).thenReturn(Optional.empty());
    CounterUpdate result = testObject.transform(monitoringInfo);
    assertNotNull(result);
    assertEqualsAsJson("{cumulative:true, integer:{highBits:0, lowBits:1}, " + "structuredNameAndMetadata:{metadata:{kind:'SUM'}, " + "name:{name:'anyName', origin:'USER', originNamespace:'anyNamespace', " + "originalStepName:'anyOriginalName'}}}", result);
}
Also used : MonitoringInfo(org.apache.beam.model.pipeline.v1.MetricsApi.MonitoringInfo) HashMap(java.util.HashMap) NameContext(org.apache.beam.runners.dataflow.worker.counters.NameContext) DataflowStepContext(org.apache.beam.runners.dataflow.worker.DataflowExecutionContext.DataflowStepContext) CounterUpdate(com.google.api.services.dataflow.model.CounterUpdate) Test(org.junit.Test)

Aggregations

MonitoringInfo (org.apache.beam.model.pipeline.v1.MetricsApi.MonitoringInfo)40 Test (org.junit.Test)39 HashMap (java.util.HashMap)23 DataflowStepContext (org.apache.beam.runners.dataflow.worker.DataflowExecutionContext.DataflowStepContext)11 SimpleMonitoringInfoBuilder (org.apache.beam.runners.core.metrics.SimpleMonitoringInfoBuilder)10 ArrayList (java.util.ArrayList)9 NameContext (org.apache.beam.runners.dataflow.worker.counters.NameContext)9 CounterUpdate (com.google.api.services.dataflow.model.CounterUpdate)7 WindowedValue (org.apache.beam.sdk.util.WindowedValue)4 SimpleCounter (org.apache.flink.metrics.SimpleCounter)4 MetricGroupTest (org.apache.flink.runtime.metrics.groups.MetricGroupTest)4 ExecutionStateTracker (org.apache.beam.runners.core.metrics.ExecutionStateTracker)3 MetricsContainerStepMap (org.apache.beam.runners.core.metrics.MetricsContainerStepMap)3 FnDataReceiver (org.apache.beam.sdk.fn.data.FnDataReceiver)3 ByteString (org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.ByteString)3 DistributionData (org.apache.beam.runners.core.metrics.DistributionData)2 StringUtf8Coder (org.apache.beam.sdk.coders.StringUtf8Coder)2 DistributionResult (org.apache.beam.sdk.metrics.DistributionResult)2 ArgumentMatcher (org.mockito.ArgumentMatcher)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2