Skip to content

Commit

Permalink
Twilio: allow service name flattening
Browse files Browse the repository at this point in the history
  • Loading branch information
amarziali committed Nov 27, 2024
1 parent 9eaf5a0 commit 19c0001
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.twilio.rest.api.v2010.account.Message;
import datadog.trace.api.Functions;
import datadog.trace.api.cache.QualifiedClassNameCache;
import datadog.trace.api.naming.SpanNaming;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.api.InternalSpanTypes;
import datadog.trace.bootstrap.instrumentation.api.UTF8BytesString;
Expand All @@ -24,6 +25,11 @@ public class TwilioClientDecorator extends ClientDecorator {

private static final CharSequence COMPONENT_NAME = UTF8BytesString.create("twilio-sdk");

private static final String SERVICE_NAME =
SpanNaming.instance().namingSchema().allowInferredServices()
? COMPONENT_NAME.toString()
: null;

private static final QualifiedClassNameCache NAMES =
new QualifiedClassNameCache(
new Function<Class<?>, CharSequence>() {
Expand Down Expand Up @@ -54,7 +60,7 @@ protected CharSequence component() {

@Override
protected String service() {
return COMPONENT_NAME.toString();
return SERVICE_NAME;
}

/** Decorate trace based on service execution metadata. */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package test

import static datadog.trace.agent.test.utils.TraceUtils.runUnderTrace

import com.fasterxml.jackson.databind.ObjectMapper
import com.google.common.util.concurrent.ListenableFuture
import com.twilio.Twilio
Expand All @@ -10,7 +12,9 @@ import com.twilio.http.TwilioRestClient
import com.twilio.rest.api.v2010.account.Call
import com.twilio.rest.api.v2010.account.Message
import com.twilio.type.PhoneNumber
import datadog.trace.agent.test.AgentTestRunner
import datadog.trace.agent.test.naming.TestingGenericHttpNamingConventions
import datadog.trace.agent.test.naming.VersionedNamingTestBase
import datadog.trace.api.Config
import datadog.trace.api.DDSpanTypes
import datadog.trace.bootstrap.instrumentation.api.Tags
import org.apache.http.HttpEntity
Expand All @@ -23,9 +27,7 @@ import org.apache.http.impl.client.HttpClientBuilder
import java.util.concurrent.ExecutionException
import java.util.concurrent.TimeUnit

import static datadog.trace.agent.test.utils.TraceUtils.runUnderTrace

class TwilioClientTest extends AgentTestRunner {
abstract class TwilioClientTest extends VersionedNamingTestBase {

// Made up Twilio Account IDs and Auth token
final static String SHORT_SID = "abc"
Expand Down Expand Up @@ -113,6 +115,13 @@ class TwilioClientTest extends AgentTestRunner {
Twilio.setRestClient(null)
}

abstract String httpClientOperation()

@Override
String operation() {
return "twilio.sdk"
}

def "synchronous message"() {
setup:
twilioRestClient.getObjectMapper() >> new ObjectMapper()
Expand Down Expand Up @@ -143,8 +152,8 @@ class TwilioClientTest extends AgentTestRunner {
}
}
span {
serviceName "twilio-sdk"
operationName "twilio.sdk"
serviceName service()
operationName operation()
resourceName "api.v2010.account.MessageCreator.create"
spanType DDSpanTypes.HTTP_CLIENT
errored false
Expand All @@ -156,7 +165,7 @@ class TwilioClientTest extends AgentTestRunner {
"twilio.account" "$LONG_SID"
"twilio.sid" "MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
"twilio.status" "sent"
defaultTags()
defaultTagsNoPeerService()
}
}
}
Expand Down Expand Up @@ -195,8 +204,8 @@ class TwilioClientTest extends AgentTestRunner {
}
}
span {
serviceName "twilio-sdk"
operationName "twilio.sdk"
serviceName service()
operationName operation()
resourceName "api.v2010.account.CallCreator.create"
spanType DDSpanTypes.HTTP_CLIENT
errored false
Expand All @@ -208,7 +217,7 @@ class TwilioClientTest extends AgentTestRunner {
"twilio.account" "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
"twilio.sid" "CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
"twilio.status" "completed"
defaultTags()
defaultTagsNoPeerService()
}
}
}
Expand Down Expand Up @@ -269,8 +278,8 @@ class TwilioClientTest extends AgentTestRunner {
}
}
span {
serviceName "twilio-sdk"
operationName "twilio.sdk"
serviceName service()
operationName operation()
resourceName "api.v2010.account.MessageCreator.create"
spanType DDSpanTypes.HTTP_CLIENT
errored false
Expand All @@ -282,12 +291,12 @@ class TwilioClientTest extends AgentTestRunner {
"twilio.account" "$LONG_SID"
"twilio.sid" "MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
"twilio.status" "sent"
defaultTags()
defaultTagsNoPeerService()
}
}
span {
serviceName "twilio-sdk"
operationName "http.request"
serviceName service()
operationName httpClientOperation()
resourceName "POST /?/Accounts/abc/Messages.json"
spanType DDSpanTypes.HTTP_CLIENT
errored false
Expand Down Expand Up @@ -375,8 +384,8 @@ class TwilioClientTest extends AgentTestRunner {
}
}
span {
serviceName "twilio-sdk"
operationName "twilio.sdk"
serviceName service()
operationName operation()
resourceName "api.v2010.account.MessageCreator.create"
spanType DDSpanTypes.HTTP_CLIENT
errored false
Expand All @@ -388,12 +397,12 @@ class TwilioClientTest extends AgentTestRunner {
"twilio.account" "$LONG_SID"
"twilio.sid" "MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
"twilio.status" "sent"
defaultTags()
defaultTagsNoPeerService()
}
}
span {
serviceName "twilio-sdk"
operationName "http.request"
serviceName service()
operationName httpClientOperation()
resourceName "POST /?/Accounts/abc/Messages.json"
spanType DDSpanTypes.HTTP_CLIENT
errored false
Expand All @@ -409,8 +418,8 @@ class TwilioClientTest extends AgentTestRunner {
}
}
span {
serviceName "twilio-sdk"
operationName "http.request"
serviceName service()
operationName httpClientOperation()
resourceName "POST /?/Accounts/abc/Messages.json"
spanType DDSpanTypes.HTTP_CLIENT
errored false
Expand Down Expand Up @@ -505,8 +514,8 @@ class TwilioClientTest extends AgentTestRunner {
}
}
span {
serviceName "twilio-sdk"
operationName "twilio.sdk"
serviceName service()
operationName operation()
resourceName "api.v2010.account.MessageCreator.createAsync"
spanType DDSpanTypes.HTTP_CLIENT
errored false
Expand All @@ -518,12 +527,12 @@ class TwilioClientTest extends AgentTestRunner {
"twilio.account" "$LONG_SID"
"twilio.sid" "MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
"twilio.status" "sent"
defaultTags()
defaultTagsNoPeerService()
}
}
span {
serviceName "twilio-sdk"
operationName "twilio.sdk"
serviceName service()
operationName operation()
resourceName "api.v2010.account.MessageCreator.create"
spanType DDSpanTypes.HTTP_CLIENT
errored false
Expand All @@ -535,14 +544,14 @@ class TwilioClientTest extends AgentTestRunner {
"twilio.account" "$LONG_SID"
"twilio.sid" "MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
"twilio.status" "sent"
defaultTags()
defaultTagsNoPeerService()
}
}
// Spans are reported in reverse order of completion,
// so the error span is last even though it happened first.
span {
serviceName "twilio-sdk"
operationName "http.request"
serviceName service()
operationName httpClientOperation()
resourceName "POST /?/Accounts/abc/Messages.json"
spanType DDSpanTypes.HTTP_CLIENT
errored false
Expand All @@ -558,8 +567,8 @@ class TwilioClientTest extends AgentTestRunner {
}
}
span {
serviceName "twilio-sdk"
operationName "http.request"
serviceName service()
operationName httpClientOperation()
resourceName "POST /?/Accounts/abc/Messages.json"
spanType DDSpanTypes.HTTP_CLIENT
errored false
Expand Down Expand Up @@ -616,8 +625,8 @@ class TwilioClientTest extends AgentTestRunner {
}
}
span {
serviceName "twilio-sdk"
operationName "twilio.sdk"
serviceName service()
operationName operation()
resourceName "api.v2010.account.MessageCreator.create"
spanType DDSpanTypes.HTTP_CLIENT
errored true
Expand All @@ -626,7 +635,7 @@ class TwilioClientTest extends AgentTestRunner {
"$Tags.COMPONENT" "twilio-sdk"
"$Tags.SPAN_KIND" Tags.SPAN_KIND_CLIENT
errorTags(ApiException, "Testing Failure")
defaultTags()
defaultTagsNoPeerService()
}
}
}
Expand All @@ -652,8 +661,8 @@ class TwilioClientTest extends AgentTestRunner {
assertTraces(1) {
trace(1) {
span {
serviceName "twilio-sdk"
operationName "twilio.sdk"
serviceName service()
operationName operation()
resourceName "api.v2010.account.MessageCreator.create"
parent()
spanType DDSpanTypes.HTTP_CLIENT
Expand All @@ -666,7 +675,7 @@ class TwilioClientTest extends AgentTestRunner {
"twilio.account" "$LONG_SID"
"twilio.sid" "MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
"twilio.status" "sent"
defaultTags()
defaultTagsNoPeerService()
}
}
}
Expand Down Expand Up @@ -714,8 +723,8 @@ class TwilioClientTest extends AgentTestRunner {
}
}
span {
serviceName "twilio-sdk"
operationName "twilio.sdk"
serviceName service()
operationName operation()
resourceName "api.v2010.account.MessageCreator.createAsync"
spanType DDSpanTypes.HTTP_CLIENT
errored false
Expand All @@ -727,12 +736,12 @@ class TwilioClientTest extends AgentTestRunner {
"twilio.account" "$LONG_SID"
"twilio.sid" "MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
"twilio.status" "sent"
defaultTags()
defaultTagsNoPeerService()
}
}
span {
serviceName "twilio-sdk"
operationName "twilio.sdk"
serviceName service()
operationName operation()
resourceName "api.v2010.account.MessageCreator.create"
spanType DDSpanTypes.HTTP_CLIENT
errored false
Expand All @@ -744,7 +753,7 @@ class TwilioClientTest extends AgentTestRunner {
"twilio.account" "$LONG_SID"
"twilio.sid" "MMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
"twilio.status" "sent"
defaultTags()
defaultTagsNoPeerService()
}
}
}
Expand Down Expand Up @@ -800,8 +809,8 @@ class TwilioClientTest extends AgentTestRunner {
}
}
span {
serviceName "twilio-sdk"
operationName "twilio.sdk"
serviceName service()
operationName operation()
resourceName "api.v2010.account.MessageCreator.createAsync"
spanType DDSpanTypes.HTTP_CLIENT
errored true
Expand All @@ -810,12 +819,12 @@ class TwilioClientTest extends AgentTestRunner {
"$Tags.COMPONENT" "twilio-sdk"
"$Tags.SPAN_KIND" Tags.SPAN_KIND_CLIENT
errorTags(ApiException, "Testing Failure")
defaultTags()
defaultTagsNoPeerService()
}
}
span {
serviceName "twilio-sdk"
operationName "twilio.sdk"
serviceName service()
operationName operation()
resourceName "api.v2010.account.MessageCreator.create"
spanType DDSpanTypes.HTTP_CLIENT
errored true
Expand All @@ -824,10 +833,46 @@ class TwilioClientTest extends AgentTestRunner {
"$Tags.COMPONENT" "twilio-sdk"
"$Tags.SPAN_KIND" Tags.SPAN_KIND_CLIENT
errorTags(ApiException, "Testing Failure")
defaultTags()
defaultTagsNoPeerService()
}
}
}
}
}
}

class TwilioClientV0Test extends TwilioClientTest {

@Override
int version() {
return 0
}

@Override
String service() {
return "twilio-sdk"
}

@Override
String httpClientOperation() {
return new TestingGenericHttpNamingConventions.ClientV0(){}.operation()
}
}

class TwilioClientV1ForkedTest extends TwilioClientTest {

@Override
int version() {
return 1
}

@Override
String service() {
return Config.get().getServiceName()
}

@Override
String httpClientOperation() {
return new TestingGenericHttpNamingConventions.ClientV1(){}.operation()
}
}

0 comments on commit 19c0001

Please sign in to comment.