Skip to content

Commit 51b841d

Browse files
authored
Entity Error Visibility and Logging (#27)
* Add logger interface and implementations for structured debugging and error reporting - Introduce `Logger` interface for debug, info, warn, and error-level logs. - Add `NopLogger` for discarding log output. - Implement `StdLogger` using the standard library to support structured logging with optional debug mode. * Enhance `Toolkit` with structured logging for debugging and error reporting - Initialize logger with `Logger` interface and added structured logging in debug mode. - Log tool invocation, middleware hooks, handler results, and client connection selection. - Improve error visibility with detailed log messages for middleware and client failures. * Improve error messaging in `GetEntity` for better debugging and nil entity handling * Add debug logging fields to `Config` struct for enhanced debugging and logging support * Add `Debug` and `Logger` fields to `Config` for improved debug logging and configurability * Add structured logging to `Client` for improved debugging and error reporting - Introduced `Logger` to `Client` for better log management. - Added detailed logs for query execution, retries, errors, and response processing. - Implemented utility functions for operation name extraction and string truncation. * Refactor `Client` to modularize error handling and response parsing - Extract request error handling to `handleRequestError`. - Add `checkStatusCode` for HTTP status code validation. - Modularize GraphQL error processing with `handleGraphQLErrors`. - Introduce `parseGraphQLResponse` for clearer response parsing logic. * Add `Debug` and `Logger` fields to `Config` and update documentation for enhanced debugging support * Refactor `APIClient` response parsing and add enhanced error handling utilities * Document `DATAHUB_DEBUG` configuration option in server settings * Document `DATAHUB_DEBUG` configuration option in README * Add comprehensive unit tests for `helpers`, `logger`, and GraphQL response handling utilities - Created tests for utility functions like `extractOperationName`, `truncateString`, and `joinStrings`. - Added robust test cases for error handling in GraphQL response parsing and logger behavior. - Verified structured logging and debug behavior for `StdLogger` and `NopLogger`. * Handle GraphQL `null` data correctly in response parsing
1 parent 08c46e8 commit 51b841d

12 files changed

Lines changed: 946 additions & 22 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ See the [tools reference](https://mcp-datahub.txn2.com/server/tools/) for detail
219219
| `DATAHUB_MAX_LIMIT` | Maximum limit | `100` |
220220
| `DATAHUB_CONNECTION_NAME` | Display name for primary connection | `datahub` |
221221
| `DATAHUB_ADDITIONAL_SERVERS` | JSON map of additional servers | (optional) |
222+
| `DATAHUB_DEBUG` | Enable debug logging (`1` or `true`) | `false` |
222223

223224
See [configuration reference](https://mcp-datahub.txn2.com/server/configuration/) for all options.
224225

docs/reference/configuration.md

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Complete configuration reference for mcp-datahub.
2020
| `DATAHUB_DEFAULT_LIMIT` | Default search result limit | `10` |
2121
| `DATAHUB_MAX_LIMIT` | Maximum allowed search limit | `100` |
2222
| `DATAHUB_MAX_LINEAGE_DEPTH` | Maximum lineage traversal depth | `5` |
23+
| `DATAHUB_DEBUG` | Enable debug logging (`1` or `true`) | `false` |
2324

2425
## Client Configuration
2526

@@ -34,6 +35,8 @@ type Config struct {
3435
DefaultLimit int // Default search limit
3536
MaxLimit int // Maximum search limit
3637
MaxLineageDepth int // Max lineage depth
38+
Debug bool // Enable debug logging
39+
Logger Logger // Custom logger (nil = auto-select)
3740
}
3841
```
3942

@@ -49,6 +52,8 @@ func DefaultConfig() Config {
4952
DefaultLimit: 10,
5053
MaxLimit: 100,
5154
MaxLineageDepth: 5,
55+
Debug: false,
56+
Logger: nil, // Uses NopLogger; StdLogger when Debug=true
5257
}
5358
}
5459
```
@@ -68,9 +73,11 @@ if err != nil {
6873

6974
```go
7075
type Config struct {
71-
DefaultLimit int // Default search limit
72-
MaxLimit int // Maximum search limit
73-
MaxLineageDepth int // Max lineage depth
76+
DefaultLimit int // Default search limit
77+
MaxLimit int // Maximum search limit
78+
MaxLineageDepth int // Max lineage depth
79+
Debug bool // Enable debug logging
80+
Logger client.Logger // Custom logger (nil = auto-select)
7481
}
7582
```
7683

@@ -84,6 +91,62 @@ toolkit := tools.NewToolkit(datahubClient, tools.Config{
8491
})
8592
```
8693

94+
## Debug Logging
95+
96+
Enable debug logging to troubleshoot issues with DataHub connectivity, GraphQL queries, and tool execution.
97+
98+
### Via Environment Variable
99+
100+
```bash
101+
export DATAHUB_DEBUG=1
102+
./mcp-datahub
103+
```
104+
105+
### Programmatic Configuration
106+
107+
```go
108+
// Auto-create StdLogger when Debug=true
109+
cfg := client.Config{
110+
URL: "https://datahub.example.com",
111+
Token: "token",
112+
Debug: true,
113+
}
114+
115+
// Or provide a custom logger
116+
cfg := client.Config{
117+
URL: "https://datahub.example.com",
118+
Token: "token",
119+
Logger: myCustomLogger,
120+
}
121+
```
122+
123+
### Logger Interface
124+
125+
The `Logger` interface is compatible with `slog.Logger` patterns:
126+
127+
```go
128+
type Logger interface {
129+
Debug(msg string, args ...any)
130+
Info(msg string, args ...any)
131+
Warn(msg string, args ...any)
132+
Error(msg string, args ...any)
133+
}
134+
```
135+
136+
Built-in implementations:
137+
- `NopLogger` - Discards all output (default when debug disabled)
138+
- `StdLogger` - Writes to stderr with structured key-value format
139+
140+
### Log Output
141+
142+
When debug logging is enabled, you'll see:
143+
144+
```
145+
[datahub] DEBUG: executing GraphQL query [operation=GetEntity endpoint=https://... request_size=256]
146+
[datahub] DEBUG: received response [status=200 response_size=1024]
147+
[datahub] DEBUG: request completed [operation=GetEntity duration_ms=150 attempts=1]
148+
```
149+
87150
## Validation
88151

89152
Configuration is validated on client creation:

docs/server/configuration.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ All configuration is done through environment variables.
2020
| `DATAHUB_MAX_LINEAGE_DEPTH` | Maximum lineage traversal depth | `5` |
2121
| `DATAHUB_CONNECTION_NAME` | Display name for primary connection | `datahub` |
2222
| `DATAHUB_ADDITIONAL_SERVERS` | JSON map of additional servers | (empty) |
23+
| `DATAHUB_DEBUG` | Enable debug logging (`1` or `true`) | `false` |
2324

2425
## Example Configuration
2526

@@ -32,6 +33,9 @@ export DATAHUB_TOKEN=your_personal_access_token
3233
export DATAHUB_TIMEOUT=60
3334
export DATAHUB_DEFAULT_LIMIT=20
3435
export DATAHUB_MAX_LIMIT=50
36+
37+
# Debug logging (for troubleshooting)
38+
export DATAHUB_DEBUG=1
3539
```
3640

3741
## Multi-Server Configuration

docs/support/troubleshooting.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,41 @@ export DATAHUB_TOKEN=your_token
113113
# Or use .env file with direnv
114114
```
115115

116+
## Debug Logging
117+
118+
Enable debug logging to see detailed information about requests, responses, and errors.
119+
120+
### Enable Debug Mode
121+
122+
```bash
123+
export DATAHUB_DEBUG=1
124+
./mcp-datahub
125+
```
126+
127+
### What Debug Logging Shows
128+
129+
- GraphQL operation names and request sizes
130+
- HTTP response status codes and sizes
131+
- Request duration and retry attempts
132+
- Detailed error messages with context
133+
- Connection selection in multi-server mode
134+
135+
### Example Debug Output
136+
137+
```
138+
[datahub] DEBUG: executing GraphQL query [operation=GetEntity endpoint=https://datahub.example.com/api/graphql request_size=256]
139+
[datahub] DEBUG: received response [status=200 response_size=1024]
140+
[datahub] DEBUG: request completed [operation=GetEntity duration_ms=150 attempts=1]
141+
```
142+
143+
### Common Debug Scenarios
144+
145+
**Silent failures**: If a query returns empty results unexpectedly, debug logging will show if GraphQL returned null data without errors.
146+
147+
**Retry behavior**: See when and how often requests are retried, including backoff timing.
148+
149+
**Auth issues**: Debug logs show when requests fail with 401/403 status codes before the error is returned.
150+
116151
## Getting Help
117152

118153
If you're still having issues:

0 commit comments

Comments
 (0)