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

gcp-observability: add custom tags for all 3 - metrics, logging, traces and remove old env-vars impl #9402

Merged
merged 4 commits into from Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -69,7 +69,7 @@ public static synchronized GcpObservability grpcInit() throws IOException {
GlobalLoggingTags globalLoggingTags = new GlobalLoggingTags();
ObservabilityConfigImpl observabilityConfig = ObservabilityConfigImpl.getInstance();
Sink sink = new GcpLogSink(observabilityConfig.getDestinationProjectId(),
globalLoggingTags.getLocationTags(), globalLoggingTags.getCustomTags(),
globalLoggingTags.getLocationTags(), observabilityConfig.getCustomTags(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only adds custom tags for logging. Don't we need to add same set of custom tags for metrics and traces as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, but that will be a separate PR (as per the task list we have created)

observabilityConfig.getFlushMessageCount());
// TODO(dnvindhya): Cleanup code for LoggingChannelProvider and LoggingServerProvider
// once ChannelBuilder and ServerBuilder are used
Expand Down
Expand Up @@ -41,14 +41,11 @@ final class GlobalLoggingTags {

private static final String ENV_KEY_PREFIX = "GRPC_OBSERVABILITY_";
private final Map<String, String> locationTags;
private final Map<String, String> customTags;

GlobalLoggingTags() {
ImmutableMap.Builder<String, String> locationTagsBuilder = ImmutableMap.builder();
ImmutableMap.Builder<String, String> customTagsBuilder = ImmutableMap.builder();
populate(locationTagsBuilder, customTagsBuilder);
populate(locationTagsBuilder);
locationTags = locationTagsBuilder.buildOrThrow();
customTags = customTagsBuilder.buildOrThrow();
}

private static String applyTrim(String value) {
Expand All @@ -62,10 +59,6 @@ Map<String, String> getLocationTags() {
return locationTags;
}

Map<String, String> getCustomTags() {
return customTags;
}

@VisibleForTesting
static void populateFromMetadataServer(ImmutableMap.Builder<String, String> customTags) {
sanjaypujare marked this conversation as resolved.
Show resolved Hide resolved
MetadataConfig metadataConfig = new MetadataConfig(new DefaultHttpTransportFactory());
Expand Down Expand Up @@ -139,10 +132,6 @@ private static String readFileContents(String file) {
return null;
}

private static void populateFromEnvironmentVars(ImmutableMap.Builder<String, String> customTags) {
populateFromMap(System.getenv(), customTags);
}

@VisibleForTesting
static void populateFromMap(Map<String, String> map,
sanjaypujare marked this conversation as resolved.
Show resolved Hide resolved
final ImmutableMap.Builder<String, String> customTags) {
Expand All @@ -155,9 +144,7 @@ static void populateFromMap(Map<String, String> map,
});
}

static void populate(ImmutableMap.Builder<String, String> locationTags,
ImmutableMap.Builder<String, String> customTags) {
populateFromEnvironmentVars(customTags);
static void populate(ImmutableMap.Builder<String, String> locationTags) {
populateFromMetadataServer(locationTags);
populateFromKubernetesValues(locationTags,
"/var/run/secrets/kubernetes.io/serviceaccount/namespace",
Expand Down
Expand Up @@ -20,6 +20,7 @@
import io.grpc.observabilitylog.v1.GrpcLogRecord.EventType;
import io.opencensus.trace.Sampler;
import java.util.List;
import java.util.Map;

@Internal
public interface ObservabilityConfig {
Expand Down Expand Up @@ -47,6 +48,9 @@ public interface ObservabilityConfig {
/** Get sampler for TraceConfig - when Cloud Tracing is enabled. */
Sampler getSampler();

/** Map of all custom tags used for logging, metrics and traces. */
Map<String, String> getCustomTags();

/**
* POJO for representing a filter used in configuration.
*/
Expand Down
Expand Up @@ -20,6 +20,7 @@

import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import io.grpc.internal.JsonParser;
import io.grpc.internal.JsonUtil;
import io.grpc.observabilitylog.v1.GrpcLogRecord.EventType;
Expand Down Expand Up @@ -48,6 +49,7 @@ final class ObservabilityConfigImpl implements ObservabilityConfig {
private List<LogFilter> logFilters;
private List<EventType> eventTypes;
private Sampler sampler;
private Map<String, String> customTags;

static ObservabilityConfigImpl getInstance() throws IOException {
ObservabilityConfigImpl config = new ObservabilityConfigImpl();
Expand Down Expand Up @@ -120,6 +122,17 @@ private void parseConfig(Map<String, ?> config) {
this.sampler = Samplers.probabilitySampler(samplingRate);
}
}
Map<String, ?> rawCustomTags = JsonUtil.getObject(config, "custom_tags");
if (rawCustomTags != null) {
ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<>();
for (Map.Entry<String, ?> entry: rawCustomTags.entrySet()) {
checkArgument(
entry.getValue() instanceof String,
"'custom_tags' needs to be a map of <string, string>");
builder.put(entry.getKey(), (String) entry.getValue());
}
customTags = builder.build();
}
}
}

Expand Down Expand Up @@ -191,4 +204,9 @@ public List<EventType> getEventTypes() {
public Sampler getSampler() {
return sampler;
}

@Override
public Map<String, String> getCustomTags() {
return customTags;
}
}
Expand Up @@ -33,6 +33,7 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;

import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -106,6 +107,23 @@ public class ObservabilityConfigImplTest {
+ " \"global_trace_sampling_rate\": -0.75\n"
+ "}";

private static final String CUSTOM_TAGS = "{\n"
+ " \"enable_cloud_logging\": true,\n"
+ " \"custom_tags\": {\n"
+ " \"SOURCE_VERSION\" : \"J2e1Cf\",\n"
+ " \"SERVICE_NAME\" : \"payment-service\",\n"
+ " \"ENTRYPOINT_SCRIPT\" : \"entrypoint.sh\"\n"
+ " }\n"
+ "}";

private static final String BAD_CUSTOM_TAGS = "{\n"
+ " \"enable_cloud_monitoring\": true,\n"
+ " \"custom_tags\": {\n"
+ " \"SOURCE_VERSION\" : \"J2e1Cf\",\n"
+ " \"SERVICE_NAME\" : { \"SUB_SERVICE_NAME\" : \"payment-service\"},\n"
+ " \"ENTRYPOINT_SCRIPT\" : \"entrypoint.sh\"\n"
+ " }\n"
+ "}";

ObservabilityConfigImpl observabilityConfig = new ObservabilityConfigImpl();

Expand Down Expand Up @@ -257,4 +275,26 @@ public void configFileLogFilters() throws Exception {
assertThat(logFilters.get(1).headerBytes).isNull();
assertThat(logFilters.get(1).messageBytes).isNull();
}

@Test
public void customTags() throws IOException {
observabilityConfig.parse(CUSTOM_TAGS);
assertTrue(observabilityConfig.isEnableCloudLogging());
Map<String, String> customTags = observabilityConfig.getCustomTags();
assertThat(customTags).hasSize(3);
assertThat(customTags).containsEntry("SOURCE_VERSION", "J2e1Cf");
assertThat(customTags).containsEntry("SERVICE_NAME", "payment-service");
assertThat(customTags).containsEntry("ENTRYPOINT_SCRIPT", "entrypoint.sh");
}

@Test
public void badCustomTags() throws IOException {
try {
observabilityConfig.parse(BAD_CUSTOM_TAGS);
fail("exception expected!");
} catch (IllegalArgumentException iae) {
assertThat(iae.getMessage()).isEqualTo(
"'custom_tags' needs to be a map of <string, string>");
}
}
}