Skip to content

Commit

Permalink
zero config: add disable svcname generation capability (#2410)
Browse files Browse the repository at this point in the history
Setting service name can be disabled by setting `generate_service_name=false` in
the config file. In a later release service name generation will be disabled by
default.
  • Loading branch information
pmcollins committed Jan 3, 2023
1 parent 9bf4ad1 commit 4db006c
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 48 deletions.
1 change: 1 addition & 0 deletions instrumentation/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ WORKDIR /libsplunk

COPY src /libsplunk/src
COPY testdata/instrumentation.conf /libsplunk/testdata/instrumentation.conf
COPY testdata/instrumentation-svcname.conf /libsplunk/testdata/instrumentation-svcname.conf
COPY install/instrumentation.conf /libsplunk/install/instrumentation.conf
COPY Makefile /libsplunk/Makefile
8 changes: 7 additions & 1 deletion instrumentation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ This directory contains functionality for building a Linux .so (shared object) f
reference to that file in
`/etc/ld.so.preload` (provided by an installer defined elsewhere) causes processes on the host to run this .so before
the main executable runs. If the executable that's starting is not named `java`, the .so quits silently and the
executable starts normally. Otherwise, it attempts to set two environment variables that will cause the
executable starts normally. Otherwise, it attempts to set environment variables that will cause the
[Splunk OTel Java JAR](https://github.com/signalfx/splunk-otel-java) (also provided by the installer) to instrument the
soon-to-be running Java application. In this way, all Java applications on the host will be automatically instrumented
by the Splunk OTel Java agent.
Expand All @@ -35,6 +35,12 @@ The `java_agent_jar` parameter is set to the default location of the agent jar.

The full path to the auto instrumentation JAR (provided by the installer).

#### `generate_service_name` (optional)

Whether to disable service name generation by the .so. If set to "true" (the default), the .so will attempt to generate
a service name from the Java command arguments. If set to "false", it will not set the service name, leaving it to be
generated by the Java auto-instrumentation library.

#### `service_name` (optional)

This is an optional override for the service name that would otherwise be generated by the shared object before Java
Expand Down
14 changes: 13 additions & 1 deletion instrumentation/src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ void load_config(logger log, struct config *cfg, char *file_name) {
if (cfg->disable_telemetry == NULL) {
log_debug(log, "disable_telemetry not specified in config");
}
if (cfg->generate_service_name == NULL) {
log_debug(log, "generate_service_name not specified in config");
}
}

void read_config_file(logger log, struct config *cfg, char *file_name) {
Expand Down Expand Up @@ -63,6 +66,8 @@ void read_lines(struct config *cfg, FILE *fp) {
cfg->resource_attributes = strdup(pair.v);
} else if (streq(pair.k, "disable_telemetry")) {
cfg->disable_telemetry = strdup(pair.v);
} else if (streq(pair.k, "generate_service_name")) {
cfg->generate_service_name = strdup(pair.v);
}
}
}
Expand All @@ -72,10 +77,14 @@ void split_on_eq(char *string, struct kv *pair) {
pair->v = string;
}

bool eq_true(char *v) {
bool str_eq_true(char *v) {
return v != NULL && !streq("false", v) && !streq("FALSE", v) && !streq("0", v);
}

bool str_eq_false(char *v) {
return v != NULL && (streq("false", v) || streq("FALSE", v) || streq("0", v));
}

void free_config(struct config *cfg) {
if (cfg->java_agent_jar != NULL) {
free(cfg->java_agent_jar);
Expand All @@ -89,4 +98,7 @@ void free_config(struct config *cfg) {
if (cfg->disable_telemetry != NULL) {
free(cfg->disable_telemetry);
}
if (cfg->generate_service_name != NULL) {
free(cfg->generate_service_name);
}
}
5 changes: 4 additions & 1 deletion instrumentation/src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ struct config {
char *service_name;
char *resource_attributes;
char *disable_telemetry;
char *generate_service_name;
};

void load_config(logger log, struct config *cfg, char *file_name);

bool eq_true(char *v);
bool str_eq_true(char *v);

bool str_eq_false(char *v);

void free_config(struct config *cfg);

Expand Down
30 changes: 19 additions & 11 deletions instrumentation/src/splunk.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ void set_env_var(logger log, const char *var_name, const char *value);

void set_env_var_from_attr(logger log, const char *attr_name, const char *env_var_name, const char *value);

void get_service_name(logger log, cmdline_reader cr, struct config *cfg, char *dest);

// The entry point for all executables prior to their execution. If the executable is named "java", we
// set the env vars JAVA_TOOL_OPTIONS to the path of the java agent jar and OTEL_SERVICE_NAME to the
// service name based on the arguments to the java command.
Expand Down Expand Up @@ -85,24 +87,22 @@ void auto_instrument(
}

char service_name[MAX_CMDLINE_LEN] = "";
if (cfg.service_name == NULL) {
get_service_name_from_cmdline(log, service_name, cr);
if (str_eq_false(cfg.generate_service_name)) {
log_debug(log, "service name generation disabled");
} else {
strncpy(service_name, cfg.service_name, MAX_CMDLINE_LEN);
get_service_name(log, cr, &cfg, service_name);
if (strlen(service_name) == 0) {
log_info(log, "service name empty, quitting");
return;
}
set_env_var(log, otel_service_name_var, service_name);
}

if (strlen(service_name) == 0) {
log_info(log, "service name empty, quitting");
return;
}

set_env_var(log, otel_service_name_var, service_name);

set_java_tool_options(log, &cfg);

set_env_var_from_attr(log, "resource_attributes", resource_attributes_var, cfg.resource_attributes);

if (eq_true(cfg.disable_telemetry)) {
if (str_eq_true(cfg.disable_telemetry)) {
log_info(log, "disabling telemetry as per config");
} else {
send_otlp_metric_func(log, service_name);
Expand All @@ -111,6 +111,14 @@ void auto_instrument(
free_config(&cfg);
}

void get_service_name(logger log, cmdline_reader cr, struct config *cfg, char *dest) {
if (cfg->service_name == NULL) {
get_service_name_from_cmdline(log, dest, cr);
} else {
strncpy(dest, (*cfg).service_name, MAX_CMDLINE_LEN);
}
}

void get_service_name_from_cmdline(logger log, char *dest, cmdline_reader cr) {
char *args[MAX_ARGS];
int n = get_cmdline_args(args, cr, MAX_ARGS, MAX_CMDLINE_LEN, log);
Expand Down
Loading

0 comments on commit 4db006c

Please sign in to comment.