|
| 1 | +/** |
| 2 | + * Copyright 2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.micrometer.tracing.otel.bridge; |
| 17 | + |
| 18 | +import io.micrometer.tracing.exporter.SpanExportingPredicate; |
| 19 | +import io.micrometer.tracing.exporter.SpanFilter; |
| 20 | +import io.micrometer.tracing.exporter.SpanReporter; |
| 21 | +import io.opentelemetry.api.common.Attributes; |
| 22 | +import io.opentelemetry.api.trace.SpanContext; |
| 23 | +import io.opentelemetry.api.trace.SpanKind; |
| 24 | +import io.opentelemetry.sdk.common.CompletableResultCode; |
| 25 | +import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; |
| 26 | +import io.opentelemetry.sdk.common.InstrumentationScopeInfo; |
| 27 | +import io.opentelemetry.sdk.resources.Resource; |
| 28 | +import io.opentelemetry.sdk.trace.data.EventData; |
| 29 | +import io.opentelemetry.sdk.trace.data.LinkData; |
| 30 | +import io.opentelemetry.sdk.trace.data.SpanData; |
| 31 | +import io.opentelemetry.sdk.trace.data.StatusData; |
| 32 | +import io.opentelemetry.sdk.trace.export.SpanExporter; |
| 33 | +import org.assertj.core.api.BDDAssertions; |
| 34 | +import org.junit.jupiter.api.Test; |
| 35 | +import org.mockito.BDDMockito; |
| 36 | + |
| 37 | +import java.util.Arrays; |
| 38 | +import java.util.Collections; |
| 39 | +import java.util.List; |
| 40 | + |
| 41 | +import static org.mockito.BDDMockito.given; |
| 42 | +import static org.mockito.BDDMockito.then; |
| 43 | +import static org.mockito.Mockito.mock; |
| 44 | +import static org.mockito.Mockito.verify; |
| 45 | + |
| 46 | +class CompositeSpanExporterTests { |
| 47 | + |
| 48 | + @Test |
| 49 | + void should_call_predicate_filter_reporter_and_then_exporter() { |
| 50 | + SpanExporter exporter = mock(SpanExporter.class); |
| 51 | + given(exporter.export(BDDMockito.any())).willReturn(CompletableResultCode.ofSuccess()); |
| 52 | + SpanExportingPredicate predicate = span -> span.getName().equals("foo"); |
| 53 | + SpanFilter filter = span -> span.setName("baz"); |
| 54 | + SpanReporter reporter = mock(SpanReporter.class); |
| 55 | + |
| 56 | + SpanData fooSpan = new CustomSpanData("foo"); |
| 57 | + SpanData barSpan = new CustomSpanData("bar"); |
| 58 | + |
| 59 | + CompletableResultCode resultCode = new CompositeSpanExporter(Collections.singleton(exporter), |
| 60 | + Collections.singletonList(predicate), Collections.singletonList(reporter), |
| 61 | + Collections.singletonList(filter)).export(Arrays.asList(fooSpan, barSpan)); |
| 62 | + |
| 63 | + then(reporter).should().report(BDDMockito.argThat(finishedSpan -> "baz".equals(finishedSpan.getName()))); |
| 64 | + then(exporter).should().export( |
| 65 | + BDDMockito.argThat(spans -> spans.size() == 1 && "baz".equals(spans.iterator().next().getName()))); |
| 66 | + BDDAssertions.then(resultCode.isSuccess()).isTrue(); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void should_flush_all_exporters() { |
| 71 | + SpanExporter exporter = mock(SpanExporter.class); |
| 72 | + given(exporter.flush()).willReturn(CompletableResultCode.ofSuccess()); |
| 73 | + |
| 74 | + CompletableResultCode resultCode = new CompositeSpanExporter(Collections.singleton(exporter), null, null, null) |
| 75 | + .flush(); |
| 76 | + |
| 77 | + then(exporter).should().flush(); |
| 78 | + BDDAssertions.then(resultCode.isSuccess()).isTrue(); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void should_shutdown_all_exporters() { |
| 83 | + SpanExporter exporter = mock(SpanExporter.class); |
| 84 | + given(exporter.shutdown()).willReturn(CompletableResultCode.ofSuccess()); |
| 85 | + |
| 86 | + CompletableResultCode resultCode = new CompositeSpanExporter(Collections.singleton(exporter), null, null, null) |
| 87 | + .shutdown(); |
| 88 | + |
| 89 | + verify(exporter).shutdown(); |
| 90 | + BDDAssertions.then(resultCode.isSuccess()).isTrue(); |
| 91 | + } |
| 92 | + |
| 93 | + static class CustomSpanData implements SpanData { |
| 94 | + |
| 95 | + private String name; |
| 96 | + |
| 97 | + CustomSpanData(String name) { |
| 98 | + this.name = name; |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public String getName() { |
| 103 | + return this.name; |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public SpanKind getKind() { |
| 108 | + return SpanKind.PRODUCER; |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public SpanContext getSpanContext() { |
| 113 | + return SpanContext.getInvalid(); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public SpanContext getParentSpanContext() { |
| 118 | + return SpanContext.getInvalid(); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public StatusData getStatus() { |
| 123 | + return StatusData.ok(); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public long getStartEpochNanos() { |
| 128 | + return 0L; |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public Attributes getAttributes() { |
| 133 | + return Attributes.empty(); |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public List<EventData> getEvents() { |
| 138 | + return Collections.emptyList(); |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public List<LinkData> getLinks() { |
| 143 | + return Collections.emptyList(); |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public long getEndEpochNanos() { |
| 148 | + return 0L; |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public boolean hasEnded() { |
| 153 | + return true; |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public int getTotalRecordedEvents() { |
| 158 | + return 10; |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public int getTotalRecordedLinks() { |
| 163 | + return 20; |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public int getTotalAttributeCount() { |
| 168 | + return 30; |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public InstrumentationLibraryInfo getInstrumentationLibraryInfo() { |
| 173 | + return InstrumentationLibraryInfo.empty(); |
| 174 | + } |
| 175 | + |
| 176 | + @Override |
| 177 | + public InstrumentationScopeInfo getInstrumentationScopeInfo() { |
| 178 | + return InstrumentationScopeInfo.empty(); |
| 179 | + } |
| 180 | + |
| 181 | + @Override |
| 182 | + public Resource getResource() { |
| 183 | + return Resource.empty(); |
| 184 | + } |
| 185 | + |
| 186 | + } |
| 187 | + |
| 188 | +} |
0 commit comments