Skip to content
This repository has been archived by the owner on Mar 27, 2021. It is now read-only.

Commit

Permalink
Add Bigtable app profile ID to metrics and analytics modules (#698)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Rustamov authored Sep 24, 2020
1 parent 0fe2ca8 commit 0222c51
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public HeroicConfig.Builder build(final ExtraParameters params) throws Exception

params.get("project").map(module::project);
params.get("instance").map(module::instance);
params.get("profile").map(module::profile);

final String credentials = params.get("credential").orElse(DEFAULT_CREDENTIALS);

Expand Down Expand Up @@ -101,6 +102,7 @@ public List<ParameterSpecification> options() {
"configured"),
parameter("project", "Bigtable project to use", "<project>"),
parameter("instance", "Bigtable instance to use", "<instance>"),
parameter("profile", "Bigtable profile to use", "<profile>"),
parameter("credentials", "Credentials implementation to use, must be one of:" +
" default, compute-engine, json, service-account", "<credentials>"),
parameter("json", "Json file to use when using json credentials", "<file>"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

@Module
public class BigtableAnalyticsModule implements AnalyticsModule {
private static final String DEFAULT_CLUSTER = "heroic";
private static final String DEFAULT_INSTANCE = "heroic";
private static final CredentialsBuilder DEFAULT_CREDENTIALS =
new ComputeEngineCredentialsBuilder();
private static final String HITS_TABLE = "hits";
Expand All @@ -56,20 +56,23 @@ public class BigtableAnalyticsModule implements AnalyticsModule {
private static final int DEFAULT_FLUSH_INTERVAL_SECONDS = 2;

private final String project;
private final String cluster;
private final String instance;
private final String profile;
private final CredentialsBuilder credentials;
private final String emulatorEndpoint;
private final int maxPendingReports;

public BigtableAnalyticsModule(
final String project,
final String cluster,
final String instance,
final String profile,
final CredentialsBuilder credentials,
@Nullable final String emulatorEndpoint,
final int maxPendingReports
) {
this.project = project;
this.cluster = cluster;
this.instance = instance;
this.profile = profile;
this.credentials = credentials;
this.emulatorEndpoint = emulatorEndpoint;
this.maxPendingReports = maxPendingReports;
Expand Down Expand Up @@ -97,9 +100,9 @@ public Managed<BigtableConnection> connection(final AsyncFramework async) {
@Override
public AsyncFuture<BigtableConnection> construct() {
return async.call(
new BigtableConnectionBuilder(project, cluster, credentials, emulatorEndpoint,
async, DEFAULT_DISABLE_BULK_MUTATIONS, DEFAULT_FLUSH_INTERVAL_SECONDS,
Optional.empty()));
new BigtableConnectionBuilder(project, instance, profile, credentials,
emulatorEndpoint, async, DEFAULT_DISABLE_BULK_MUTATIONS,
DEFAULT_FLUSH_INTERVAL_SECONDS, Optional.empty()));
}

@Override
Expand Down Expand Up @@ -145,14 +148,15 @@ public static Builder builder() {
}

public String toString() {
return "BigtableAnalyticsModule(project=" + this.project + ", cluster=" + this.cluster
+ ", credentials=" + this.credentials + ", maxPendingReports=" + this.maxPendingReports
+ ")";
return "BigtableAnalyticsModule(project=" + this.project + ", cluster=" + this.instance
+ ", profile=" + this.profile + ", credentials=" + this.credentials
+ ", maxPendingReports=" + this.maxPendingReports + ")";
}

public static class Builder implements AnalyticsModule.Builder {
private Optional<String> project = Optional.empty();
private Optional<String> instance = Optional.empty();
private Optional<String> profile = Optional.empty();
private Optional<CredentialsBuilder> credentials = Optional.empty();
private Optional<String> emulatorEndpoint = Optional.empty();
private Optional<Integer> maxPendingReports = Optional.empty();
Expand All @@ -161,12 +165,14 @@ public static class Builder implements AnalyticsModule.Builder {
public Builder(
@JsonProperty("project") Optional<String> project,
@JsonProperty("instance") Optional<String> instance,
@JsonProperty("profile") Optional<String> profile,
@JsonProperty("credentials") Optional<CredentialsBuilder> credentials,
@JsonProperty("emulatorEndpoint") Optional<String> emulatorEndpoint,
@JsonProperty("maxPendingReports") Optional<Integer> maxPendingReports
) {
this.project = project;
this.instance = instance;
this.profile = profile;
this.credentials = credentials;
this.emulatorEndpoint = emulatorEndpoint;
this.maxPendingReports = maxPendingReports;
Expand All @@ -185,6 +191,11 @@ public Builder instance(String instance) {
return this;
}

public Builder profile(String profile) {
this.profile = Optional.of(profile);
return this;
}

public Builder credentials(CredentialsBuilder credentials) {
this.credentials = Optional.of(credentials);
return this;
Expand All @@ -206,7 +217,8 @@ public BigtableAnalyticsModule build() {

return new BigtableAnalyticsModule(
project,
instance.orElse(DEFAULT_CLUSTER),
instance.orElse(DEFAULT_INSTANCE),
profile.orElse(null),
credentials.orElse(DEFAULT_CREDENTIALS),
emulatorEndpoint.orElse(null),
maxPendingReports.orElse(DEFAULT_MAX_PENDING_REPORTS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public AsyncFuture<Void> close() {

public String toString() {
return "BigtableConnectionBuilder.GrpcBigtableConnection(project=" + this.project
+ ", instance=" + this.instance + ")";
+ ", instance=" + this.instance
+ ", profile=" + this.session.getOptions().getAppProfileId() + ")";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class BigtableConnectionBuilder implements Callable<BigtableConnection> {

private final String project;
private final String instance;
private final String profile;

private final CredentialsBuilder credentials;

Expand All @@ -56,6 +57,7 @@ public class BigtableConnectionBuilder implements Callable<BigtableConnection> {
public BigtableConnectionBuilder(
final String project,
final String instance,
final String profile,
final CredentialsBuilder credentials,
@Nullable final String emulatorEndpoint,
final AsyncFramework async,
Expand All @@ -65,6 +67,7 @@ public BigtableConnectionBuilder(
) {
this.project = project;
this.instance = instance;
this.profile = profile;
this.credentials = credentials;
this.emulatorEndpoint = emulatorEndpoint;
this.async = async;
Expand Down Expand Up @@ -96,6 +99,10 @@ public BigtableConnection call() throws Exception {
.setRetryOptions(retryOptions)
.setBulkOptions(bulkOptions);

if (profile != null) {
builder.setAppProfileId(profile);
}

if (emulatorEndpoint != null) {
builder.enableEmulator(emulatorEndpoint);
}
Expand All @@ -117,6 +124,6 @@ public BigtableConnection call() throws Exception {

public String toString() {
return "BigtableConnectionBuilder(project=" + this.project + ", instance=" + this.instance
+ ", credentials=" + this.credentials + ")";
+ ", profile=" + this.profile + ", credentials=" + this.credentials + ")";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public final class BigtableMetricModule implements MetricModule, DynamicModuleId
private final Groups groups;
private final String project;
private final String instance;
private final String profile;
private final String table;
private final CredentialsBuilder credentials;
private final boolean configure;
Expand All @@ -75,6 +76,7 @@ public BigtableMetricModule(
@JsonProperty("groups") Optional<Groups> groups,
@JsonProperty("project") Optional<String> project,
@JsonProperty("instance") Optional<String> instance,
@JsonProperty("profile") Optional<String> profile,
@JsonProperty("table") Optional<String> table,
@JsonProperty("credentials") Optional<CredentialsBuilder> credentials,
@JsonProperty("configure") Optional<Boolean> configure,
Expand All @@ -87,6 +89,7 @@ public BigtableMetricModule(
this.groups = groups.orElseGet(Groups::empty).or(DEFAULT_GROUP);
this.project = project.orElseThrow(() -> new NullPointerException("project"));
this.instance = instance.orElse(DEFAULT_INSTANCE);
this.profile = profile.orElse(null);
this.table = table.orElse(DEFAULT_TABLE);
this.credentials = credentials.orElse(DEFAULT_CREDENTIALS);
this.configure = configure.orElse(DEFAULT_CONFIGURE);
Expand Down Expand Up @@ -126,7 +129,7 @@ public Managed<BigtableConnection> connection(final AsyncFramework async) {
public AsyncFuture<BigtableConnection> construct() {
return async.call(
new BigtableConnectionBuilder(
project, instance, credentials, emulatorEndpoint,
project, instance, profile, credentials, emulatorEndpoint,
async, disableBulkMutations, flushIntervalSeconds, batchSize));
}

Expand Down Expand Up @@ -188,6 +191,7 @@ public static class Builder {
private Optional<Groups> groups = empty();
private Optional<String> project = empty();
private Optional<String> instance = empty();
private Optional<String> profile = empty();
private Optional<String> table = empty();
private Optional<CredentialsBuilder> credentials = empty();
private Optional<Boolean> configure = empty();
Expand Down Expand Up @@ -216,6 +220,11 @@ public Builder instance(String instance) {
return this;
}

public Builder profile(String profile) {
this.profile = of(profile);
return this;
}

public Builder credentials(CredentialsBuilder credentials) {
this.credentials = of(credentials);
return this;
Expand Down Expand Up @@ -252,8 +261,9 @@ public Builder emulatorEndpoint(final String emulatorEndpoint) {
}

public BigtableMetricModule build() {
return new BigtableMetricModule(id, groups, project, instance, table, credentials,
configure, disableBulkMutations, flushIntervalSeconds, batchSize, emulatorEndpoint);
return new BigtableMetricModule(id, groups, project, instance, profile,
table, credentials, configure, disableBulkMutations, flushIntervalSeconds,
batchSize, emulatorEndpoint);
}
}
}

0 comments on commit 0222c51

Please sign in to comment.