Skip to content

Commit 6b1e2bc

Browse files
authored
[Initial commit] Add telemetry log levels (#1032)
## Description - This is the first PR to introduce log levels to telemetry. ## Testing - Unit tests added ## Additional Notes to the Reviewer - In subsequent PRs, we will be incorporating logLevels in telemetry push too. - Internal doc : https://docs.google.com/document/d/1EmsW4jYMtpvjlLovyRQsOwqIBAN-MAcOZKAS0Bm0c7Q/edit?tab=t.0 NO_CHANGELOG=true OVERRIDE_FREEZE=true (Note that the release is already made, I will remove the freeze in my next PR)
1 parent 965bd19 commit 6b1e2bc

5 files changed

Lines changed: 112 additions & 0 deletions

File tree

src/main/java/com/databricks/jdbc/api/impl/DatabricksConnectionContext.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,4 +1071,10 @@ public boolean isBatchedInsertsEnabled() {
10711071
public boolean getIgnoreTransactions() {
10721072
return getParameter(DatabricksJdbcUrlParams.IGNORE_TRANSACTIONS, "0").equals("1");
10731073
}
1074+
1075+
@Override
1076+
public TelemetryLogLevel getTelemetryLogLevel() {
1077+
return TelemetryLogLevel.parse(
1078+
getParameter(DatabricksJdbcUrlParams.TELEMETRY_LOG_LEVEL), TelemetryLogLevel.DEBUG);
1079+
}
10741080
}

src/main/java/com/databricks/jdbc/api/internal/IDatabricksConnectionContext.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ public interface IDatabricksConnectionContext {
7474

7575
LogLevel getLogLevel();
7676

77+
TelemetryLogLevel getTelemetryLogLevel();
78+
7779
String getLogPathString();
7880

7981
int getLogFileSize();

src/main/java/com/databricks/jdbc/common/DatabricksJdbcUrlParams.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
/** Enum to hold all the Databricks JDBC URL parameters. */
66
public enum DatabricksJdbcUrlParams {
77
LOG_LEVEL("loglevel", "Log level for debugging"),
8+
TELEMETRY_LOG_LEVEL("telemetryLogLevel", "Log level for telemetry logs", "DEBUG"),
89
LOG_PATH("logpath", "Path to the log file"),
910
LOG_FILE_SIZE("LogFileSize", "Maximum size of the log file", "10"), // 10 MB
1011
LOG_FILE_COUNT("LogFileCount", "Number of log files to retain", "10"),
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.databricks.jdbc.common;
2+
3+
/**
4+
* Telemetry log verbosity for structured driver events; independent of {@link LogLevel} and set via
5+
* {@code telemetryLogLevel} (see {@link DatabricksJdbcUrlParams#TELEMETRY_LOG_LEVEL}).
6+
*/
7+
public enum TelemetryLogLevel {
8+
/** Disable telemetry logging. */
9+
OFF(0),
10+
11+
/** Unrecoverable conditions that abort processing. */
12+
FATAL(1),
13+
14+
/** Errors that fail an operation but do not crash the process. */
15+
ERROR(2),
16+
17+
/** Potential issues without interrupting normal flow. */
18+
WARN(3),
19+
20+
/** High-level normal activity. */
21+
INFO(4),
22+
23+
/** Detailed diagnostics for troubleshooting. */
24+
DEBUG(5),
25+
26+
/** Maximum verbosity including internal timings and state. */
27+
TRACE(6);
28+
29+
private final int code;
30+
31+
TelemetryLogLevel(int code) {
32+
this.code = code;
33+
}
34+
35+
/** Returns the integer code for this level. */
36+
public int toInt() {
37+
return code;
38+
}
39+
40+
/** Parses a telemetry level from its integer code. Defaults to DEBUG for unknown codes. */
41+
public static TelemetryLogLevel fromInt(int code) {
42+
switch (code) {
43+
case 0:
44+
return OFF;
45+
case 1:
46+
return FATAL;
47+
case 2:
48+
return ERROR;
49+
case 3:
50+
return WARN;
51+
case 4:
52+
return INFO;
53+
case 5:
54+
return DEBUG;
55+
case 6:
56+
return TRACE;
57+
default:
58+
return DEBUG;
59+
}
60+
}
61+
62+
/** Parses from string accepting either integer codes or names; falls back to defaultLevel. */
63+
public static TelemetryLogLevel parse(String value, TelemetryLogLevel defaultLevel) {
64+
if (value == null) return defaultLevel;
65+
String v = value.trim();
66+
if (v.isEmpty()) return defaultLevel;
67+
try {
68+
return fromInt(Integer.parseInt(v));
69+
} catch (NumberFormatException ignored) {
70+
// not an integer
71+
}
72+
try {
73+
return TelemetryLogLevel.valueOf(v.toUpperCase());
74+
} catch (IllegalArgumentException ignored) {
75+
return defaultLevel;
76+
}
77+
}
78+
}

src/test/java/com/databricks/jdbc/api/impl/DatabricksConnectionContextTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.util.Properties;
2323
import org.junit.jupiter.api.BeforeAll;
2424
import org.junit.jupiter.api.Test;
25+
import org.junit.jupiter.params.ParameterizedTest;
26+
import org.junit.jupiter.params.provider.CsvSource;
2527

2628
class DatabricksConnectionContextTest {
2729

@@ -505,6 +507,29 @@ void testLogLevels() {
505507
assertEquals(getLogLevel(6), LogLevel.TRACE);
506508
}
507509

510+
@ParameterizedTest
511+
@CsvSource(
512+
value = {
513+
"<NULL>, DEBUG",
514+
"'', DEBUG",
515+
"6, TRACE",
516+
"0, OFF",
517+
"' trace ', TRACE",
518+
"nope, DEBUG"
519+
},
520+
nullValues = "<NULL>")
521+
void testTelemetryLogLevelParameterized(String input, TelemetryLogLevel expected)
522+
throws DatabricksSQLException {
523+
String baseUrl = TestConstants.VALID_URL_1;
524+
Properties props = new Properties();
525+
if (input != null) {
526+
props.setProperty("telemetryLogLevel", input);
527+
}
528+
DatabricksConnectionContext ctx =
529+
(DatabricksConnectionContext) DatabricksConnectionContext.parse(baseUrl, props);
530+
assertEquals(expected, ctx.getTelemetryLogLevel());
531+
}
532+
508533
@Test
509534
public void testGetOAuth2RedirectUrlPorts() throws DatabricksSQLException {
510535
// Test default value

0 commit comments

Comments
 (0)