Skip to content

Commit 221821d

Browse files
committed
requested changes
1 parent 89668a0 commit 221821d

File tree

3 files changed

+37
-7
lines changed

3 files changed

+37
-7
lines changed

.chloggen/feat--Add-support-for-atlasgov.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ change_type: enhancement
77
component: mongodbatlasreceiver
88

99
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10-
note: "Add support for AtlasGov"
10+
note: "Add support for setting custom base URL for the MongoDB Atlas API"
1111

1212
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
1313
issues: [39345]
@@ -24,4 +24,4 @@ subtext:
2424
# Include 'user' if the change is relevant to end users.
2525
# Include 'api' if there is a change to a library API.
2626
# Default: '[user]'
27-
change_logs: [api]
27+
change_logs: [user]

receiver/mongodbatlasreceiver/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ In order to collect access logs, the requesting API key needs the appropriate pe
3434

3535
MongoDB Atlas [Documentation](https://www.mongodb.com/docs/atlas/reference/api/logs/#logs) recommends a polling interval of 5 minutes.
3636

37-
- `base_url` (optional) set to https://cloud.mongodbgov.com to connecto to Atlas Gov
37+
- `base_url` (optional) set the base URL to connect to to Atlas Cloud
3838
- `public_key` (required for metrics, logs, or alerts in `poll` mode)
3939
- `private_key` (required for metrics, logs, or alerts in `poll` mode)
4040
- `granularity` (default `PT1M` - See [MongoDB Atlas Documentation](https://docs.atlas.mongodb.com/reference/api/process-measurements/))

receiver/mongodbatlasreceiver/internal/mongodb_atlas_client.go

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"errors"
1010
"fmt"
1111
"net/http"
12+
"net/url"
1213
"strconv"
1314
"sync"
1415
"time"
@@ -142,15 +143,32 @@ func NewMongoDBAtlasClient(
142143
tc := &http.Client{Transport: roundTripper}
143144

144145
if baseURL == "" {
145-
baseURL = "https://cloud.mongodb.com"
146+
baseURL = mongodbatlas.CloudURL
146147
}
147148

148-
log.Debug("Creating MongoDB Atlas client",
149-
zap.String("baseURL", baseURL))
149+
if !isValidURL(baseURL) {
150+
log.Error("Invalid baseURL provided, using default:", zap.String("providedURL", baseURL), zap.String("defaultURL", mongodbatlas.CloudURL))
151+
baseURL = mongodbatlas.CloudURL // Fallback to the default if invalid
152+
}
150153

151154
client, err := mongodbatlas.New(tc, mongodbatlas.SetBaseURL(baseURL))
152155
if err != nil {
153-
log.Info("failed to create client", zap.Error(err))
156+
log.Error("Failed to create client", zap.Error(err))
157+
}
158+
159+
log.Info("Testing MongoDB Atlas API connection",
160+
zap.String("baseURL", baseURL))
161+
162+
// Test the connection by making a simple API call (list organizations)
163+
_, _, err = client.Organizations.List(context.Background(), &mongodbatlas.OrganizationsListOptions{})
164+
if err != nil {
165+
log.Error("Failed to connect to MongoDB Atlas API. Possible invalid base URL or credentials:",
166+
zap.String("baseURL", baseURL),
167+
zap.Error(err), // Include the underlying error for more details
168+
)
169+
} else {
170+
log.Info("Successfully connected to MongoDB Atlas API",
171+
zap.String("baseURL", baseURL))
154172
}
155173

156174
return &MongoDBAtlasClient{
@@ -161,6 +179,18 @@ func NewMongoDBAtlasClient(
161179
}
162180
}
163181

182+
// isValidURL checks if a string is a valid URL.
183+
func isValidURL(str string) bool {
184+
u, err := url.ParseRequestURI(str)
185+
if err != nil {
186+
return false
187+
}
188+
if u.Scheme == "" || u.Host == "" {
189+
return false
190+
}
191+
return true
192+
}
193+
164194
func (s *MongoDBAtlasClient) Shutdown() error {
165195
s.transport.CloseIdleConnections()
166196
return s.roundTripper.Shutdown()

0 commit comments

Comments
 (0)