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
gcp-observability: add custom tags for all 3 - metrics, logging, traces and remove old env-vars impl #9402
Changes from 3 commits
089f9a7
b527faa
c74ad0f
b3c7c43
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,8 +16,6 @@ | |
|
||
package io.grpc.gcp.observability; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
import com.google.api.client.http.HttpTransport; | ||
import com.google.api.client.http.javanet.NetHttpTransport; | ||
import com.google.api.client.util.Strings; | ||
|
@@ -35,20 +33,16 @@ | |
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
/** A container of all global custom tags used for logging (for now). */ | ||
/** A container of all global location tags used for observability. */ | ||
final class GlobalLoggingTags { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the class specifically contains "location" tags, should the class be called GlobalLocationTags? Or more types of tags to be added? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point. Renamed everywhere. |
||
private static final Logger logger = Logger.getLogger(GlobalLoggingTags.class.getName()); | ||
|
||
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) { | ||
|
@@ -62,40 +56,36 @@ Map<String, String> getLocationTags() { | |
return locationTags; | ||
} | ||
|
||
Map<String, String> getCustomTags() { | ||
return customTags; | ||
} | ||
|
||
@VisibleForTesting | ||
static void populateFromMetadataServer(ImmutableMap.Builder<String, String> customTags) { | ||
static void populateFromMetadataServer(ImmutableMap.Builder<String, String> locationTags) { | ||
MetadataConfig metadataConfig = new MetadataConfig(new DefaultHttpTransportFactory()); | ||
metadataConfig.init(); | ||
customTags.putAll(metadataConfig.getAllValues()); | ||
locationTags.putAll(metadataConfig.getAllValues()); | ||
} | ||
|
||
@VisibleForTesting | ||
static void populateFromKubernetesValues(ImmutableMap.Builder<String, String> customTags, | ||
static void populateFromKubernetesValues(ImmutableMap.Builder<String, String> locationTags, | ||
String namespaceFile, | ||
String hostnameFile, String cgroupFile) { | ||
// namespace name: contents of file /var/run/secrets/kubernetes.io/serviceaccount/namespace | ||
populateFromFileContents(customTags, "namespace_name", | ||
populateFromFileContents(locationTags, "namespace_name", | ||
namespaceFile, GlobalLoggingTags::applyTrim); | ||
|
||
// pod_name: hostname i.e. contents of /etc/hostname | ||
populateFromFileContents(customTags, "pod_name", hostnameFile, | ||
populateFromFileContents(locationTags, "pod_name", hostnameFile, | ||
GlobalLoggingTags::applyTrim); | ||
|
||
// container_id: parsed from /proc/self/cgroup . Note: only works for Linux-based containers | ||
populateFromFileContents(customTags, "container_id", cgroupFile, | ||
populateFromFileContents(locationTags, "container_id", cgroupFile, | ||
(value) -> getContainerIdFromFileContents(value)); | ||
} | ||
|
||
@VisibleForTesting | ||
static void populateFromFileContents(ImmutableMap.Builder<String, String> customTags, String key, | ||
String filePath, Function<String, String> parser) { | ||
static void populateFromFileContents(ImmutableMap.Builder<String, String> locationTags, | ||
String key, String filePath, Function<String, String> parser) { | ||
String value = parser.apply(readFileContents(filePath)); | ||
if (value != null) { | ||
customTags.put(key, value); | ||
locationTags.put(key, value); | ||
} | ||
} | ||
|
||
|
@@ -139,25 +129,7 @@ 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, | ||
final ImmutableMap.Builder<String, String> customTags) { | ||
checkNotNull(map); | ||
map.forEach((k, v) -> { | ||
if (k.startsWith(ENV_KEY_PREFIX)) { | ||
String customTagKey = k.substring(ENV_KEY_PREFIX.length()); | ||
customTags.put(customTagKey, v); | ||
} | ||
}); | ||
} | ||
|
||
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", | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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)