Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

SOLR-16650, SOLR-16532: OTEL tracer additional tags (#1342)

Jan Høydahl72858a8bdd9f
diff --git a/solr/modules/opentelemetry/src/java/org/apache/solr/opentelemetry/OtelTracerConfigurator.java b/solr/modules/opentelemetry/src/java/org/apache/solr/opentelemetry/OtelTracerConfigurator.java
index b6797df3313..6b876c7c1b6 100644
--- a/solr/modules/opentelemetry/src/java/org/apache/solr/opentelemetry/OtelTracerConfigurator.java
+++ b/solr/modules/opentelemetry/src/java/org/apache/solr/opentelemetry/OtelTracerConfigurator.java
@@ -21,6 +21,7 @@ import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
import io.opentracing.Tracer;
import java.lang.invoke.MethodHandles;
+import java.util.Arrays;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
@@ -46,6 +47,7 @@ public class OtelTracerConfigurator extends TracerConfigurator {
setDefaultIfNotConfigured("OTEL_TRACES_EXPORTER", "otlp");
setDefaultIfNotConfigured("OTEL_EXPORTER_OTLP_PROTOCOL", "grpc");
setDefaultIfNotConfigured("OTEL_TRACES_SAMPLER", "parentbased_always_on");
+ addOtelResourceAttributes(Map.of("host.name", System.getProperty("host")));
final String currentConfig = getCurrentOtelConfigAsString();
log.info("OpenTelemetry tracer enabled with configuration: {}", currentConfig);
@@ -66,6 +68,27 @@ public class OtelTracerConfigurator extends TracerConfigurator {
return new ClosableTracerShim(shim, otelSdk.getSdkTracerProvider());
}
+ /**
+ * Add explicit tags statically to all traces, independent of request. Attributes with same name
+ * supplied in ENV or SysProp will take precedence over attributes added in code.
+ */
+ void addOtelResourceAttributes(Map<String, String> attrsToAdd) {
+ String commaSepAttrs = getEnvOrSysprop("OTEL_RESOURCE_ATTRIBUTES");
+ Map<String, String> attrs = new HashMap<>(attrsToAdd);
+ if (commaSepAttrs != null) {
+ attrs.putAll(
+ Arrays.stream(commaSepAttrs.split(","))
+ .filter(e -> e.contains("="))
+ .map(e -> e.strip().split("="))
+ .collect(Collectors.toMap(kv -> kv[0], kv -> kv[1])));
+ }
+ System.setProperty(
+ envNameToSyspropName("OTEL_RESOURCE_ATTRIBUTES"),
+ attrs.entrySet().stream()
+ .map(e -> e.getKey() + "=" + e.getValue())
+ .collect(Collectors.joining(",")));
+ }
+
/** Prepares a string with all configuration K/V pairs sorted and semicolon separated */
String getCurrentOtelConfigAsString() {
return getCurrentOtelConfig().entrySet().stream()
diff --git a/solr/modules/opentelemetry/src/test/org/apache/solr/opentelemetry/OtelTracerConfiguratorTest.java b/solr/modules/opentelemetry/src/test/org/apache/solr/opentelemetry/OtelTracerConfiguratorTest.java
index 4de4eca64ce..54b89d1d521 100644
--- a/solr/modules/opentelemetry/src/test/org/apache/solr/opentelemetry/OtelTracerConfiguratorTest.java
+++ b/solr/modules/opentelemetry/src/test/org/apache/solr/opentelemetry/OtelTracerConfiguratorTest.java
@@ -18,6 +18,7 @@ package org.apache.solr.opentelemetry;
import com.carrotsearch.randomizedtesting.annotations.ThreadLeakLingering;
import io.opentracing.util.GlobalTracer;
+import java.util.List;
import java.util.Map;
import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.cloud.MiniSolrCloudCluster;
@@ -93,6 +94,8 @@ public class OtelTracerConfiguratorTest extends SolrTestCaseJ4 {
@Test
public void testInjected() throws Exception {
+ System.setProperty("otel.resource.attributes", "foo=bar,ILLEGAL-LACKS-VALUE,");
+ System.setProperty("host", "my.solr.host");
// Make sure the batch exporter times out before our thread lingering time of 10s
instance.setDefaultIfNotConfigured("OTEL_BSP_SCHEDULE_DELAY", "1000");
instance.setDefaultIfNotConfigured("OTEL_BSP_EXPORT_TIMEOUT", "2000");
@@ -105,8 +108,13 @@ public class OtelTracerConfiguratorTest extends SolrTestCaseJ4 {
assertTrue(
"Tracer shim not registered with GlobalTracer",
GlobalTracer.get().toString().contains("ClosableTracerShim"));
+ assertEquals(
+ List.of("host.name=my.solr.host", "foo=bar"),
+ List.of(System.getProperty("otel.resource.attributes").split(",")));
} finally {
cluster.shutdown();
+ System.clearProperty("otel.resource.attributes");
+ System.clearProperty("host");
}
}
}
diff --git a/solr/solr-ref-guide/modules/deployment-guide/pages/distributed-tracing.adoc b/solr/solr-ref-guide/modules/deployment-guide/pages/distributed-tracing.adoc
index 2dd013ff1d7..e51d7fbed15 100644
--- a/solr/solr-ref-guide/modules/deployment-guide/pages/distributed-tracing.adoc
+++ b/solr/solr-ref-guide/modules/deployment-guide/pages/distributed-tracing.adoc
@@ -88,6 +88,13 @@ An equivalent configuration using system properties would be:
SOLR_OPTS=-Dotel.exporter.otlp.endpoint=my-remote-collector:4317 -Dotel.traces.sampler=parentbased_traceidratio -Dotel.traces.sampler.arg=0.1
----
+To add custom tags to the trace, use `OTEL_RESOURCE_ATTRIBUTES`:
+
+[source,bash]
+----
+OTEL_RESOURCE_ATTRIBUTES="application=OnlineBanking,exampleKey=exampleValue"
+----
+
Consult the https://opentelemetry.io/docs/reference/specification/sdk-environment-variables/[OTEL documentation] for details on all the configuration options.
This version of Solr uses https://github.com/open-telemetry/opentelemetry-java/tree/v{dep-version-opentelemetry}[OpenTelemetry SDK v{dep-version-opentelemetry}].