public static MetricsReporter combine(MetricsReporter first, MetricsReporter second) {
    if (null == first) {
      return second;
    } else if (null == second || first == second) {
      return first;
    }

    Set<MetricsReporter> reporters = Sets.newIdentityHashSet();

    if (first instanceof CompositeMetricsReporter) {
      reporters.addAll(((CompositeMetricsReporter) first).reporters());
    } else {
      reporters.add(first);
    }

    if (second instanceof CompositeMetricsReporter) {
      reporters.addAll(((CompositeMetricsReporter) second).reporters());
    } else {
      reporters.add(second);
    }

    return new CompositeMetricsReporter(reporters);
  }
@Test
  public void combineComposites() {
    MetricsReporter one = report -> {};
    MetricsReporter two = report -> {};
    MetricsReporter firstComposite =
        MetricsReporters.combine(one, LoggingMetricsReporter.instance());
    MetricsReporter secondComposite =
        MetricsReporters.combine(two, LoggingMetricsReporter.instance());

    MetricsReporter combined = MetricsReporters.combine(firstComposite, two);
    assertThat(combined).isInstanceOf(MetricsReporters.CompositeMetricsReporter.class);
    assertThat(((MetricsReporters.CompositeMetricsReporter) combined).reporters())
        .hasSize(3)
        .containsExactlyInAnyOrder(one, two, LoggingMetricsReporter.instance());

    combined = MetricsReporters.combine(firstComposite, secondComposite);
    assertThat(combined).isInstanceOf(MetricsReporters.CompositeMetricsReporter.class);
    assertThat(((MetricsReporters.CompositeMetricsReporter) combined).reporters())
        .hasSize(3)
        .containsExactlyInAnyOrder(one, two, LoggingMetricsReporter.instance());
  }