Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
9c9b69e
update existing to use new load function and add archiving example fo…
cbullinger Aug 6, 2025
e86d99c
Add programmatic scaling example for Scalability page
cbullinger Aug 6, 2025
340ce32
Add archive example for Scalability page
cbullinger Aug 14, 2025
26ce74e
Snip and copy files
cbullinger Aug 14, 2025
ff1424d
Update README
cbullinger Aug 14, 2025
7662323
Fix up comments
cbullinger Aug 14, 2025
de6e2af
Merge branch 'main' of ssh://github.com/mongodb/docs-code-examples in…
cbullinger Aug 15, 2025
901d194
Simplify config internals, and move out env load to main
cbullinger Aug 18, 2025
0110e97
Re-generate snippets
cbullinger Aug 19, 2025
b385fc7
remove extra keys from configs
cbullinger Aug 19, 2025
9eeebad
clean up comments and debugging; re-generate snippets
cbullinger Aug 19, 2025
81d88af
Update main callers with simplified env loading
cbullinger Aug 20, 2025
117ee36
Re-generate snippets...again...
cbullinger Aug 20, 2025
8717025
Fix failing test
cbullinger Aug 20, 2025
ef1eb45
Apply Dachary feedback 2
cbullinger Aug 21, 2025
7eed57d
Update usage-examples/go/atlas-sdk-go/examples/performance/archiving/…
cbullinger Aug 21, 2025
5fabaa3
Apply suggestion from @dacharyc
cbullinger Aug 21, 2025
39cc41b
Remove redundant ValidateCandidate calls
cbullinger Aug 21, 2025
eb03409
Merge branch 'docsp-46224-46228' of ssh://github.com/cbullinger/docs-…
cbullinger Aug 21, 2025
7b1e73c
Re-generate copied files
cbullinger Aug 21, 2025
28416f9
Re-generate files ... again
cbullinger Aug 21, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// See entire project at https://github.com/mongodb/atlas-architecture-go-sdk
package main

import (
"context"
"fmt"
"log"
"time"

"atlas-sdk-go/internal/archive"
"atlas-sdk-go/internal/auth"
"atlas-sdk-go/internal/config"

"github.com/joho/godotenv"
)

func main() {
envFile := ".env.production"
if err := godotenv.Load(envFile); err != nil {
log.Printf("Warning: could not load %s file: %v", envFile, err)
}

secrets, cfg, err := config.LoadAllFromEnv()
if err != nil {
log.Fatalf("Failed to load configuration %v", err)
}

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
defer cancel()
client, err := auth.NewClient(ctx, cfg, secrets)
if err != nil {
log.Fatalf("Failed to initialize authentication client: %v", err)
}

projectID := cfg.ProjectID
if projectID == "" {
log.Fatal("Failed to find Project ID in configuration")
}

fmt.Printf("Starting archive analysis for project: %s\n", projectID)

// Get all clusters in the project
clusters, _, err := client.ClustersApi.ListClusters(ctx, projectID).Execute()
if err != nil {
log.Fatalf("Failed to list clusters: %v", err)
}

fmt.Printf("\nFound %d clusters to analyze\n", len(clusters.GetResults()))

// Connect to each cluster and analyze collections for archiving
failedArchives := 0
skippedCandidates := 0
totalCandidates := 0

// Create archive options with custom settings
opts := archive.DefaultOptions()
opts.DefaultRetentionMultiplier = 2
opts.MinimumRetentionDays = 30
opts.EnableDataExpiration = true
opts.ArchiveSchedule = "DAILY"

for _, cluster := range clusters.GetResults() {
clusterName := cluster.GetName()
fmt.Printf("\n=== Analyzing cluster: %s ===", clusterName)

// Find collections suitable for archiving based on demo criteria.
// This simplified example first selects all collections with counts, and then filters them.
// NOTE: In a real implementation, you would analyze collections based on size, age,
// access patterns, and other factors to determine candidates for archiving.
stats := archive.ListCollectionsWithCounts(ctx, client, projectID, clusterName)
candidates := make([]archive.Candidate, 0)
const docThreshold = 100000
for _, s := range stats {
// Skip internal databases
if s.DatabaseName == "admin" || s.DatabaseName == "local" || s.DatabaseName == "config" {
continue
}
// Demo criterion: collections with >= 100k documents
if s.EstimatedCount >= docThreshold {
candidates = append(candidates, archive.Candidate{
DatabaseName: s.DatabaseName,
CollectionName: s.CollectionName,
DateField: "createdAt",
DateFormat: "DATE",
RetentionDays: 90,
PartitionFields: []string{"createdAt"},
})
}
}
totalCandidates += len(candidates)
fmt.Printf("\nFound %d collections eligible for archiving in cluster %s\n",
len(candidates), clusterName)

// Configure online archive for each candidate collection
for _, candidate := range candidates {
// Pre-validate candidate before attempting configuration
if err := archive.ValidateCandidate(candidate, opts); err != nil {
fmt.Printf("- Skipping %s.%s: invalid candidate: %v\n",
candidate.DatabaseName, candidate.CollectionName, err)
skippedCandidates++
continue
}

fmt.Printf("- Configuring archive for %s.%s\n",
candidate.DatabaseName, candidate.CollectionName)

configureErr := archive.ConfigureOnlineArchive(ctx, client, projectID, clusterName, candidate, opts)
if configureErr != nil {
fmt.Printf(" Failed to configure archive: %v\n", configureErr)
failedArchives++
continue
}

fmt.Printf(" Successfully configured online archive for %s.%s\n",
candidate.DatabaseName, candidate.CollectionName)
}
}

if skippedCandidates > 0 {
fmt.Printf("\nINFO: Skipped %d of %d candidates due to validation errors\n", skippedCandidates, totalCandidates)
}
if failedArchives > 0 {
fmt.Printf("WARNING: %d of %d archive configurations failed (excluding skipped)\n", failedArchives, totalCandidates-skippedCandidates)
}

fmt.Println("Archive analysis and configuration completed.")
}

