|
| 1 | +/* |
| 2 | + * Copyright 2024 Google LLC |
| 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 | + * http://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 com.google.cloud.opentelemetry.example.trace; |
| 17 | + |
| 18 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 20 | + |
| 21 | +import io.opentelemetry.api.common.AttributeKey; |
| 22 | +import io.opentelemetry.sdk.OpenTelemetrySdk; |
| 23 | +import io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter; |
| 24 | +import io.opentelemetry.sdk.trace.SdkTracerProvider; |
| 25 | +import io.opentelemetry.sdk.trace.data.SpanData; |
| 26 | +import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor; |
| 27 | +import java.util.List; |
| 28 | +import org.junit.jupiter.api.AfterEach; |
| 29 | +import org.junit.jupiter.api.BeforeEach; |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | + |
| 32 | +class TraceExporterExampleTest { |
| 33 | + |
| 34 | + private OpenTelemetrySdk testOTelSdk; |
| 35 | + private InMemorySpanExporter testInMemorySpanExporter; |
| 36 | + |
| 37 | + @BeforeEach |
| 38 | + public void setupOTelSdk() { |
| 39 | + testInMemorySpanExporter = InMemorySpanExporter.create(); |
| 40 | + SdkTracerProvider testTracerProvider = |
| 41 | + SdkTracerProvider.builder() |
| 42 | + .addSpanProcessor(SimpleSpanProcessor.create(testInMemorySpanExporter)) |
| 43 | + .build(); |
| 44 | + |
| 45 | + testOTelSdk = OpenTelemetrySdk.builder().setTracerProvider(testTracerProvider).build(); |
| 46 | + } |
| 47 | + |
| 48 | + @AfterEach |
| 49 | + public void cleanupOTelSdk() { |
| 50 | + testOTelSdk.close(); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testMyUseCase() { |
| 55 | + TraceExporterExample.myUseCase(testOTelSdk, "test"); |
| 56 | + List<SpanData> spanItems = testInMemorySpanExporter.getFinishedSpanItems(); |
| 57 | + assertFalse(spanItems.isEmpty()); |
| 58 | + |
| 59 | + // assert on custom span attributes set by application |
| 60 | + spanItems.stream() |
| 61 | + .filter(spanData -> spanData.getName().equals("test")) |
| 62 | + .map(SpanData::getAttributes) |
| 63 | + .forEach( |
| 64 | + spanAttributes -> { |
| 65 | + assertEquals(true, spanAttributes.get(AttributeKey.booleanKey("test_span"))); |
| 66 | + assertEquals(3, spanAttributes.get(AttributeKey.longKey("work_loop"))); |
| 67 | + }); |
| 68 | + } |
| 69 | +} |
0 commit comments