public String generateLogMessage(
      String header, Set<String> allowedMetricUrns, long lastReported) {
    MetricsContainerImpl nextMetricsSnapshot = new MetricsContainerImpl(this.stepName);
    nextMetricsSnapshot.update(this);
    MetricsContainerImpl deltaContainer =
        MetricsContainerImpl.deltaContainer(lastMetricsSnapshot, nextMetricsSnapshot);

    StringBuilder logMessage = new StringBuilder();
    logMessage.append(header);
    logMessage.append(deltaContainer.getCumulativeString(allowedMetricUrns));
    logMessage.append(String.format("(last reported at %s)%n", new Date(lastReported)));

    lastMetricsSnapshot = nextMetricsSnapshot;
    return logMessage.toString();
  }
@Test
  public void testGeneratedLogMessageShowsDeltas() {
    MetricName cName =
        MonitoringInfoMetricName.named(
            MonitoringInfoConstants.Urns.ELEMENT_COUNT,
            Collections.singletonMap("name", "counter"));
    HistogramData.BucketType bucketType = HistogramData.LinearBuckets.of(0, 2, 5);
    MetricName hName =
        MonitoringInfoMetricName.named(
            MonitoringInfoConstants.Urns.ELEMENT_COUNT,
            Collections.singletonMap("name", "histogram"));

    MetricsLogger logger = new MetricsLogger(null);
    logger.getCounter(cName).inc(2L);
    // Set buckets counts to: [0,1,1,,0,0,...]
    logger.getHistogram(hName, bucketType).update(1);
    logger.getHistogram(hName, bucketType).update(3);

    Set<String> allowedMetricUrns = new HashSet<String>();
    allowedMetricUrns.add(MonitoringInfoConstants.Urns.ELEMENT_COUNT);
    String msg = logger.generateLogMessage("My Headder", allowedMetricUrns, 0);
    assertThat(msg, CoreMatchers.containsString("beam:metric:element_count:v1 {name=counter} = 2"));
    assertThat(
        msg,
        CoreMatchers.containsString(
            "{name=histogram} = {count: 2, p50: 2.000000, p90: 3.600000, p99: 3.960000}"));

    logger.getCounter(cName).inc(3L);
    // Set buckets counts to: [0,5,6,0,0,0]
    // Which means a delta of: [0,4,5,0,0,0]
    for (int i = 0; i < 4; i++) {
      logger.getHistogram(hName, bucketType).update(1);
    }
    for (int i = 0; i < 5; i++) {
      logger.getHistogram(hName, bucketType).update(3);
    }
    msg = logger.generateLogMessage("My Header: ", allowedMetricUrns, 0);
    assertThat(msg, CoreMatchers.containsString("beam:metric:element_count:v1 {name=counter} = 3"));
    assertThat(
        msg,
        CoreMatchers.containsString(
            "{name=histogram} = {count: 9, p50: 2.200000, p90: 3.640000, p99: 3.964000}"));

    logger.getCounter(cName).inc(4L);
    // Set buckets counts to: [0,8,10,0,0,0]
    // Which means a delta of: [0,3,4,0,0,0]
    for (int i = 0; i < 3; i++) {
      logger.getHistogram(hName, bucketType).update(1);
    }
    for (int i = 0; i < 4; i++) {
      logger.getHistogram(hName, bucketType).update(3);
    }
    msg = logger.generateLogMessage("My Header: ", allowedMetricUrns, 0);
    assertThat(
        msg,
        CoreMatchers.containsString(
            "{name=histogram} = {count: 7, p50: 2.250000, p90: 3.650000, p99: 3.965000}"));
  }