Skip to content

DOCSP-49330: Update Monitoring and Logging page with Go SDK examples #178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ A content repository for the Atlas Architecture Center documentation site.
## Report Issues

To file issues or requests regarding the documentation, go to the
`Documentation Jira Project <https://jira.mongodb.org/browse/DOCS>`_.
[Documentation Jira Project](https://jira.mongodb.org/browse/DOCS).

## License

All documentation is available under the terms of a `Creative Commons
License <https://creativecommons.org/licenses/by-nc-sa/3.0/>`_.
All documentation is available under the terms of a
[Creative Commons License](https://creativecommons.org/licenses/by-nc-sa/3.0/).

If you have any questions, please contact `docs@mongodb.com
<mailto:docs@mongodb.com>`_.
If you have any questions, please contact [docs@mongodb.com](mailto:docs@mongodb.com).

-- The MongoDB Documentation Team
7 changes: 7 additions & 0 deletions source/includes/complete-examples-go-sdk.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.. cta-banner::
:url: https://github.com/mongodb/atlas-architecture-go-sdk
:icon: Code

See the example scripts from the Atlas SDK for Go project in one place
`in Github <https://github.com/mongodb/atlas-architecture-go-sdk>`__.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
func main() {
ctx := context.Background()

// Create an Atlas client authenticated using OAuth2 with service account credentials
client, _, config, err := auth.CreateAtlasClient()
if err != nil {
log.Fatalf("Failed to create Atlas client: %v", err)
}

params := &admin.GetHostLogsApiParams{
GroupId: config.ProjectID,
HostName: config.HostName,
LogName: "mongodb", // The type of log to get ("mongodb" or "mongos")
}

logFileName, err := getHostLogs(ctx, *client, params)
if err != nil {
log.Fatalf("Failed to download logs: %v", err)
}

plainTextLog := strings.TrimSuffix(logFileName, ".gz") + ".log"
if err := unzipGzFile(logFileName, plainTextLog); err != nil {
log.Fatalf("Failed to unzip log file: %v", err)
}

}

112 changes: 112 additions & 0 deletions source/includes/examples/generated-examples/main.snippet.get-logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// See entire project at https://github.com/mongodb/atlas-architecture-go-sdk
package main

import (
"atlas-sdk-go/internal/auth"
"compress/gzip"
"context"
"fmt"
"io"
"log"
"os"
"strings"

"go.mongodb.org/atlas-sdk/v20250219001/admin"
)

func SafeClose(c io.Closer) {
if c != nil {
if err := c.Close(); err != nil {
log.Printf("Warning: failed to close resource: %v", err)
}
}
}

// getHostLogs downloads a compressed .gz file that contains the MongoDB logs for
// the specified host in your project.
func getHostLogs(ctx context.Context, atlasClient admin.APIClient, params *admin.GetHostLogsApiParams) (string, error) {
logFileName := fmt.Sprintf("logs_%s_%s.gz", params.GroupId, params.HostName)
fmt.Printf("Fetching %s log for host %s in project %s\n", params.LogName, params.HostName, params.GroupId)

if err := downloadLogs(ctx, atlasClient, params, logFileName); err != nil {
return "", err
}

fmt.Printf("Logs saved to %s\n", logFileName)
return logFileName, nil
}

func downloadLogs(ctx context.Context, atlasClient admin.APIClient, params *admin.GetHostLogsApiParams, filePath string) error {
resp, _, err := atlasClient.MonitoringAndLogsApi.GetHostLogsWithParams(ctx, params).Execute()
if err != nil {
return fmt.Errorf("fetch logs: %w", err)
}
defer SafeClose(resp)

file, err := os.Create(filePath)
if err != nil {
return fmt.Errorf("create %q: %w", filePath, err)
}
defer SafeClose(file)

if _, err := io.Copy(file, resp); err != nil {
return fmt.Errorf("write to %q: %w", filePath, err)
}

return nil
}

func unzipGzFile(srcPath, destPath string) error {
srcFile, err := os.Open(srcPath)
if err != nil {
return fmt.Errorf("open gz file: %w", err)
}
defer SafeClose(srcFile)

gzReader, err := gzip.NewReader(srcFile)
if err != nil {
return fmt.Errorf("create gzip reader: %w", err)
}
defer SafeClose(gzReader)

destFile, err := os.Create(destPath)
if err != nil {
return fmt.Errorf("create destination file: %w", err)
}
defer SafeClose(destFile)

if _, err := io.Copy(destFile, gzReader); err != nil {
return fmt.Errorf("unzip copy error: %w", err)
}

fmt.Printf("Unzipped logs to %s\n", destPath)
return nil
}

func main() {
ctx := context.Background()

// Create an Atlas client authenticated using OAuth2 with service account credentials
client, _, config, err := auth.CreateAtlasClient()
if err != nil {
log.Fatalf("Failed to create Atlas client: %v", err)
}

params := &admin.GetHostLogsApiParams{
GroupId: config.ProjectID,
HostName: config.HostName,
LogName: "mongodb", // The type of log to get ("mongodb" or "mongos")
}

logFileName, err := getHostLogs(ctx, *client, params)
if err != nil {
log.Fatalf("Failed to download logs: %v", err)
}

plainTextLog := strings.TrimSuffix(logFileName, ".gz") + ".log"
if err := unzipGzFile(logFileName, plainTextLog); err != nil {
log.Fatalf("Failed to unzip log file: %v", err)
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// See entire project at https://github.com/mongodb/atlas-architecture-go-sdk
package main

import (
"atlas-sdk-go/internal/auth"
"context"
"encoding/json"
"fmt"
"go.mongodb.org/atlas-sdk/v20250219001/admin"
"log"
)

// getDiskMetrics fetches metrics for a specified disk partition in a project and prints results to the console
func getDiskMetrics(ctx context.Context, atlasClient admin.APIClient, params *admin.GetDiskMeasurementsApiParams) (*admin.ApiMeasurementsGeneralViewAtlas, error) {

resp, _, err := atlasClient.MonitoringAndLogsApi.GetDiskMeasurementsWithParams(ctx, params).Execute()
if err != nil {
if apiError, ok := admin.AsError(err); ok {
return nil, fmt.Errorf("failed to get metrics for partition: %s (API error: %v)", err, apiError.GetDetail())
}
return nil, fmt.Errorf("failed to get metrics: %w", err)
}
if resp == nil || resp.HasMeasurements() == false {
return nil, fmt.Errorf("no metrics found for partition %s in project %s", params.PartitionName, params.GroupId)
}
jsonData, err := json.MarshalIndent(resp, "", " ")
if err != nil {
return nil, fmt.Errorf("failed to marshal response: %w", err)
}
fmt.Println(string(jsonData))
return resp, nil
}

func main() {
ctx := context.Background()

// Create an Atlas client authenticated using OAuth2 with service account credentials
atlasClient, _, config, err := auth.CreateAtlasClient()
if err != nil {
log.Fatalf("Failed to create Atlas client: %v", err)
}

// Fetch disk metrics using the following parameters:
partitionName := "data"
diskMetricsGranularity := admin.PtrString("P1D")
diskMetricsPeriod := admin.PtrString("P1D")
diskMetrics := []string{
"DISK_PARTITION_SPACE_FREE", "DISK_PARTITION_SPACE_USED",
}

diskMeasurementsParams := &admin.GetDiskMeasurementsApiParams{
GroupId: config.ProjectID,
ProcessId: config.ProcessID,
PartitionName: partitionName,
M: &diskMetrics,
Granularity: diskMetricsGranularity,
Period: diskMetricsPeriod,
}
_, err = getDiskMetrics(ctx, *atlasClient, diskMeasurementsParams)
if err != nil {
fmt.Printf("Error fetching disk metrics: %v", err)
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
func main() {
ctx := context.Background()

// Create an Atlas client authenticated using OAuth2 with service account credentials
atlasClient, _, config, err := auth.CreateAtlasClient()
if err != nil {
log.Fatalf("Failed to create Atlas client: %v", err)
}

// Fetch disk metrics using the following parameters:
partitionName := "data"
diskMetricsGranularity := admin.PtrString("P1D")
diskMetricsPeriod := admin.PtrString("P1D")
diskMetrics := []string{
"DISK_PARTITION_SPACE_FREE", "DISK_PARTITION_SPACE_USED",
}

diskMeasurementsParams := &admin.GetDiskMeasurementsApiParams{
GroupId: config.ProjectID,
ProcessId: config.ProcessID,
PartitionName: partitionName,
M: &diskMetrics,
Granularity: diskMetricsGranularity,
Period: diskMetricsPeriod,
}
_, err = getDiskMetrics(ctx, *atlasClient, diskMeasurementsParams)
if err != nil {
fmt.Printf("Error fetching disk metrics: %v", err)
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
func main() {
ctx := context.Background()

// Create an Atlas client authenticated using OAuth2 with service account credentials
atlasClient, _, config, err := auth.CreateAtlasClient()
if err != nil {
log.Fatalf("Failed to create Atlas client: %v", err)
}

// Fetch process metrics using the following parameters:
processMetricGranularity := admin.PtrString("PT1H")
processMetricPeriod := admin.PtrString("P7D")
processMetrics := []string{
"OPCOUNTER_INSERT", "OPCOUNTER_QUERY", "OPCOUNTER_UPDATE", "TICKETS_AVAILABLE_READS",
"TICKETS_AVAILABLE_WRITE", "CONNECTIONS", "QUERY_TARGETING_SCANNED_OBJECTS_PER_RETURNED",
"QUERY_TARGETING_SCANNED_PER_RETURNED", "SYSTEM_CPU_GUEST", "SYSTEM_CPU_IOWAIT",
"SYSTEM_CPU_IRQ", "SYSTEM_CPU_KERNEL", "SYSTEM_CPU_NICE", "SYSTEM_CPU_SOFTIRQ",
"SYSTEM_CPU_STEAL", "SYSTEM_CPU_USER",
}
hostMeasurementsParams := &admin.GetHostMeasurementsApiParams{
GroupId: config.ProjectID,
ProcessId: config.ProcessID,
M: &processMetrics,
Granularity: processMetricGranularity,
Period: processMetricPeriod,
}
_, err = getProcessMetrics(ctx, *atlasClient, hostMeasurementsParams)
if err != nil {
fmt.Printf("Error fetching host process metrics: %v", err)
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// See entire project at https://github.com/mongodb/atlas-architecture-go-sdk
package main

import (
"atlas-sdk-go/internal/auth"
"context"
"encoding/json"
"fmt"
"go.mongodb.org/atlas-sdk/v20250219001/admin"
"log"
)

// getProcessMetrics fetches metrics for a specified host process in a project and prints results to the console
func getProcessMetrics(ctx context.Context, atlasClient admin.APIClient, params *admin.GetHostMeasurementsApiParams) (*admin.ApiMeasurementsGeneralViewAtlas, error) {
fmt.Printf("Fetching metrics for host process %s in project %s", params.ProcessId, params.GroupId)

resp, _, err := atlasClient.MonitoringAndLogsApi.GetHostMeasurementsWithParams(ctx, params).Execute()
if err != nil {
if apiError, ok := admin.AsError(err); ok {
return nil, fmt.Errorf("failed to get metrics for process in host: %s (API error: %v)", err, apiError.GetDetail())
}
return nil, fmt.Errorf("failed to get metrics: %w", err)
}

if resp == nil || resp.HasMeasurements() == false {
return nil, fmt.Errorf("no metrics found for host process %s in project %s", params.ProcessId, params.GroupId)
}
jsonData, err := json.MarshalIndent(resp, "", " ")
if err != nil {
return nil, fmt.Errorf("failed to marshal response: %w", err)
}
fmt.Println(string(jsonData))
return resp, nil
}

func main() {
ctx := context.Background()

// Create an Atlas client authenticated using OAuth2 with service account credentials
atlasClient, _, config, err := auth.CreateAtlasClient()
if err != nil {
log.Fatalf("Failed to create Atlas client: %v", err)
}

// Fetch process metrics using the following parameters:
processMetricGranularity := admin.PtrString("PT1H")
processMetricPeriod := admin.PtrString("P7D")
processMetrics := []string{
"OPCOUNTER_INSERT", "OPCOUNTER_QUERY", "OPCOUNTER_UPDATE", "TICKETS_AVAILABLE_READS",
"TICKETS_AVAILABLE_WRITE", "CONNECTIONS", "QUERY_TARGETING_SCANNED_OBJECTS_PER_RETURNED",
"QUERY_TARGETING_SCANNED_PER_RETURNED", "SYSTEM_CPU_GUEST", "SYSTEM_CPU_IOWAIT",
"SYSTEM_CPU_IRQ", "SYSTEM_CPU_KERNEL", "SYSTEM_CPU_NICE", "SYSTEM_CPU_SOFTIRQ",
"SYSTEM_CPU_STEAL", "SYSTEM_CPU_USER",
}
hostMeasurementsParams := &admin.GetHostMeasurementsApiParams{
GroupId: config.ProjectID,
ProcessId: config.ProcessID,
M: &processMetrics,
Granularity: processMetricGranularity,
Period: processMetricPeriod,
}
_, err = getProcessMetrics(ctx, *atlasClient, hostMeasurementsParams)
if err != nil {
fmt.Printf("Error fetching host process metrics: %v", err)
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"MONGODB_ATLAS_BASE_URL": "https://cloud.mongodb.com",
"ATLAS_ORG_ID": "32b6e34b3d91647abb20e7b8",
"ATLAS_PROJECT_ID": "67212db237c5766221eb6ad9",
"ATLAS_CLUSTER_NAME": "myCluster",
"ATLAS_PROCESS_ID": "myCluster-shard-00-00.ajlj3.mongodb.net:27017"
}
Loading