2727import com .google .common .base .MoreObjects ;
2828import com .google .common .base .Preconditions ;
2929import com .google .common .collect .ImmutableList ;
30+ import com .google .common .collect .ImmutableMap ;
3031import com .google .common .collect .ImmutableSet ;
3132import io .grpc .ClientInterceptor ;
3233import io .grpc .ManagedChannel ;
3637import java .net .MalformedURLException ;
3738import java .net .URL ;
3839import java .util .List ;
40+ import java .util .Map ;
3941import java .util .Set ;
4042import javax .net .ssl .SSLException ;
4143
@@ -75,7 +77,8 @@ public ServiceRpc create(SpannerOptions options) {
7577 private final int prefetchChunks ;
7678 private final int numChannels ;
7779 private final String userAgent ;
78-
80+ private final ImmutableMap <String , String > sessionLabels ;
81+
7982 private SpannerOptions (Builder builder ) {
8083 super (SpannerFactory .class , SpannerRpcFactory .class , builder , new SpannerDefaults ());
8184 numChannels = builder .numChannels ;
@@ -94,6 +97,7 @@ private SpannerOptions(Builder builder) {
9497 ? builder .sessionPoolOptions
9598 : SessionPoolOptions .newBuilder ().build ();
9699 prefetchChunks = builder .prefetchChunks ;
100+ sessionLabels = builder .sessionLabels ;
97101 }
98102
99103 /** Builder for {@link SpannerOptions} instances. */
@@ -108,6 +112,7 @@ public static class Builder
108112 private int prefetchChunks = DEFAULT_PREFETCH_CHUNKS ;
109113 private SessionPoolOptions sessionPoolOptions ;
110114 private String userAgentPrefix ;
115+ private ImmutableMap <String , String > sessionLabels ;
111116
112117 private Builder () {}
113118
@@ -117,6 +122,7 @@ private Builder() {}
117122 this .sessionPoolOptions = options .sessionPoolOptions ;
118123 this .prefetchChunks = options .prefetchChunks ;
119124 this .userAgentPrefix = options .userAgent ;
125+ this .sessionLabels = options .sessionLabels ;
120126 }
121127
122128 @ Override
@@ -152,6 +158,23 @@ public Builder setSessionPoolOption(SessionPoolOptions sessionPoolOptions) {
152158 return this ;
153159 }
154160
161+ /**
162+ * Sets the labels to add to all Sessions created in this client.
163+ *
164+ * @param sessionLabels Map from label key to label value. Label key and value cannot be null.
165+ * For more information on valid syntax see
166+ * <a href="https://cloud.google.com/spanner/docs/reference/rpc/google.spanner.v1#google.spanner.v1.Session">
167+ * api docs </a>.
168+ */
169+ public Builder setSessionLabels (Map <String , String > sessionLabels ) {
170+ Preconditions .checkNotNull (sessionLabels , "Session labels map cannot be null" );
171+ for (String value : sessionLabels .values ()) {
172+ Preconditions .checkNotNull (value , "Null values are not allowed in the labels map." );
173+ }
174+ this .sessionLabels = ImmutableMap .copyOf (sessionLabels );
175+ return this ;
176+ }
177+
155178 /**
156179 * Specifying this will allow the client to prefetch up to {@code prefetchChunks} {@code
157180 * PartialResultSet} chunks for each read and query. The data size of each chunk depends on the
@@ -205,6 +228,10 @@ public SessionPoolOptions getSessionPoolOptions() {
205228 return sessionPoolOptions ;
206229 }
207230
231+ public Map <String , String > getSessionLabels () {
232+ return sessionLabels ;
233+ }
234+
208235 public int getPrefetchChunks () {
209236 return prefetchChunks ;
210237 }
0 commit comments