|
| 1 | +/* |
| 2 | + * Copyright 2024 The gRPC 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 | + * 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 | + |
| 17 | +package io.grpc.gcp.csm.observability; |
| 18 | + |
| 19 | +import com.google.common.annotations.VisibleForTesting; |
| 20 | +import io.grpc.ExperimentalApi; |
| 21 | +import io.grpc.InternalConfigurator; |
| 22 | +import io.grpc.InternalConfiguratorRegistry; |
| 23 | +import io.grpc.ManagedChannelBuilder; |
| 24 | +import io.grpc.ServerBuilder; |
| 25 | +import io.grpc.opentelemetry.GrpcOpenTelemetry; |
| 26 | +import io.grpc.opentelemetry.InternalGrpcOpenTelemetry; |
| 27 | +import io.opentelemetry.api.OpenTelemetry; |
| 28 | +import java.io.Closeable; |
| 29 | +import java.util.Collection; |
| 30 | +import java.util.Collections; |
| 31 | + |
| 32 | +/** |
| 33 | + * The entrypoint for GCP's CSM OpenTelemetry metrics functionality in gRPC. |
| 34 | + * |
| 35 | + * <p>CsmObservability uses {@link io.opentelemetry.api.OpenTelemetry} APIs for instrumentation. |
| 36 | + * When no SDK is explicitly added no telemetry data will be collected. See |
| 37 | + * {@code io.opentelemetry.sdk.OpenTelemetrySdk} for information on how to construct the SDK. |
| 38 | + */ |
| 39 | +@ExperimentalApi("TODO") |
| 40 | +public final class CsmObservability implements Closeable { |
| 41 | + private final GrpcOpenTelemetry delegate; |
| 42 | + private final MetadataExchanger exchanger; |
| 43 | + |
| 44 | + public static Builder newBuilder() { |
| 45 | + return new Builder(); |
| 46 | + } |
| 47 | + |
| 48 | + private CsmObservability(Builder builder) { |
| 49 | + this.delegate = builder.delegate.build(); |
| 50 | + this.exchanger = builder.exchanger; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Registers CsmObservability globally, applying its configuration to all subsequently created |
| 55 | + * gRPC channels and servers. |
| 56 | + * |
| 57 | + * <p>Note: Only one of CsmObservability and GrpcOpenTelemetry instance can be registered |
| 58 | + * globally. Any subsequent call to {@code registerGlobal()} will throw an {@code |
| 59 | + * IllegalStateException}. |
| 60 | + */ |
| 61 | + public void registerGlobal() { |
| 62 | + InternalConfiguratorRegistry.setConfigurators(Collections.singletonList( |
| 63 | + new InternalConfigurator() { |
| 64 | + @Override |
| 65 | + public void configureChannelBuilder(ManagedChannelBuilder<?> channelBuilder) { |
| 66 | + CsmObservability.this.configureChannelBuilder(channelBuilder); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public void configureServerBuilder(ServerBuilder<?> serverBuilder) { |
| 71 | + CsmObservability.this.configureServerBuilder(serverBuilder); |
| 72 | + } |
| 73 | + })); |
| 74 | + } |
| 75 | + |
| 76 | + @VisibleForTesting |
| 77 | + void configureChannelBuilder(ManagedChannelBuilder<?> builder) { |
| 78 | + delegate.configureChannelBuilder(builder); |
| 79 | + } |
| 80 | + |
| 81 | + @VisibleForTesting |
| 82 | + void configureServerBuilder(ServerBuilder<?> serverBuilder) { |
| 83 | + delegate.configureServerBuilder(serverBuilder); |
| 84 | + exchanger.configureServerBuilder(serverBuilder); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public void close() {} |
| 89 | + |
| 90 | + /** |
| 91 | + * Builder for configuring {@link CsmObservability}. |
| 92 | + */ |
| 93 | + @ExperimentalApi("TODO") |
| 94 | + public static final class Builder { |
| 95 | + private final GrpcOpenTelemetry.Builder delegate = GrpcOpenTelemetry.newBuilder(); |
| 96 | + private final MetadataExchanger exchanger; |
| 97 | + |
| 98 | + private Builder() { |
| 99 | + this(new MetadataExchanger()); |
| 100 | + } |
| 101 | + |
| 102 | + @VisibleForTesting |
| 103 | + Builder(MetadataExchanger exchanger) { |
| 104 | + this.exchanger = exchanger; |
| 105 | + InternalGrpcOpenTelemetry.builderPlugin(delegate, exchanger); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Sets the {@link io.opentelemetry.api.OpenTelemetry} entrypoint to use. This can be used to |
| 110 | + * configure OpenTelemetry by returning the instance created by a |
| 111 | + * {@code io.opentelemetry.sdk.OpenTelemetrySdkBuilder}. |
| 112 | + */ |
| 113 | + public Builder sdk(OpenTelemetry sdk) { |
| 114 | + delegate.sdk(sdk); |
| 115 | + return this; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Adds optionalLabelKey to all the metrics that can provide value for the |
| 120 | + * optionalLabelKey. |
| 121 | + */ |
| 122 | + public Builder addOptionalLabel(String optionalLabelKey) { |
| 123 | + delegate.addOptionalLabel(optionalLabelKey); |
| 124 | + return this; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Enables the specified metrics for collection and export. By default, only a subset of |
| 129 | + * metrics are enabled. |
| 130 | + */ |
| 131 | + public Builder enableMetrics(Collection<String> enableMetrics) { |
| 132 | + delegate.enableMetrics(enableMetrics); |
| 133 | + return this; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Disables the specified metrics from being collected and exported. |
| 138 | + */ |
| 139 | + public Builder disableMetrics(Collection<String> disableMetrics) { |
| 140 | + delegate.disableMetrics(disableMetrics); |
| 141 | + return this; |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Disable all metrics. If set to true all metrics must be explicitly enabled. |
| 146 | + */ |
| 147 | + public Builder disableAllMetrics() { |
| 148 | + delegate.disableAllMetrics(); |
| 149 | + return this; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Returns a new {@link CsmObservability} built with the configuration of this {@link |
| 154 | + * Builder}. |
| 155 | + */ |
| 156 | + public CsmObservability build() { |
| 157 | + return new CsmObservability(this); |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments