Skip to content

Commit

Permalink
Merge pull request #7 from lightstep/OTEL_RESOURCE_LABELS
Browse files Browse the repository at this point in the history
Add OTEL_RESOURCE_LABELS
  • Loading branch information
carlosalberto authored Jul 30, 2020
2 parents 8bcd523 + f198a5f commit f875168
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Supported system properties and environmental variables:
| otel.exporter.otlp.span.insecure | OTEL_EXPORTER_OTLP_SPAN_INSECURE | Use insecure transport or not | false |
| otel.propagators | OTEL_PROPAGATORS | Propagator | b3 |
| otel.log.level | OTEL_LOG_LEVEL | Log level for agent | info |
| otel.resource.labels | OTEL_RESOURCE_LABELS | Comma separated key-value pairs | |

## Agent
The Lightstep OpenTelemetry Agent is a configuration layer over OpenTelemetry Instrumentation Agent.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class VariablesConverter {
static final String OTEL_PROPAGATORS = "OTEL_PROPAGATORS";
static final String OTEL_EXPORTER_OTLP_SPAN_INSECURE = "OTEL_EXPORTER_OTLP_SPAN_INSECURE";
static final String OTEL_LOG_LEVEL = "OTEL_LOG_LEVEL";
static final String OTEL_RESOURCE_LABELS = "OTEL_RESOURCE_LABELS";
static final String OTEL_RESOURCE_ATTRIBUTES = "OTEL_RESOURCE_ATTRIBUTES";

public static void setSystemProperties(String spanEndpoint,
Expand All @@ -28,6 +29,7 @@ public static void setSystemProperties(String spanEndpoint,
String logLevel,
String serviceName,
String serviceVersion,
String resourceLabels,
boolean isAgent) {

if (serviceName == null || serviceName.isEmpty()) {
Expand Down Expand Up @@ -76,6 +78,9 @@ public static void setSystemProperties(String spanEndpoint,
if (serviceVersion != null) {
otelResourceAttributes += ",service.version=" + serviceVersion;
}
if (resourceLabels != null && !resourceLabels.isEmpty()) {
otelResourceAttributes += "," + resourceLabels;
}
String envResourceAttributes = getResourceAttributes();
if (envResourceAttributes != null && !envResourceAttributes.isEmpty()) {
// Keep the existing env Resource attributes, if any.
Expand All @@ -96,7 +101,7 @@ static boolean isTokenRequired(String spanEndpoint) {
public static void convertFromEnv() {
setSystemProperties(getSpanEndpoint(), useInsecureTransport(),
getAccessToken(), getPropagator(), getLogLevel(), getServiceName(), getServiceVersion(),
true);
getResourceLabels(), true);
}

public static String getAccessToken() {
Expand All @@ -111,6 +116,10 @@ public static String getServiceVersion() {
return getProperty(LS_SERVICE_VERSION, null);
}

public static String getResourceLabels() {
return getProperty(OTEL_RESOURCE_LABELS, null);
}

public static String getLogLevel() {
return getProperty(OTEL_LOG_LEVEL, DEFAULT_OTEL_LOG_LEVEL);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ public class VariablesConverterTest {

@Before
public void before() {
System.clearProperty(toSystemProperty(VariablesConverter.OTEL_LOG_LEVEL));
System.clearProperty(toSystemProperty(VariablesConverter.LS_ACCESS_TOKEN));
System.clearProperty(toSystemProperty(VariablesConverter.LS_SERVICE_NAME));
System.clearProperty(toSystemProperty(VariablesConverter.LS_SERVICE_VERSION));
System.clearProperty(toSystemProperty(VariablesConverter.OTEL_LOG_LEVEL));
System.clearProperty(toSystemProperty(VariablesConverter.OTEL_EXPORTER_OTLP_SPAN_ENDPOINT));
System.clearProperty(toSystemProperty(VariablesConverter.OTEL_PROPAGATORS));
System.clearProperty(toSystemProperty(VariablesConverter.OTEL_EXPORTER_OTLP_SPAN_INSECURE));
System.clearProperty(toSystemProperty(VariablesConverter.LS_SERVICE_NAME));
System.clearProperty(toSystemProperty(VariablesConverter.LS_SERVICE_VERSION));
System.clearProperty(toSystemProperty(VariablesConverter.OTEL_RESOURCE_ATTRIBUTES));
System.clearProperty(toSystemProperty(VariablesConverter.OTEL_RESOURCE_LABELS));
}

@Test
Expand Down Expand Up @@ -192,22 +194,42 @@ public void useInsecureTransport_fromEnvVariable() {

@Test
public void getResourceAttributes_Default() {
assertEquals(null, VariablesConverter.getResourceAttributes());
assertNull(VariablesConverter.getResourceAttributes());
}

@Test
public void getResourceAttributes_fromSystemProperty() {
System.setProperty(toSystemProperty(VariablesConverter.OTEL_RESOURCE_ATTRIBUTES), "key1=value1");
System
.setProperty(toSystemProperty(VariablesConverter.OTEL_RESOURCE_ATTRIBUTES), "key1=value1");
assertEquals("key1=value1", VariablesConverter.getResourceAttributes());
}

@Test
public void getResourceAttributes_fromEnvVariable() {
mockSystem();
Mockito.when(System.getenv(VariablesConverter.OTEL_RESOURCE_ATTRIBUTES)).thenReturn("key1=value1");
Mockito.when(System.getenv(VariablesConverter.OTEL_RESOURCE_ATTRIBUTES))
.thenReturn("key1=value1");
assertEquals("key1=value1", VariablesConverter.getResourceAttributes());
}

@Test
public void getResourceLabels_Default() {
assertNull(VariablesConverter.getResourceLabels());
}

@Test
public void getResourceLabels_fromSystemProperty() {
System.setProperty(toSystemProperty(VariablesConverter.OTEL_RESOURCE_LABELS), "key1=value1");
assertEquals("key1=value1", VariablesConverter.getResourceLabels());
}

@Test
public void getResourceLabels_fromEnvVariable() {
mockSystem();
Mockito.when(System.getenv(VariablesConverter.OTEL_RESOURCE_LABELS)).thenReturn("key1=value1");
assertEquals("key1=value1", VariablesConverter.getResourceLabels());
}

private void mockSystem() {
PowerMockito.mockStatic(System.class);
Mockito.when(System.getProperty(anyString(), anyString())).thenAnswer(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public static class Builder {
private String serviceName;
private String serviceVersion;
private String spanEndpoint;
private String resourceLabels;
private boolean insecureTransport;
private Propagator propagator;

Expand Down Expand Up @@ -88,7 +89,7 @@ public Builder useInsecureTransport(boolean insecureTransport) {
public OtlpGrpcSpanExporter buildExporter() {
VariablesConverter
.setSystemProperties(spanEndpoint, insecureTransport, accessToken, null, null,
serviceName, serviceVersion, false);
serviceName, serviceVersion, resourceLabels, false);

if (propagator != null) {
final HttpTextFormat httpTextFormat = PROPAGATORS.get(propagator);
Expand Down Expand Up @@ -118,6 +119,7 @@ private void readEnvVariablesAndSystemProperties() {
this.insecureTransport = VariablesConverter.useInsecureTransport();
this.spanEndpoint = VariablesConverter.getSpanEndpoint();
this.propagator = Propagator.valueOfLabel(VariablesConverter.getPropagator());
this.resourceLabels = VariablesConverter.getResourceLabels();
}

}
Expand Down

0 comments on commit f875168

Please sign in to comment.