Skip to content

Commit 71c3af8

Browse files
author
timpratim
committed
Updated SDK to use SDK_TOKEN and API_KEY
1 parent 8bf03b5 commit 71c3af8

File tree

13 files changed

+68
-77
lines changed

13 files changed

+68
-77
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ import (
6969

7070
func main() {
7171
treblle.Configure(treblle.Configuration{
72-
APIKey: "your-treblle-api-key",
73-
ProjectID: "your-treblle-project-id",
72+
SDK_TOKEN: "your-treblle-sdk-token",
73+
API_KEY: "your-treblle-api-key",
7474
})
7575

7676
// Your API server setup
@@ -93,8 +93,8 @@ import (
9393
func main() {
9494
// Configure Treblle
9595
treblle.Configure(treblle.Configuration{
96-
APIKey: "your-treblle-api-key",
97-
ProjectID: "your-treblle-project-id",
96+
SDK_TOKEN: "your-treblle-sdk-token",
97+
API_KEY: "your-treblle-api-key",
9898
})
9999

100100
// Create a new router
@@ -124,8 +124,8 @@ import (
124124
func main() {
125125
// Configure Treblle
126126
treblle.Configure(treblle.Configuration{
127-
APIKey: "your-treblle-api-key",
128-
ProjectID: "your-treblle-project-id",
127+
SDK_TOKEN: "your-treblle-sdk-token",
128+
API_KEY: "your-treblle-api-key",
129129
})
130130

131131
// Create a new serve mux

batch_error_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
func TestBatchErrorCollector(t *testing.T) {
1111
// Configure Treblle with test settings
1212
Configure(Configuration{
13-
APIKey: "test-api-key",
14-
ProjectID: "test-project-id",
13+
SDK_TOKEN: "test-sdk-token",
14+
API_KEY: "test-api-key",
1515
Endpoint: "http://localhost:8080",
1616
})
1717

configuration.go

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ var Config internalConfiguration
1111

1212
// Configuration sets up and customizes communication with the Treblle API
1313
type Configuration struct {
14-
APIKey string
15-
ProjectID string
14+
SDK_TOKEN string
15+
API_KEY string
1616
AdditionalFieldsToMask []string
1717
DefaultFieldsToMask []string
1818
MaskingEnabled bool
@@ -47,15 +47,15 @@ type internalConfiguration struct {
4747
AsyncProcessingEnabled bool
4848
MaxConcurrentProcessing int
4949
AsyncShutdownTimeout time.Duration
50-
IgnoredEnvironments []string // Environments where Treblle does not track requests
50+
IgnoredEnvironments []string
5151
}
5252

5353
func Configure(config Configuration) {
54-
if config.APIKey != "" {
55-
Config.APIKey = config.APIKey
54+
if config.SDK_TOKEN != "" {
55+
Config.APIKey = config.SDK_TOKEN
5656
}
57-
if config.ProjectID != "" {
58-
Config.ProjectID = config.ProjectID
57+
if config.API_KEY != "" {
58+
Config.ProjectID = config.API_KEY
5959
}
6060
if config.Endpoint != "" {
6161
Config.Endpoint = config.Endpoint
@@ -65,11 +65,11 @@ func Configure(config Configuration) {
6565
Config.Debug = config.Debug
6666

6767
// Initialize server and language info
68-
Config.serverInfo = GetServerInfo(nil) // Pass nil as request, protocol will be updated in middleware
68+
Config.serverInfo = GetServerInfo(nil)
6969
Config.languageInfo = GetLanguageInfo()
7070

7171
// Initialize default masking settings
72-
Config.MaskingEnabled = true // Enable by default
72+
Config.MaskingEnabled = true
7373

7474
// Set SDK Name and Version (Can be overridden via ENV)
7575
sdkName := "go"
@@ -94,21 +94,19 @@ func Configure(config Configuration) {
9494
Config.AsyncProcessingEnabled = config.AsyncProcessingEnabled
9595
Config.MaxConcurrentProcessing = config.MaxConcurrentProcessing
9696
if Config.MaxConcurrentProcessing <= 0 {
97-
Config.MaxConcurrentProcessing = 10 // Default to 10 concurrent operations
97+
Config.MaxConcurrentProcessing = 10
9898
}
9999

100100
Config.AsyncShutdownTimeout = config.AsyncShutdownTimeout
101101
if Config.AsyncShutdownTimeout <= 0 {
102-
Config.AsyncShutdownTimeout = 5 * time.Second // Default to 5 seconds
102+
Config.AsyncShutdownTimeout = 5 * time.Second
103103
}
104104

105105
// Initialize batch error collector if enabled
106106
if config.BatchErrorEnabled {
107-
// Close existing collector if any
108107
if Config.batchErrorCollector != nil {
109108
Config.batchErrorCollector.Close()
110109
}
111-
// Create new batch error collector
112110
Config.batchErrorCollector = NewBatchErrorCollector(config.BatchErrorSize, config.BatchFlushInterval)
113111
}
114112

@@ -131,15 +129,13 @@ func Configure(config Configuration) {
131129
if len(config.IgnoredEnvironments) > 0 {
132130
Config.IgnoredEnvironments = config.IgnoredEnvironments
133131
} else {
134-
// Default ignored environments: dev, test, testing
135132
defaultIgnoredEnvs := []string{"dev", "test", "testing"}
136133
Config.IgnoredEnvironments = getEnvAsSlice("TREBLLE_IGNORED_ENV", defaultIgnoredEnvs)
137134
}
138135

139136
Config.FieldsMap = generateFieldsToMask(Config.DefaultFieldsToMask, Config.AdditionalFieldsToMask)
140137
}
141138

142-
// getEnvMaskedFields reads masked fields from environment variable
143139
func getEnvMaskedFields() []string {
144140
fieldsStr := os.Getenv("TREBLLE_MASKED_FIELDS")
145141
if fieldsStr == "" {
@@ -148,7 +144,6 @@ func getEnvMaskedFields() []string {
148144
return strings.Split(fieldsStr, ",")
149145
}
150146

151-
// getDefaultFieldsToMask returns the default list of fields to mask
152147
func getDefaultFieldsToMask() []string {
153148
return []string{
154149
"password",
@@ -184,37 +179,30 @@ func generateFieldsToMask(defaultFields, additionalFields []string) map[string]b
184179
return fieldsToMask
185180
}
186181

187-
// Utility function to get env variable or return default
188182
func getEnvOrDefault(envKey, defaultValue string) string {
189183
if value := os.Getenv(envKey); value != "" {
190184
return value
191185
}
192186
return defaultValue
193187
}
194188

195-
// GetSDKInfo returns SDK name and version (for debugging)
196189
func GetSDKInfo() map[string]string {
197190
return map[string]string{
198191
"SDK Name": Config.SDKName,
199192
"SDK Version": strconv.FormatFloat(Config.SDKVersion, 'f', 2, 64),
200193
}
201194
}
202195

203-
// getEnvAsSlice reads a comma-separated environment variable and returns it as a slice
204-
// If the environment variable is not set, it returns the default values
205196
func getEnvAsSlice(envKey string, defaultValues []string) []string {
206197
if value := os.Getenv(envKey); value != "" {
207198
return strings.Split(value, ",")
208199
}
209200
return defaultValues
210201
}
211202

212-
// IsEnvironmentIgnored checks if the current environment should be ignored
213203
func IsEnvironmentIgnored() bool {
214-
// Get the current environment
215-
currentEnv := os.Getenv("GO_ENV") // Default Go environment variable
204+
currentEnv := os.Getenv("GO_ENV")
216205
if currentEnv == "" {
217-
// Try alternative environment variables if GO_ENV is not set
218206
currentEnv = os.Getenv("ENV")
219207
if currentEnv == "" {
220208
currentEnv = os.Getenv("ENVIRONMENT")
@@ -224,12 +212,10 @@ func IsEnvironmentIgnored() bool {
224212
}
225213
}
226214

227-
// If no environment is set, don't ignore
228215
if currentEnv == "" {
229216
return false
230217
}
231218

232-
// Check if the environment is in the ignored list
233219
for _, ignoredEnv := range Config.IgnoredEnvironments {
234220
if strings.TrimSpace(currentEnv) == strings.TrimSpace(ignoredEnv) {
235221
return true

configuration_ignored_env_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ func TestIgnoredEnvironments(t *testing.T) {
8888

8989
// Configure Treblle with test case settings
9090
config := Configuration{
91-
APIKey: "test-api-key",
92-
ProjectID: "test-project-id",
93-
IgnoredEnvironments: tc.ignoredEnvs,
91+
SDK_TOKEN: "test-sdk-token",
92+
API_KEY: "test-api-key",
93+
IgnoredEnvironments: tc.ignoredEnvs,
9494
}
9595
Configure(config)
9696

configuration_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
func TestSDKVersioning(t *testing.T) {
1111
// Initialize with default configuration
1212
Configure(Configuration{
13-
APIKey: "test-api-key",
14-
ProjectID: "test-project-id",
13+
SDK_TOKEN: "test-sdk-token",
14+
API_KEY: "test-api-key",
1515
})
1616

1717
// Ensure default version is correct

debug.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ func DebugCommand() {
1212
// Initialize configuration from environment variables if not already set
1313
if Config.APIKey == "" {
1414
// Read configuration from environment variables
15-
apiKey := getEnvOrDefault("TREBLLE_API_KEY", "")
16-
projectID := getEnvOrDefault("TREBLLE_PROJECT_ID", "")
15+
apiKey := getEnvOrDefault("TREBLLE_SDK_TOKEN", "")
16+
projectID := getEnvOrDefault("TREBLLE_API_KEY", "")
1717
endpoint := getEnvOrDefault("TREBLLE_ENDPOINT", "")
1818
ignoredEnvs := getEnvAsSlice("TREBLLE_IGNORED_ENVIRONMENTS", []string{"local", "development"})
1919

@@ -38,8 +38,8 @@ func DebugCommand() {
3838

3939
// Display basic SDK configuration
4040
fmt.Println("SDK Version:", strconv.FormatFloat(Config.SDKVersion, 'f', 1, 64))
41-
fmt.Println("Project ID:", maskString(Config.ProjectID))
42-
fmt.Println("API Key:", maskString(Config.APIKey))
41+
fmt.Println("API Key:", maskString(Config.ProjectID))
42+
fmt.Println("SDK Token:", maskString(Config.APIKey))
4343
fmt.Println("Configured Treblle URL:", getConfiguredEndpoint())
4444
fmt.Println("Ignored Environments:", Config.IgnoredEnvironments)
4545
}

examples/gorilla_example/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,9 @@ func getUsersHandler(w http.ResponseWriter, r *http.Request) {
8888
func main() {
8989
// Configure Treblle
9090
treblle.Configure(treblle.Configuration{
91-
APIKey: "Treblle API key", // Set your Treblle API key
92-
ProjectID: "Treblle Project ID", // Set your Treblle Project ID
91+
SDK_TOKEN: "Treblle SDK Token", // Set your Treblle SDK Token
92+
API_KEY: "Treblle API Key", // Set your Treblle API Key
93+
Debug: true, // Enable debug mode to see what's being sent to Treblle
9394
})
9495

9596
// Create a new router

examples/standard_example/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@ func createUserHandler(w http.ResponseWriter, r *http.Request) {
8181
func main() {
8282
// Configure Treblle
8383
treblle.Configure(treblle.Configuration{
84-
APIKey: "Treblle API key", // Set your Treblle API key
85-
ProjectID: "Treblle Project ID", // Set your Treblle Project ID
84+
SDK_TOKEN: "Treblle SDK Token", // Set your Treblle SDK Token
85+
API_KEY: "Treblle API Key", // Set your Treblle API Key
86+
Debug: true, // Enable debug mode to see what's being sent to Treblle
8687
})
8788

8889
// Create a new serve mux

examples/test_cli/main.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,23 @@ import (
1919

2020
func main() {
2121
// Set environment variables for testing
22-
os.Setenv("TREBLLE_API_KEY", "test_api_key_12345")
23-
os.Setenv("TREBLLE_PROJECT_ID", "test_project_id_67890")
22+
os.Setenv("TREBLLE_SDK_TOKEN", "test_api_key_12345")
23+
os.Setenv("TREBLLE_API_KEY", "test_project_id_67890")
2424
os.Setenv("TREBLLE_ENDPOINT", "https://test-api.treblle.com")
2525
os.Setenv("TREBLLE_IGNORED_ENVIRONMENTS", "local,testing")
2626

27-
// Configure Treblle with test values
27+
// Configure Treblle
2828
treblle.Configure(treblle.Configuration{
29-
APIKey: "test_api_key_12345",
30-
ProjectID: "test_project_id_67890",
31-
AdditionalFieldsToMask: []string{"custom_field", "secret_data"},
32-
MaskingEnabled: true,
29+
SDK_TOKEN: os.Getenv("TREBLLE_SDK_TOKEN"), // Get SDK Token from environment variable
30+
API_KEY: os.Getenv("TREBLLE_API_KEY"), // Get API Key from environment variable
31+
Debug: true, // Enable debug mode
32+
AdditionalFieldsToMask: []string{"custom_field", "another_field"},
33+
BatchErrorEnabled: true,
34+
BatchErrorSize: 5,
35+
BatchFlushInterval: 10 * time.Second,
3336
AsyncProcessingEnabled: true,
3437
MaxConcurrentProcessing: 5,
35-
AsyncShutdownTimeout: time.Second * 3,
38+
AsyncShutdownTimeout: 3 * time.Second,
3639
IgnoredEnvironments: []string{"local", "testing"},
3740
})
3841

examples/treblle-go-sdk-example/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,10 @@ func main() {
138138
}
139139

140140
treblle.Configure(treblle.Configuration{
141-
APIKey: os.Getenv("TREBLLE_API_KEY"),
142-
ProjectID: os.Getenv("TREBLLE_PROJECT_ID"),
143-
AdditionalFieldsToMask: []string{"bank_account", "routing_number", "tax_id", "auth_token", "ssn", "api_key", "password", "credit_card"},
141+
APIKey: os.Getenv("TREBLLE_SDK_TOKEN"),
142+
ProjectID: os.Getenv("TREBLLE_API_KEY"),
144143
Debug: debug == "true",
144+
AdditionalFieldsToMask: []string{"bank_account", "routing_number", "tax_id", "auth_token", "ssn", "api_key", "password", "credit_card"},
145145
})
146146

147147
r := mux.NewRouter()

metadata.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212
)
1313

1414
type MetaData struct {
15-
ApiKey string `json:"api_key"`
16-
ProjectID string `json:"project_id"`
15+
ApiKey string `json:"api_key"` // Renamed internally but kept the same JSON field name
16+
ProjectID string `json:"project_id"` // Renamed internally but kept the same JSON field name
1717
Version float64 `json:"version"`
1818
Sdk string `json:"sdk"`
1919
Data DataInfo `json:"data"`

middleware_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ func (s *TestSuite) SetupTest() {
3535
s.treblleMockMux = http.NewServeMux()
3636
s.treblleMockServer = httptest.NewServer(s.treblleMockMux)
3737
Configure(Configuration{
38-
APIKey: "test-api-key",
39-
ProjectID: "test-project",
38+
SDK_TOKEN: "test-sdk-token",
39+
API_KEY: "test-api-key",
4040
DefaultFieldsToMask: []string{
4141
"password",
4242
"api_key",
@@ -183,8 +183,8 @@ func (s *TestSuite) TestMiddleware() {
183183
log.Printf("Test case: %s, Mock URL: %s", tn, mockURL)
184184

185185
Configure(Configuration{
186-
APIKey: "test-api-key",
187-
ProjectID: "test-project-id",
186+
SDK_TOKEN: "test-sdk-token",
187+
API_KEY: "test-api-key",
188188
DefaultFieldsToMask: []string{"password"},
189189
Endpoint: mockURL,
190190
})
@@ -198,9 +198,9 @@ func (s *TestSuite) TestMiddleware() {
198198
log.Printf("Error decoding request body in mock server: %v", err)
199199
return
200200
}
201-
log.Printf("Received metadata - APIKey: %s, ProjectID: %s", treblleMetadata.ApiKey, treblleMetadata.ProjectID)
202-
s.Require().Equal("test-api-key", treblleMetadata.ApiKey)
203-
s.Require().Equal("test-project-id", treblleMetadata.ProjectID)
201+
log.Printf("Received metadata - SDK Token: %s, API Key: %s", treblleMetadata.ApiKey, treblleMetadata.ProjectID)
202+
s.Require().Equal("test-sdk-token", treblleMetadata.ApiKey)
203+
s.Require().Equal("test-api-key", treblleMetadata.ProjectID)
204204

205205
if tn == "non-json-response" {
206206
// For non-JSON responses, the body should be a JSON string
@@ -264,8 +264,8 @@ func (s *TestSuite) TestProtocolDetection() {
264264
mockURL := s.treblleMockServer.URL
265265

266266
Configure(Configuration{
267-
APIKey: "test-api-key",
268-
ProjectID: "test-project-id",
267+
SDK_TOKEN: "test-sdk-token",
268+
API_KEY: "test-api-key",
269269
DefaultFieldsToMask: []string{"password"},
270270
Endpoint: mockURL,
271271
})

shutdown_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
func TestShutdown(t *testing.T) {
1212
// Configure Treblle for testing
1313
Configure(Configuration{
14-
APIKey: "test-api-key",
15-
ProjectID: "test-project-id",
14+
SDK_TOKEN: "test-sdk-token",
15+
API_KEY: "test-api-key",
1616
Endpoint: "https://test-endpoint.treblle.com", // Use a test endpoint
1717
})
1818

@@ -92,12 +92,12 @@ func TestShutdown(t *testing.T) {
9292
func TestGracefulShutdown(t *testing.T) {
9393
// Configure Treblle with batch error collector
9494
Configure(Configuration{
95-
APIKey: "test-api-key",
96-
ProjectID: "test-project-id",
97-
Endpoint: "https://test-endpoint.treblle.com",
98-
BatchErrorEnabled: true,
99-
BatchErrorSize: 10,
100-
BatchFlushInterval: 5 * time.Second,
95+
SDK_TOKEN: "test-sdk-token",
96+
API_KEY: "test-api-key",
97+
Endpoint: "https://test-endpoint.treblle.com",
98+
BatchErrorEnabled: true,
99+
BatchErrorSize: 10,
100+
BatchFlushInterval: 5 * time.Second,
101101
})
102102

103103
// Add some errors to the batch collector

0 commit comments

Comments
 (0)