Skip to content
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
test.bash

/bin
/ctrlc
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Verify the ignore pattern is correct.

The pattern /ctrlc ignores a file at the repository root, but based on the AI summary and the existing /bin entry, you likely intended /bin/ctrlc to ignore the built binary under the bin directory.

Apply this diff if the binary is built to bin/ctrlc:

 /bin
-/ctrlc
+/bin/ctrlc
 .idea
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
/ctrlc
/bin
/bin/ctrlc
.idea
🤖 Prompt for AI Agents
In .gitignore around line 5, the pattern "/ctrlc" ignores a file at repo root
but you intended to ignore the built binary under bin; update the pattern to
"/bin/ctrlc" (or the actual build path if different) so the built binary in the
bin directory is ignored, and verify there are no duplicate or conflicting
entries for /bin elsewhere in the file.

.idea
.vscode
*.test.yaml
48 changes: 47 additions & 1 deletion cmd/ctrlc/ctrlc.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package main

import (
"context"
"os"
"strings"
"time"

"github.com/charmbracelet/log"
"github.com/ctrlplanedev/cli/cmd/ctrlc/root"
"github.com/ctrlplanedev/cli/internal/telemetry"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -36,9 +40,51 @@ func init() {
}

func main() {
if err := cmd.Execute(); err != nil {
ctx := context.Background()

// Initialize telemetry
shutdown, err := telemetry.InitTelemetry(ctx)
if err != nil {
log.Warn("Failed to initialize telemetry", "error", err)
// Continue execution even if telemetry fails
}

// Ensure telemetry is properly shut down
if shutdown != nil {
defer func() {
// Give a brief moment for any pending spans to be exported
shutdownCtx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
if shutdownErr := shutdown(shutdownCtx); shutdownErr != nil {
log.Debug("Error during telemetry shutdown", "error", shutdownErr)
}
}()
}

// Determine command name for the root span
commandName := "help"
if len(os.Args) > 1 {
commandName = strings.Join(os.Args[1:], " ")
}

// Start root span
ctx, rootSpan := telemetry.StartRootSpan(ctx, commandName, os.Args[1:])
defer rootSpan.End()

// Execute command with telemetry context
if err := executeWithTelemetry(ctx, cmd); err != nil {
telemetry.SetSpanError(rootSpan, err)
os.Exit(1)
}

telemetry.SetSpanSuccess(rootSpan)
}

// executeWithTelemetry wraps the command execution with telemetry context
func executeWithTelemetry(ctx context.Context, cmd *cobra.Command) error {
// Set the context in the command so it can be used by subcommands
cmd.SetContext(ctx)
return cmd.Execute()
}

func initConfig() {
Expand Down
17 changes: 17 additions & 0 deletions cmd/ctrlc/root/sync/aws/ec2/ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ import (
"github.com/charmbracelet/log"
"github.com/ctrlplanedev/cli/internal/api"
"github.com/ctrlplanedev/cli/internal/cliutil"
"github.com/ctrlplanedev/cli/internal/telemetry"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)

type ConnectionMethod struct {
Expand Down Expand Up @@ -92,11 +95,22 @@ func NewSyncEC2Cmd() *cobra.Command {
workspaceId := viper.GetString("workspace")

// Get EC2 instances
ctx, span := telemetry.StartSpan(ctx, "aws.ec2.describe_instances",
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(
attribute.String("aws.region", region),
),
)
defer span.End()

result, err := ec2Client.DescribeInstances(ctx, &ec2.DescribeInstancesInput{})
if err != nil {
telemetry.SetSpanError(span, err)
return fmt.Errorf("failed to describe instances: %w", err)
}

telemetry.AddSpanAttribute(span, "aws.ec2.reservations_count", len(result.Reservations))

resources := []api.CreateResource{}
for _, reservation := range result.Reservations {
accountId := *reservation.OwnerId
Expand Down Expand Up @@ -242,6 +256,9 @@ func NewSyncEC2Cmd() *cobra.Command {
}
}

telemetry.AddSpanAttribute(span, "aws.ec2.instances_processed", len(resources))
telemetry.SetSpanSuccess(span)

// Create or update resource provider
if name == "" {
name = fmt.Sprintf("aws-ec2-region-%s", region)
Expand Down
49 changes: 47 additions & 2 deletions cmd/ctrlc/root/sync/aws/eks/eks.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ import (
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/sync/aws/common"
"github.com/ctrlplanedev/cli/internal/api"
"github.com/ctrlplanedev/cli/internal/kinds"
"github.com/ctrlplanedev/cli/internal/telemetry"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)

// NewSyncEKSCmd creates a new cobra command for syncing EKS clusters
Expand Down Expand Up @@ -131,31 +134,70 @@ func initEKSClient(ctx context.Context, region string) (*eks.Client, aws.Config,
}

func processClusters(ctx context.Context, eksClient *eks.Client, region string, cfg aws.Config) ([]api.CreateResource, error) {
ctx, span := telemetry.StartSpan(ctx, "aws.eks.process_clusters",
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(
attribute.String("aws.region", region),
),
)
defer span.End()

var resources []api.CreateResource
var nextToken *string

accountID, err := common.GetAccountID(ctx, cfg)
if err != nil {
telemetry.SetSpanError(span, err)
return nil, fmt.Errorf("failed to get AWS account ID: %w", err)
}

for {
resp, err := eksClient.ListClusters(ctx, &eks.ListClustersInput{
// Create span for ListClusters call
listCtx, listSpan := telemetry.StartSpan(ctx, "aws.eks.list_clusters",
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(
attribute.String("aws.region", region),
),
)

resp, err := eksClient.ListClusters(listCtx, &eks.ListClustersInput{
NextToken: nextToken,
})

if err != nil {
telemetry.SetSpanError(listSpan, err)
listSpan.End()
return nil, fmt.Errorf("failed to list EKS clusters: %w", err)
}

telemetry.AddSpanAttribute(listSpan, "aws.eks.clusters_found", len(resp.Clusters))
telemetry.SetSpanSuccess(listSpan)
listSpan.End()

for _, clusterName := range resp.Clusters {
cluster, err := eksClient.DescribeCluster(ctx, &eks.DescribeClusterInput{
// Create span for DescribeCluster call
descCtx, descSpan := telemetry.StartSpan(ctx, "aws.eks.describe_cluster",
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(
attribute.String("aws.region", region),
attribute.String("aws.eks.cluster_name", clusterName),
),
)

cluster, err := eksClient.DescribeCluster(descCtx, &eks.DescribeClusterInput{
Name: &clusterName,
})

if err != nil {
log.Error("Failed to describe cluster", "name", clusterName, "error", err)
telemetry.SetSpanError(descSpan, err)
descSpan.End()
continue
}

telemetry.SetSpanSuccess(descSpan)
descSpan.End()

resource, err := processCluster(ctx, cluster.Cluster, region, accountID)
if err != nil {
log.Error("Failed to process EKS cluster", "name", clusterName, "error", err)
Expand All @@ -170,6 +212,9 @@ func processClusters(ctx context.Context, eksClient *eks.Client, region string,
nextToken = resp.NextToken
}

telemetry.AddSpanAttribute(span, "aws.eks.total_clusters", len(resources))
telemetry.SetSpanSuccess(span)

log.Info("Found EKS clusters", "region", region, "count", len(resources))
return resources, nil
}
Expand Down
53 changes: 51 additions & 2 deletions cmd/ctrlc/root/sync/aws/networks/networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ import (
"github.com/charmbracelet/log"
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/sync/aws/common"
"github.com/ctrlplanedev/cli/internal/api"
"github.com/ctrlplanedev/cli/internal/telemetry"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"os"
"strconv"
"sync"
Expand Down Expand Up @@ -174,6 +177,14 @@ func initComputeClient(ctx context.Context, region string) (*ec2.Client, aws.Con
func processNetworks(
ctx context.Context, ec2Client *ec2.Client, awsSubnets []types.Subnet, region string, accountId string,
) ([]api.CreateResource, error) {
ctx, span := telemetry.StartSpan(ctx, "aws.networks.process_networks",
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(
attribute.String("aws.region", region),
),
)
defer span.End()

var nextToken *string
vpcs := make([]types.Vpc, 0)
subnetsByVpc := make(map[string][]types.Subnet)
Expand All @@ -189,13 +200,27 @@ func processNetworks(
}

for {
output, err := ec2Client.DescribeVpcs(ctx, &ec2.DescribeVpcsInput{
listCtx, listSpan := telemetry.StartSpan(ctx, "aws.networks.describe_vpcs",
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(
attribute.String("aws.region", region),
),
)

output, err := ec2Client.DescribeVpcs(listCtx, &ec2.DescribeVpcsInput{
NextToken: nextToken,
})
if err != nil {
telemetry.SetSpanError(listSpan, err)
listSpan.End()
telemetry.SetSpanError(span, err)
return nil, fmt.Errorf("failed to list VPCs: %w", err)
}

telemetry.AddSpanAttribute(listSpan, "aws.networks.vpcs_found", len(output.Vpcs))
telemetry.SetSpanSuccess(listSpan)
listSpan.End()

vpcs = append(vpcs, output.Vpcs...)
if output.NextToken == nil {
break
Expand All @@ -218,6 +243,8 @@ func processNetworks(
resources = append(resources, resource)
}

telemetry.AddSpanAttribute(span, "aws.networks.vpcs_processed", len(resources))
telemetry.SetSpanSuccess(span)
return resources, nil
}

Expand Down Expand Up @@ -287,6 +314,14 @@ func initNetworkMetadata(vpc types.Vpc, region string, subnetCount int) map[stri
// getSubnetsForVpc retrieves subnets as AWS SDK objects
// these objects are processed differently for VPC and subnet resources
func getAwsSubnets(ctx context.Context, ec2Client *ec2.Client, region string) ([]types.Subnet, error) {
ctx, span := telemetry.StartSpan(ctx, "aws.networks.get_subnets",
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(
attribute.String("aws.region", region),
),
)
defer span.End()

var subnets []types.Subnet
var nextToken *string

Expand All @@ -298,6 +333,7 @@ func getAwsSubnets(ctx context.Context, ec2Client *ec2.Client, region string) ([

subnetsOutput, err := ec2Client.DescribeSubnets(ctx, subnetInput)
if err != nil {
telemetry.SetSpanError(span, err)
return nil, fmt.Errorf("failed to list subnets at region %s: %w", region, err)
}

Expand All @@ -308,11 +344,22 @@ func getAwsSubnets(ctx context.Context, ec2Client *ec2.Client, region string) ([
nextToken = subnetsOutput.NextToken
}

telemetry.AddSpanAttribute(span, "aws.networks.subnets_found", len(subnets))
telemetry.SetSpanSuccess(span)
return subnets, nil
}

// processSubnets lists and processes all subnetworks
func processSubnets(_ context.Context, subnets []types.Subnet, region string) ([]api.CreateResource, error) {
func processSubnets(ctx context.Context, subnets []types.Subnet, region string) ([]api.CreateResource, error) {
ctx, span := telemetry.StartSpan(ctx, "aws.networks.process_subnets",
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(
attribute.String("aws.region", region),
attribute.Int("aws.networks.subnets_total", len(subnets)),
),
)
defer span.End()

resources := make([]api.CreateResource, 0)
subnetCount := 0

Expand All @@ -327,6 +374,8 @@ func processSubnets(_ context.Context, subnets []types.Subnet, region string) ([
subnetCount++
}

telemetry.AddSpanAttribute(span, "aws.networks.subnets_processed", subnetCount)
telemetry.SetSpanSuccess(span)
log.Info("Processed subnets", "count", subnetCount)
return resources, nil
}
Expand Down
Loading