Skip to content

Commit

Permalink
Set 'env' to 'none' if not set or empty
Browse files Browse the repository at this point in the history
  • Loading branch information
nikita-tkachenko-datadog committed Feb 19, 2023
1 parent 17ed007 commit 0293a1b
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ private void process(DDSpan span) {
span.setResourceName(span.getOperationName());
}

if (span.getTag("env") != null) {
span.setTag("env", TraceUtils.normalizeEnv((String) span.getTag("env")));
}
span.setTag("env", TraceUtils.normalizeEnv((String) span.getTag("env")));

final short httpStatusCode = span.getHttpStatusCode();
if (httpStatusCode != 0 && !isValidStatusCode(httpStatusCode)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,18 @@ class DDIntakeTraceInterceptorTest extends DDCoreSpecification {
def span = trace[0]
span.type == originalSpanType
}

def "test default env setting"() {
setup:
tracer.buildSpan("my-operation-name").start().finish()
writer.waitForTraces(1)

expect:
def trace = writer.firstTrace()
trace.size() == 1

def span = trace[0]

span.getTag("env") == "none"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class TraceUtils {

static final String DEFAULT_SERVICE_NAME = "unnamed-service";
static final String DEFAULT_OPERATION_NAME = "unnamed_operation";
static final String DEFAULT_ENV = "none";

private static final Logger log = LoggerFactory.getLogger(TraceUtils.class);

Expand Down Expand Up @@ -73,7 +74,7 @@ public static CharSequence normalizeSpanType(final CharSequence spanType) {

public static String normalizeEnv(final String env) {
if (env == null || env.length() == 0) {
return "";
return DEFAULT_ENV;
}

String e = truncate(env, MAX_ENV_LEN);
Expand Down
130 changes: 65 additions & 65 deletions internal-api/src/test/groovy/datadog/trace/util/TraceUtilsTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ class TraceUtilsTest extends DDSpecification {
normalized == expected

where:
service | expected
null | TraceUtils.DEFAULT_SERVICE_NAME
"" | TraceUtils.DEFAULT_SERVICE_NAME
"good" | "good"
"bad\$service" | "bad_service"
service | expected
null | TraceUtils.DEFAULT_SERVICE_NAME
"" | TraceUtils.DEFAULT_SERVICE_NAME
"good" | "good"
"bad\$service" | "bad_service"
"Too\$Long\$.Too\$Long\$.Too\$Long\$.Too\$Long\$.Too\$Long\$.Too\$Long\$.Too\$Long\$.Too\$Long\$.Too\$Long\$.Too\$Long\$.Too\$Long\$." | "too_long_.too_long_.too_long_.too_long_.too_long_.too_long_.too_long_.too_long_.too_long_.too_long_."
}

Expand All @@ -28,20 +28,20 @@ class TraceUtilsTest extends DDSpecification {
normalized == expected

where:
name | expected
null | TraceUtils.DEFAULT_OPERATION_NAME
"" | TraceUtils.DEFAULT_OPERATION_NAME
"good" | "good"
"bad-name" | "bad_name"
name | expected
null | TraceUtils.DEFAULT_OPERATION_NAME
"" | TraceUtils.DEFAULT_OPERATION_NAME
"good" | "good"
"bad-name" | "bad_name"
"Too-Long-.Too-Long-.Too-Long-.Too-Long-.Too-Long-.Too-Long-.Too-Long-.Too-Long-.Too-Long-.Too-Long-.Too-Long-." | "Too_Long.Too_Long.Too_Long.Too_Long.Too_Long.Too_Long.Too_Long.Too_Long.Too_Long.Too_Long."
"pylons.controller"|"pylons.controller"
"trace-api.request"|"trace_api.request"
"/"|"unnamed_operation"
"{çà]test"|"test"
"l___."|"l."
"a___b"|"a_b"
"a___"|"a"
"🐨🐶 繋"|"unnamed_operation"
"pylons.controller" | "pylons.controller"
"trace-api.request" | "trace_api.request"
"/" | "unnamed_operation"
"{çà]test" | "test"
"l___." | "l."
"a___b" | "a_b"
"a___" | "a"
"🐨🐶 繋" | "unnamed_operation"
}


Expand All @@ -53,40 +53,40 @@ class TraceUtilsTest extends DDSpecification {
normalized == expected

where:
tag | expected
null | ""
"" | ""
"ok" | "ok"
" " | ""
"#test_starting_hash"|"test_starting_hash"
"TestCAPSandSuch" | "testcapsandsuch"
tag | expected
null | ""
"" | ""
"ok" | "ok"
" " | ""
"#test_starting_hash" | "test_starting_hash"
"TestCAPSandSuch" | "testcapsandsuch"
"Test Conversion Of Weird !@#\$%^&**() Characters" | "test_conversion_of_weird_characters"
"\$#weird_starting" | "weird_starting"
"allowed:c0l0ns" |"allowed:c0l0ns"
"1love" | "love"
"ünicöde" | "ünicöde"
"ünicöde:metäl"| "ünicöde:metäl"
"Data🐨dog🐶 繋がっ⛰てて"| "data_dog_繋がっ_てて"
" spaces "| "spaces"
" #hashtag!@#spaces #__<># "|"hashtag_spaces"
":testing"|":testing"
"_foo"|"foo"
":::test"| ":::test"
"contiguous_____underscores"| "contiguous_underscores"
"foo_"| "foo"
"\u017Fodd_\u017Fcase\u017F"| "\u017Fodd_\u017Fcase\u017F"
"™Ö™Ö™™Ö™"| "ö_ö_ö"
"AlsO:ök"| "also:ök"
":still_ok"| ":still_ok"
"___trim"| "trim"
"12.:trim@"| ":trim"
"12.:trim@@"| ":trim"
"fun:ky__tag/1"| "fun:ky_tag/1"
"fun:ky@tag/2"| "fun:ky_tag/2"
"fun:ky@@@tag/3"| "fun:ky_tag/3"
"tag:1/2.3"| "tag:1/2.3"
"---fun:k####y_ta@#g/1_@@#"|"fun:k_y_ta_g/1"
"AlsO:œ#@ö))œk"|"also:œ_ö_œk"
"\$#weird_starting" | "weird_starting"
"allowed:c0l0ns" | "allowed:c0l0ns"
"1love" | "love"
"ünicöde" | "ünicöde"
"ünicöde:metäl" | "ünicöde:metäl"
"Data🐨dog🐶 繋がっ⛰てて" | "data_dog_繋がっ_てて"
" spaces " | "spaces"
" #hashtag!@#spaces #__<># " | "hashtag_spaces"
":testing" | ":testing"
"_foo" | "foo"
":::test" | ":::test"
"contiguous_____underscores" | "contiguous_underscores"
"foo_" | "foo"
"\u017Fodd_\u017Fcase\u017F" | "\u017Fodd_\u017Fcase\u017F"
"™Ö™Ö™™Ö™" | "ö_ö_ö"
"AlsO:ök" | "also:ök"
":still_ok" | ":still_ok"
"___trim" | "trim"
"12.:trim@" | ":trim"
"12.:trim@@" | ":trim"
"fun:ky__tag/1" | "fun:ky_tag/1"
"fun:ky@tag/2" | "fun:ky_tag/2"
"fun:ky@@@tag/3" | "fun:ky_tag/3"
"tag:1/2.3" | "tag:1/2.3"
"---fun:k####y_ta@#g/1_@@#" | "fun:k_y_ta_g/1"
"AlsO:œ#@ö))œk" | "also:œ_ö_œk"
}


Expand All @@ -98,11 +98,11 @@ class TraceUtilsTest extends DDSpecification {
normalized == expected

where:
spanType | expected
null | null
"" | ""
"ok" | "ok"
"VeryLongVeryLongVeryLongVeryLongVeryLongVeryLongVeryLongVeryLongVeryLongVeryLongVeryLongVeryLongVeryLongVeryLong"|"VeryLongVeryLongVeryLongVeryLongVeryLongVeryLongVeryLongVeryLongVeryLongVeryLongVeryLongVeryLongVery"
spanType | expected
null | null
"" | ""
"ok" | "ok"
"VeryLongVeryLongVeryLongVeryLongVeryLongVeryLongVeryLongVeryLongVeryLongVeryLongVeryLongVeryLongVeryLongVeryLong" | "VeryLongVeryLongVeryLongVeryLongVeryLongVeryLongVeryLongVeryLongVeryLongVeryLongVeryLongVeryLongVery"
}

def "test normalize env"() {
Expand All @@ -113,11 +113,11 @@ class TraceUtilsTest extends DDSpecification {
normalized == expected

where:
env | expected
null | ""
"" | ""
"ok" | "ok"
repeat("a",300)|repeat("a",200)
env | expected
null | TraceUtils.DEFAULT_ENV
"" | TraceUtils.DEFAULT_ENV
"ok" | "ok"
repeat("a", 300) | repeat("a", 200)
}

def "test is valid http status code"() {
Expand All @@ -129,14 +129,14 @@ class TraceUtilsTest extends DDSpecification {

where:
httpStatusCode | expected
100 | true
404 | true
600 | false
100 | true
404 | true
600 | false
}

def repeat(String str, int length) {
StringBuilder sb = new StringBuilder(length)
for(int i=0;i<length;i++) {
for (int i = 0; i < length; i++) {
sb.append(str)
}
return sb.toString()
Expand Down

0 comments on commit 0293a1b

Please sign in to comment.