Skip to content

Commit 24a77f4

Browse files
newtorkbot-sdk-js
andauthored
chore: Reuse DotEnv instance and reduce log messages (#635)
* Add 5min cache to service binding loader * Change solution to cache DotEnv instance instead of ServiceBinding(Accessor); And consider log level dynamically * Formatting * Add line-break * Update release_notes.md * Update log message format. --------- Co-authored-by: SAP Cloud SDK Bot <[email protected]>
1 parent cea022d commit 24a77f4

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

core/src/main/java/com/sap/ai/sdk/core/AiCoreService.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ public class AiCoreService {
4343

4444
/** The default constructor. */
4545
public AiCoreService() {
46-
val resolver = new DestinationResolver();
47-
this.baseDestinationResolver = resolver::getDestination;
48-
this.deploymentResolver = new DeploymentResolver(this);
46+
this(new DestinationResolver()::getDestination);
4947
}
5048

5149
AiCoreService(@Nonnull final Supplier<HttpDestination> baseDestinationResolver) {

core/src/main/java/com/sap/ai/sdk/core/AiCoreServiceKeyAccessor.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import lombok.AllArgsConstructor;
1818
import lombok.extern.slf4j.Slf4j;
1919
import lombok.val;
20+
import org.slf4j.event.Level;
2021

2122
/**
2223
* Loads an AI Core service key from the "AICORE_SERVICE_KEY" environment variable with support for
@@ -27,10 +28,13 @@
2728
class AiCoreServiceKeyAccessor implements ServiceBindingAccessor {
2829
static final String ENV_VAR_KEY = "AICORE_SERVICE_KEY";
2930

31+
private static final Lazy<Dotenv> DEFAULT_DOTENV =
32+
Lazy.of(() -> Dotenv.configure().ignoreIfMissing().ignoreIfMalformed().load());
33+
3034
private final Lazy<Dotenv> dotenv;
3135

3236
AiCoreServiceKeyAccessor() {
33-
this(Dotenv.configure().ignoreIfMissing().ignoreIfMalformed());
37+
this(DEFAULT_DOTENV);
3438
}
3539

3640
AiCoreServiceKeyAccessor(@Nonnull final DotenvBuilder dotenvBuilder) {
@@ -41,6 +45,7 @@ class AiCoreServiceKeyAccessor implements ServiceBindingAccessor {
4145
@Override
4246
public List<ServiceBinding> getServiceBindings() throws ServiceBindingAccessException {
4347
final String serviceKey;
48+
final Level logLevel = dotenv.isEvaluated() ? Level.TRACE : Level.INFO;
4449
try {
4550
serviceKey = dotenv.get().get(ENV_VAR_KEY);
4651
} catch (Exception e) {
@@ -50,13 +55,11 @@ public List<ServiceBinding> getServiceBindings() throws ServiceBindingAccessExce
5055
log.debug("No service key found in environment variable {}", ENV_VAR_KEY);
5156
return List.of();
5257
}
53-
log.info(
54-
"""
55-
Found a service key in environment variable {}.
56-
Using a service key is recommended for local testing only.
57-
Bind the AI Core service to the application for productive usage.
58-
""",
59-
ENV_VAR_KEY);
58+
final String logMessage =
59+
"Found a service key in environment variable {}. "
60+
+ "Using a service key is recommended for local testing only."
61+
+ "Bind the AI Core service to the application for productive usage.";
62+
log.atLevel(logLevel).log(logMessage, ENV_VAR_KEY);
6063

6164
val binding = createServiceBinding(serviceKey);
6265
return List.of(binding);

docs/release_notes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
### 📈 Improvements
1818

1919
- [Core] If the AI Core credentials used are missing an explicit `credential-type` but `clientid` and `clientsecret` are present then `"credential-type": "binding-secret"` is inferred automatically.
20-
20+
- [Core] Log message about "service key in environment variable" to `INFO` level only once.
2121

2222
### 🐛 Fixed Issues
2323

0 commit comments

Comments
 (0)