-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Spring Boot Starter service-name is constant (open-telemetry#5359)
* Spring Boot Starter service-name is constant Pattern-based resource configuration * Add ResourceProvider beans for spring with ConfigProperties
- Loading branch information
1 parent
4d250a8
commit fe00545
Showing
12 changed files
with
469 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...lemetry/instrumentation/spring/autoconfigure/resources/OtelResourceAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.spring.autoconfigure.resources; | ||
|
||
import io.opentelemetry.instrumentation.spring.autoconfigure.OpenTelemetryAutoConfiguration; | ||
import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider; | ||
import io.opentelemetry.sdk.extension.resources.ContainerResource; | ||
import io.opentelemetry.sdk.extension.resources.ContainerResourceProvider; | ||
import io.opentelemetry.sdk.extension.resources.HostResource; | ||
import io.opentelemetry.sdk.extension.resources.HostResourceProvider; | ||
import io.opentelemetry.sdk.extension.resources.OsResource; | ||
import io.opentelemetry.sdk.extension.resources.OsResourceProvider; | ||
import io.opentelemetry.sdk.extension.resources.ProcessResource; | ||
import io.opentelemetry.sdk.extension.resources.ProcessResourceProvider; | ||
import io.opentelemetry.sdk.extension.resources.ProcessRuntimeResource; | ||
import io.opentelemetry.sdk.extension.resources.ProcessRuntimeResourceProvider; | ||
import org.springframework.boot.autoconfigure.AutoConfigureBefore; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
@EnableConfigurationProperties(OtelResourceProperties.class) | ||
@AutoConfigureBefore(OpenTelemetryAutoConfiguration.class) | ||
@ConditionalOnProperty(prefix = "otel.springboot.resource", name = "enabled", matchIfMissing = true) | ||
public class OtelResourceAutoConfiguration { | ||
|
||
@Bean | ||
public ResourceProvider otelResourceProvider(OtelResourceProperties otelResourceProperties) { | ||
return new SpringResourceProvider(otelResourceProperties); | ||
} | ||
|
||
@Bean | ||
@ConditionalOnClass(OsResource.class) | ||
public ResourceProvider otelOsResourceProvider() { | ||
return new OsResourceProvider(); | ||
} | ||
|
||
@Bean | ||
@ConditionalOnClass(ProcessResource.class) | ||
public ResourceProvider otelProcessResourceProvider() { | ||
return new ProcessResourceProvider(); | ||
} | ||
|
||
@Bean | ||
@ConditionalOnClass(ProcessRuntimeResource.class) | ||
public ResourceProvider otelProcessRuntimeResourceProvider() { | ||
return new ProcessRuntimeResourceProvider(); | ||
} | ||
|
||
@Bean | ||
@ConditionalOnClass(HostResource.class) | ||
public ResourceProvider otelHostResourceProvider() { | ||
return new HostResourceProvider(); | ||
} | ||
|
||
@Bean | ||
@ConditionalOnClass(ContainerResource.class) | ||
public ResourceProvider otelContainerResourceProvider() { | ||
return new ContainerResourceProvider(); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
.../opentelemetry/instrumentation/spring/autoconfigure/resources/OtelResourceProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.spring.autoconfigure.resources; | ||
|
||
import java.util.Collections; | ||
import java.util.Map; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
@ConfigurationProperties(prefix = "otel.springboot.resource") | ||
public class OtelResourceProperties { | ||
private Map<String, String> attributes = Collections.emptyMap(); | ||
|
||
public Map<String, String> getAttributes() { | ||
return attributes; | ||
} | ||
|
||
public void setAttributes(Map<String, String> attributes) { | ||
this.attributes = attributes; | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
...emetry/instrumentation/spring/autoconfigure/resources/SpringResourceConfigProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.spring.autoconfigure.resources; | ||
|
||
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; | ||
import java.time.Duration; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import javax.annotation.Nullable; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.expression.ExpressionParser; | ||
|
||
public class SpringResourceConfigProperties implements ConfigProperties { | ||
private final Environment environment; | ||
|
||
private final ExpressionParser parser; | ||
|
||
public SpringResourceConfigProperties(Environment environment, ExpressionParser parser) { | ||
this.environment = environment; | ||
this.parser = parser; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public String getString(String name) { | ||
return environment.getProperty(name, String.class); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Boolean getBoolean(String name) { | ||
return environment.getProperty(name, Boolean.class); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Integer getInt(String name) { | ||
return environment.getProperty(name, Integer.class); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Long getLong(String name) { | ||
return environment.getProperty(name, Long.class); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Double getDouble(String name) { | ||
return environment.getProperty(name, Double.class); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Duration getDuration(String name) { | ||
return environment.getProperty(name, Duration.class); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public List<String> getList(String name) { | ||
return (List<String>) environment.getProperty(name, List.class); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public Map<String, String> getMap(String name) { | ||
String value = environment.getProperty(name); | ||
return (Map<String, String>) parser.parseExpression(Objects.requireNonNull(value)).getValue(); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
.../opentelemetry/instrumentation/spring/autoconfigure/resources/SpringResourceProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.spring.autoconfigure.resources; | ||
|
||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.api.common.AttributesBuilder; | ||
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; | ||
import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider; | ||
import io.opentelemetry.sdk.resources.Resource; | ||
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes; | ||
import java.util.Map; | ||
|
||
public class SpringResourceProvider implements ResourceProvider { | ||
|
||
private final OtelResourceProperties otelResourceProperties; | ||
|
||
public SpringResourceProvider(OtelResourceProperties otelResourceProperties) { | ||
this.otelResourceProperties = otelResourceProperties; | ||
} | ||
|
||
@Override | ||
public Resource createResource(ConfigProperties configProperties) { | ||
String applicationName = configProperties.getString("spring.application.name"); | ||
Map<String, String> attributes = otelResourceProperties.getAttributes(); | ||
AttributesBuilder attributesBuilder = Attributes.builder(); | ||
attributes.forEach(attributesBuilder::put); | ||
return defaultResource(applicationName).merge(Resource.create(attributesBuilder.build())); | ||
} | ||
|
||
private static Resource defaultResource(String applicationName) { | ||
if (applicationName == null) { | ||
return Resource.getDefault(); | ||
} | ||
return Resource.getDefault() | ||
.merge(Resource.create(Attributes.of(ResourceAttributes.SERVICE_NAME, applicationName))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.