1- package tech .ydb .slo ;
2-
3- /**
4- * Configuration for the SLO workload, populated from environment variables
5- * provided by the YDB SLO action runtime.
6- *
7- * <p>The action sets these variables on the workload container:
8- * <ul>
9- * <li>{@code YDB_CONNECTION_STRING} or {@code YDB_ENDPOINT} + {@code YDB_DATABASE} — YDB connection</li>
10- * <li>{@code WORKLOAD_REF} — value used as the {@code ref} label on all metrics</li>
11- * <li>{@code WORKLOAD_NAME} — workload name (also used as part of the table path)</li>
12- * <li>{@code WORKLOAD_DURATION} — workload run duration in seconds (0 = unlimited)</li>
13- * <li>{@code OTEL_EXPORTER_OTLP_ENDPOINT} — OTLP endpoint for pushing metrics</li>
14- * </ul>
15- */
1+ package tech .ydb .slo .core ;
2+
163public final class Config {
174 private final String connectionString ;
5+ private final String jdbcUrl ;
186 private final String token ;
197 private final String ref ;
208 private final String workloadName ;
@@ -23,24 +11,34 @@ public final class Config {
2311
2412 private Config (
2513 String connectionString ,
14+ String jdbcUrl ,
2615 String token ,
2716 String ref ,
2817 String workloadName ,
2918 int durationSeconds ,
3019 String otlpEndpoint
3120 ) {
3221 this .connectionString = connectionString ;
22+ this .jdbcUrl = jdbcUrl ;
3323 this .token = token ;
3424 this .ref = ref ;
3525 this .workloadName = workloadName ;
3626 this .durationSeconds = durationSeconds ;
3727 this .otlpEndpoint = otlpEndpoint ;
3828 }
3929
30+
31+
4032 public String connectionString () {
4133 return connectionString ;
4234 }
4335
36+
37+
38+ public String jdbcUrl () {
39+ return jdbcUrl ;
40+ }
41+
4442 public String token () {
4543 return token ;
4644 }
@@ -61,43 +59,72 @@ public String otlpEndpoint() {
6159 return otlpEndpoint ;
6260 }
6361
64- /**
65- * Loads configuration from environment variables.
66- *
67- * @return configuration instance
68- * @throws IllegalStateException if required variables are missing or invalid
69- */
70- public static Config fromEnv () {
62+
63+
64+ public static Config fromEnv (String defaultWorkloadName ) {
7165 String connectionString = resolveConnectionString ();
7266 if (connectionString == null || connectionString .isEmpty ()) {
7367 throw new IllegalStateException (
74- "YDB connection is not configured: set YDB_CONNECTION_STRING or YDB_ENDPOINT + YDB_DATABASE"
68+ "YDB connection is not configured: set YDB_CONNECTION_STRING, "
69+ + "YDB_JDBC_URL or YDB_ENDPOINT + YDB_DATABASE"
7570 );
7671 }
7772
7873 String token = envOrDefault ("YDB_TOKEN" , "" );
7974 String ref = envOrDefault ("WORKLOAD_REF" , "unknown" );
80- String workloadName = envOrDefault ("WORKLOAD_NAME" , "java-slo-workload" );
75+ String workloadName = envOrDefault ("WORKLOAD_NAME" , defaultWorkloadName );
8176 int durationSeconds = parseInt (envOrDefault ("WORKLOAD_DURATION" , "600" ), 600 );
8277 String otlpEndpoint = envOrDefault ("OTEL_EXPORTER_OTLP_ENDPOINT" , "" );
8378
84- return new Config (connectionString , token , ref , workloadName , durationSeconds , otlpEndpoint );
79+ return new Config (
80+ connectionString ,
81+ toJdbcUrl (connectionString ),
82+ token ,
83+ ref ,
84+ workloadName ,
85+ durationSeconds ,
86+ otlpEndpoint
87+ );
8588 }
8689
90+
91+
8792 private static String resolveConnectionString () {
93+ String jdbc = System .getenv ("YDB_JDBC_URL" );
94+ if (jdbc != null && !jdbc .isEmpty ()) {
95+ return stripJdbcPrefix (jdbc );
96+ }
97+
8898 String cs = System .getenv ("YDB_CONNECTION_STRING" );
8999 if (cs != null && !cs .isEmpty ()) {
90- return cs ;
100+ return stripJdbcPrefix ( cs ) ;
91101 }
92102
93103 String endpoint = System .getenv ("YDB_ENDPOINT" );
94104 String database = System .getenv ("YDB_DATABASE" );
95105 if (endpoint == null || endpoint .isEmpty () || database == null || database .isEmpty ()) {
96106 return null ;
97107 }
108+ return composeConnectionString (endpoint , database );
109+ }
110+
111+ private static String stripJdbcPrefix (String value ) {
112+ if (value .startsWith ("jdbc:ydb:" )) {
113+ return value .substring ("jdbc:ydb:" .length ());
114+ }
115+ return value ;
116+ }
117+
118+
119+
120+ private static String toJdbcUrl (String connectionString ) {
121+ if (connectionString .startsWith ("jdbc:" )) {
122+ return connectionString ;
123+ }
124+ return "jdbc:ydb:" + connectionString ;
125+ }
98126
99- // Compose connection string in the form expected by GrpcTransport.forConnectionString:
100- // grpc://host:port/database
127+ private static String composeConnectionString (String endpoint , String database ) {
101128 if (endpoint .endsWith ("/" ) && database .startsWith ("/" )) {
102129 return endpoint + database .substring (1 );
103130 }
0 commit comments