2020import static io .opentelemetry .sdk .autoconfigure .AutoConfigureUtil .getResource ;
2121
2222import com .google .auto .service .AutoService ;
23+ import io .opentelemetry .api .incubator .config .ConfigProvider ;
24+ import io .opentelemetry .api .incubator .config .GlobalConfigProvider ;
2325import io .opentelemetry .javaagent .tooling .BeforeAgentListener ;
2426import io .opentelemetry .sdk .autoconfigure .AutoConfiguredOpenTelemetrySdk ;
2527import io .opentelemetry .sdk .autoconfigure .spi .ConfigProperties ;
@@ -50,10 +52,16 @@ public void beforeAgent(AutoConfiguredOpenTelemetrySdk autoConfiguredOpenTelemet
5052 ConfigProperties config = getConfig (autoConfiguredOpenTelemetrySdk );
5153 Resource resource = getResource (autoConfiguredOpenTelemetrySdk );
5254 if (serviceNameNotConfigured (config , resource )) {
53- logWarn .accept (
54- "The service.name resource attribute is not set. Your service is unnamed and will be difficult to identify.\n "
55- + " Set your service name using the OTEL_SERVICE_NAME or OTEL_RESOURCE_ATTRIBUTES environment variable.\n "
56- + " E.g. `OTEL_SERVICE_NAME=\" <YOUR_SERVICE_NAME_HERE>\" `" );
55+ if (isDeclarativeConfigInUse ()) {
56+ logWarn .accept (
57+ "The service.name resource attribute is not set. Your service is unnamed and will be difficult to identify.\n "
58+ + " Set your service name in '.resource.attributes' node, or specify appropriate resource detector in the configuration YAML file." );
59+ } else {
60+ logWarn .accept (
61+ "The service.name resource attribute is not set. Your service is unnamed and will be difficult to identify.\n "
62+ + " Set your service name using the OTEL_SERVICE_NAME or OTEL_RESOURCE_ATTRIBUTES environment variable.\n "
63+ + " E.g. `OTEL_SERVICE_NAME=\" <YOUR_SERVICE_NAME_HERE>\" `" );
64+ }
5765 }
5866 }
5967
@@ -70,4 +78,8 @@ static boolean serviceNameNotConfigured(ConfigProperties config, Resource resour
7078 && !resourceAttributes .containsKey (ServiceAttributes .SERVICE_NAME .getKey ())
7179 && "unknown_service:java" .equals (resource .getAttribute (ServiceAttributes .SERVICE_NAME ));
7280 }
81+
82+ private static boolean isDeclarativeConfigInUse () {
83+ return !GlobalConfigProvider .get ().equals (ConfigProvider .noop ());
84+ }
7385}
0 commit comments