|
| 1 | +/* |
| 2 | + * Copyright 2023 Google |
| 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.trace; |
| 17 | + |
| 18 | +import static com.google.api.client.util.Preconditions.checkNotNull; |
| 19 | + |
| 20 | +import com.google.api.gax.core.FixedCredentialsProvider; |
| 21 | +import com.google.api.gax.core.NoCredentialsProvider; |
| 22 | +import com.google.api.gax.grpc.GrpcTransportChannel; |
| 23 | +import com.google.api.gax.rpc.FixedTransportChannelProvider; |
| 24 | +import com.google.api.gax.rpc.HeaderProvider; |
| 25 | +import com.google.auth.Credentials; |
| 26 | +import com.google.auth.oauth2.GoogleCredentials; |
| 27 | +import com.google.cloud.trace.v2.TraceServiceClient; |
| 28 | +import com.google.cloud.trace.v2.TraceServiceSettings; |
| 29 | +import com.google.cloud.trace.v2.stub.TraceServiceStub; |
| 30 | +import com.google.common.collect.ImmutableMap; |
| 31 | +import com.google.devtools.cloudtrace.v2.AttributeValue; |
| 32 | +import com.google.devtools.cloudtrace.v2.ProjectName; |
| 33 | +import com.google.devtools.cloudtrace.v2.Span; |
| 34 | +import io.grpc.ManagedChannelBuilder; |
| 35 | +import io.opentelemetry.sdk.common.CompletableResultCode; |
| 36 | +import io.opentelemetry.sdk.trace.data.SpanData; |
| 37 | +import io.opentelemetry.sdk.trace.export.SpanExporter; |
| 38 | +import java.io.IOException; |
| 39 | +import java.util.ArrayList; |
| 40 | +import java.util.Collection; |
| 41 | +import java.util.List; |
| 42 | +import java.util.Map; |
| 43 | + |
| 44 | +/** |
| 45 | + * This class encapsulates internal implementation details for exporting spans for applications |
| 46 | + * running on Google cloud environment. This class should not be exposed to the end user for direct |
| 47 | + * usage. |
| 48 | + */ |
| 49 | +class InternalTraceExporter implements SpanExporter { |
| 50 | + |
| 51 | + private final CloudTraceClient cloudTraceClient; |
| 52 | + private final ProjectName projectName; |
| 53 | + private final String projectId; |
| 54 | + private final TraceTranslator translator; |
| 55 | + |
| 56 | + private static final Map<String, String> HEADERS = |
| 57 | + Map.of("User-Agent", "opentelemetry-operations-java/" + TraceVersions.EXPORTER_VERSION); |
| 58 | + private static final HeaderProvider HEADER_PROVIDER = () -> HEADERS; |
| 59 | + |
| 60 | + private static InternalTraceExporter createWithClient( |
| 61 | + String projectId, |
| 62 | + CloudTraceClient cloudTraceClient, |
| 63 | + ImmutableMap<String, String> attributeMappings, |
| 64 | + Map<String, AttributeValue> fixedAttributes) { |
| 65 | + return new InternalTraceExporter( |
| 66 | + projectId, cloudTraceClient, attributeMappings, fixedAttributes); |
| 67 | + } |
| 68 | + |
| 69 | + static SpanExporter createWithConfiguration(TraceConfiguration configuration) throws IOException { |
| 70 | + String projectId = configuration.getProjectId(); |
| 71 | + TraceServiceStub stub = configuration.getTraceServiceStub(); |
| 72 | + |
| 73 | + // TODO: Remove stub - tracked in issue #198 |
| 74 | + if (stub == null) { |
| 75 | + TraceServiceSettings.Builder builder = TraceServiceSettings.newBuilder(); |
| 76 | + |
| 77 | + // We only use the batchWriteSpans API in this exporter. |
| 78 | + builder |
| 79 | + .batchWriteSpansSettings() |
| 80 | + .setSimpleTimeoutNoRetries( |
| 81 | + org.threeten.bp.Duration.ofMillis(configuration.getDeadline().toMillis())); |
| 82 | + // For testing, we need to hack around our gRPC config. |
| 83 | + if (configuration.getInsecureEndpoint()) { |
| 84 | + builder.setCredentialsProvider(NoCredentialsProvider.create()); |
| 85 | + builder.setTransportChannelProvider( |
| 86 | + FixedTransportChannelProvider.create( |
| 87 | + GrpcTransportChannel.create( |
| 88 | + ManagedChannelBuilder.forTarget(configuration.getTraceServiceEndpoint()) |
| 89 | + .usePlaintext() |
| 90 | + .build()))); |
| 91 | + } else { |
| 92 | + Credentials credentials = |
| 93 | + configuration.getCredentials() == null |
| 94 | + ? GoogleCredentials.getApplicationDefault() |
| 95 | + : configuration.getCredentials(); |
| 96 | + builder.setCredentialsProvider( |
| 97 | + FixedCredentialsProvider.create(checkNotNull(credentials, "credentials"))); |
| 98 | + builder.setEndpoint(configuration.getTraceServiceEndpoint()); |
| 99 | + builder.setHeaderProvider(HEADER_PROVIDER); |
| 100 | + } |
| 101 | + |
| 102 | + return new InternalTraceExporter( |
| 103 | + projectId, |
| 104 | + new CloudTraceClientImpl(TraceServiceClient.create(builder.build())), |
| 105 | + configuration.getAttributeMapping(), |
| 106 | + configuration.getFixedAttributes()); |
| 107 | + } |
| 108 | + return InternalTraceExporter.createWithClient( |
| 109 | + projectId, |
| 110 | + new CloudTraceClientImpl(TraceServiceClient.create(stub)), |
| 111 | + configuration.getAttributeMapping(), |
| 112 | + configuration.getFixedAttributes()); |
| 113 | + } |
| 114 | + |
| 115 | + InternalTraceExporter( |
| 116 | + String projectId, |
| 117 | + CloudTraceClient cloudTraceClient, |
| 118 | + ImmutableMap<String, String> attributeMappings, |
| 119 | + Map<String, AttributeValue> fixedAttributes) { |
| 120 | + this.projectId = projectId; |
| 121 | + this.cloudTraceClient = cloudTraceClient; |
| 122 | + this.projectName = ProjectName.of(projectId); |
| 123 | + this.translator = new TraceTranslator(attributeMappings, fixedAttributes); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public CompletableResultCode flush() { |
| 128 | + // We do no exporter buffering of spans, so we're always flushed. |
| 129 | + return CompletableResultCode.ofSuccess(); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public CompletableResultCode export(Collection<SpanData> spanDataList) { |
| 134 | + List<Span> spans = new ArrayList<>(spanDataList.size()); |
| 135 | + for (SpanData spanData : spanDataList) { |
| 136 | + spans.add(translator.generateSpan(spanData, projectId)); |
| 137 | + } |
| 138 | + |
| 139 | + cloudTraceClient.batchWriteSpans(projectName, spans); |
| 140 | + return CompletableResultCode.ofSuccess(); |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public CompletableResultCode shutdown() { |
| 145 | + this.cloudTraceClient.shutdown(); |
| 146 | + return CompletableResultCode.ofSuccess(); |
| 147 | + } |
| 148 | +} |
0 commit comments