30 changes: 16 additions & 14 deletions generated-usage-examples/go/atlas-sdk-go/main.snippet.get-logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"atlas-sdk-go/internal/auth"
"atlas-sdk-go/internal/config"
"atlas-sdk-go/internal/errors"
"atlas-sdk-go/internal/fileutils"
"atlas-sdk-go/internal/logs"

Expand All @@ -17,55 +16,58 @@ import (
)

func main() {
if err := godotenv.Load(); err != nil {
log.Printf("Warning: .env file not loaded: %v", err)
envFile := ".env.production"
if err := godotenv.Load(envFile); err != nil {
log.Printf("Warning: could not load %s file: %v", envFile, err)
}

secrets, cfg, err := config.LoadAll("configs/config.json")
secrets, cfg, err := config.LoadAllFromEnv()
if err != nil {
errors.ExitWithError("Failed to load configuration", err)
log.Fatalf("Failed to load configuration %v", err)
}

client, err := auth.NewClient(cfg, secrets)
ctx := context.Background()
client, err := auth.NewClient(ctx, cfg, secrets)
if err != nil {
errors.ExitWithError("Failed to initialize authentication client", err)
log.Fatalf("Failed to initialize authentication client: %v", err)
}

ctx := context.Background()

// Fetch logs with the provided parameters
p := &admin.GetHostLogsApiParams{
GroupId: cfg.ProjectID,
HostName: cfg.HostName,
LogName: "mongodb",
}
fmt.Printf("Request parameters: GroupID=%s, HostName=%s, LogName=%s\n",
cfg.ProjectID, cfg.HostName, p.LogName)
rc, err := logs.FetchHostLogs(ctx, client.MonitoringAndLogsApi, p)
if err != nil {
errors.ExitWithError("Failed to fetch logs", err)
log.Fatalf("Failed to fetch logs: %v", err)
}
defer fileutils.SafeClose(rc)

// Prepare output paths
// If the ATLAS_DOWNLOADS_DIR env variable is set, it will be used as the base directory for output files
outDir := "logs"
prefix := fmt.Sprintf("%s_%s", p.HostName, p.LogName)
gzPath, err := fileutils.GenerateOutputPath(outDir, prefix, "gz")
if err != nil {
errors.ExitWithError("Failed to generate GZ output path", err)
log.Fatalf("Failed to generate GZ output path: %v", err)
}
txtPath, err := fileutils.GenerateOutputPath(outDir, prefix, "txt")
if err != nil {
errors.ExitWithError("Failed to generate TXT output path", err)
log.Fatalf("Failed to generate TXT output path: %v", err)
}

// Save compressed logs
if err := fileutils.WriteToFile(rc, gzPath); err != nil {
errors.ExitWithError("Failed to save compressed logs", err)
log.Fatalf("Failed to save compressed logs: %v", err)
}
fmt.Println("Saved compressed log to", gzPath)

// Decompress logs
if err := fileutils.DecompressGzip(gzPath, txtPath); err != nil {
errors.ExitWithError("Failed to decompress logs", err)
log.Fatalf("Failed to decompress logs: %v", err)
}
fmt.Println("Uncompressed log to", txtPath)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,29 @@ import (

"atlas-sdk-go/internal/auth"
"atlas-sdk-go/internal/config"
"atlas-sdk-go/internal/errors"
"atlas-sdk-go/internal/metrics"

"github.com/joho/godotenv"
"go.mongodb.org/atlas-sdk/v20250219001/admin"
)

func main() {
if err := godotenv.Load(); err != nil {
log.Printf("Warning: .env file not loaded: %v", err)
envFile := ".env.development"
if err := godotenv.Load(envFile); err != nil {
log.Printf("Warning: could not load %s file: %v", envFile, err)
}

secrets, cfg, err := config.LoadAll("configs/config.json")
secrets, cfg, err := config.LoadAllFromEnv()
if err != nil {
errors.ExitWithError("Failed to load configuration", err)
log.Fatalf("Failed to load configuration %v", err)
}

client, err := auth.NewClient(cfg, secrets)
ctx := context.Background()
client, err := auth.NewClient(ctx, cfg, secrets)
if err != nil {
errors.ExitWithError("Failed to initialize authentication client", err)
log.Fatalf("Failed to initialize authentication client: %v", err)
}

ctx := context.Background()

// Fetch disk metrics with the provided parameters
p := &admin.GetDiskMeasurementsApiParams{
GroupId: cfg.ProjectID,
Expand All @@ -44,13 +43,13 @@ func main() {
}
view, err := metrics.FetchDiskMetrics(ctx, client.MonitoringAndLogsApi, p)
if err != nil {
errors.ExitWithError("Failed to fetch disk metrics", err)
log.Fatalf("Failed to fetch disk metrics: %v", err)
}

// Output metrics
out, err := json.MarshalIndent(view, "", " ")
if err != nil {
errors.ExitWithError("Failed to format metrics data", err)
log.Fatalf("Failed to format metrics data: %v", err)
}
fmt.Println(string(out))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"fmt"
"log"

"atlas-sdk-go/internal/errors"

"atlas-sdk-go/internal/auth"
"atlas-sdk-go/internal/config"

Expand All @@ -19,22 +17,22 @@ import (
)

func main() {
if err := godotenv.Load(); err != nil {
log.Printf("Warning: .env file not loaded: %v", err)
envFile := ".env.production"
if err := godotenv.Load(envFile); err != nil {
log.Printf("Warning: could not load %s file: %v", envFile, err)
}

secrets, cfg, err := config.LoadAll("configs/config.json")
secrets, cfg, err := config.LoadAllFromEnv()
if err != nil {
errors.ExitWithError("Failed to load configuration", err)
log.Fatalf("Failed to load configuration %v", err)
}

client, err := auth.NewClient(cfg, secrets)
ctx := context.Background()
client, err := auth.NewClient(ctx, cfg, secrets)
if err != nil {
errors.ExitWithError("Failed to initialize authentication client", err)
log.Fatalf("Failed to initialize authentication client: %v", err)
}

ctx := context.Background()

// Fetch process metrics with the provided parameters
p := &admin.GetHostMeasurementsApiParams{
GroupId: cfg.ProjectID,
Expand All @@ -52,13 +50,13 @@ func main() {

view, err := metrics.FetchProcessMetrics(ctx, client.MonitoringAndLogsApi, p)
if err != nil {
errors.ExitWithError("Failed to fetch process metrics", err)
log.Fatalf("Failed to fetch process metrics: %v", err)
}

// Output metrics
out, err := json.MarshalIndent(view, "", " ")
if err != nil {
errors.ExitWithError("Failed to format metrics data", err)
log.Fatalf("Failed to format metrics data: %v", err)
}
fmt.Println(string(out))
}
Expand Down
Loading
Loading