-
Notifications
You must be signed in to change notification settings - Fork 870
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ResourceProvider beans for spring with ConfigProperties
- Loading branch information
1 parent
53678f2
commit 393e0b9
Showing
12 changed files
with
284 additions
and
130 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
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
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
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.