Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stackdriver - Ability to configure credentials #1682

Merged
merged 4 commits into from
Jan 17, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,16 @@
*/
package io.micrometer.stackdriver;

import com.google.api.gax.core.CredentialsProvider;
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.monitoring.v3.MetricServiceSettings;
import io.micrometer.core.instrument.config.MissingRequiredConfigurationException;
import io.micrometer.core.instrument.step.StepRegistryConfig;

import java.io.FileInputStream;
import java.io.IOException;

/**
* {@link StepRegistryConfig} for Stackdriver.
*
Expand All @@ -42,4 +49,11 @@ default String resourceType() {
String resourceType = get(prefix() + ".resourceType");
return resourceType == null ? "global" : resourceType;
}

default CredentialsProvider credentials() throws IOException {
String credentials = get(prefix() + ".credentials");
return credentials == null ? MetricServiceSettings.defaultCredentialsProviderBuilder().build()
: FixedCredentialsProvider.create(GoogleCredentials.fromStream(new FileInputStream(credentials))
.createScoped(MetricServiceSettings.getDefaultServiceScopes()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ public class StackdriverMeterRegistry extends StepMeterRegistry {
@Nullable
private MetricServiceClient client;

public StackdriverMeterRegistry(StackdriverConfig config, Clock clock) {
this(config, clock, DEFAULT_THREAD_FACTORY, () -> MetricServiceSettings.newBuilder().build());
protected StackdriverMeterRegistry(StackdriverConfig config, Clock clock) {
shakuzen marked this conversation as resolved.
Show resolved Hide resolved
this(config, clock, DEFAULT_THREAD_FACTORY, () -> MetricServiceSettings.newBuilder().build());
}

private StackdriverMeterRegistry(StackdriverConfig config, Clock clock, ThreadFactory threadFactory,
Expand Down Expand Up @@ -256,6 +256,14 @@ public static class Builder {

Builder(StackdriverConfig config) {
this.config = config;
this.metricServiceSettings = () -> {
MetricServiceSettings.Builder builder = MetricServiceSettings.newBuilder();
if (config.credentials() != null) {
builder.setCredentialsProvider(config.credentials());
}
builder.setHeaderProvider(new UserAgentHeaderProvider("stackdriver"));
return builder.build();
};
}

public Builder clock(Clock clock) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* Copyright 2019 Pivotal Software, Inc.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micrometer.stackdriver;

import com.google.api.gax.rpc.HeaderProvider;

import java.util.Collections;
import java.util.Map;

/**
* Provides the User-Agent header to signal to the Google Cloud Client Libraries that requests originate from a
* Micrometer Stackdriver Registry.
*
* @author João André Martins
* @author Chengyuan Zhao
* @author Mike Eltsufin
* @author Ray Tsang
*/
public class UserAgentHeaderProvider implements HeaderProvider {

private String userAgent;

private final Map<String, String> headers;

/**
* Default constructor.
*
* @param component The component
*/
public UserAgentHeaderProvider(String component) {
this.userAgent = computeUserAgent(component);
this.headers = Collections.singletonMap("User-Agent", this.userAgent);
}

/**
* Returns the "User-Agent" header whose value should be added to the google-cloud-java REST API calls.
* e.g., {@code User-Agent: Micrometer/1.0.0.RELEASE micrometer-registry-stackdriver/1.0.0.RELEASE}.
*/
@Override
public Map<String, String> getHeaders() {
return this.headers;
}

/**
* Returns the "User-Agent" header value which should be added to the google-cloud-java calls.
* e.g., {@code Micrometer/1.0.0.RELEASE micrometer-registry-stackdriver/1.0.0.RELEASE}.
*
* @return the user agent string.
*/
public String getUserAgent() {
return this.userAgent;
}

private String computeUserAgent(String component) {
String library = "micrometer-registry-" + component;
String version = this.getClass().getPackage().getImplementationVersion();

return "Micrometer/" + version + " " + library + "/" + version;

}
}