Skip to content

Commit

Permalink
Merge pull request #627 from ericzbeard/analytics
Browse files Browse the repository at this point in the history
Add analytics to template Metadata
  • Loading branch information
ericzbeard authored Jan 10, 2025
2 parents 6c651fd + a785154 commit 86ef206
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cft/cft.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (t Template) GetSection(section Section) (*yaml.Node, error) {
m := t.Node.Content[0]
_, s, _ := s11n.GetMapValue(m, string(section))
if s == nil {
return nil, fmt.Errorf("unable to locate the %s node", section)
return nil, fmt.Errorf("unable to locate the %s section", section)
}
return s, nil
}
Expand Down
4 changes: 4 additions & 0 deletions cft/pkg/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"gopkg.in/yaml.v3"
)

var HasModules bool

const (
Rain = "Rain"
Metadata = "Metadata"
Expand Down Expand Up @@ -795,6 +797,8 @@ func module(ctx *directiveContext) (bool, error) {
return false, errors.New("expected !Rain::Module <URI>")
}

HasModules = true

uri := n.Content[1].Value
var content []byte
var err error
Expand Down
4 changes: 4 additions & 0 deletions cft/pkg/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,7 @@ func TestCsvToSequence(t *testing.T) {
t.Errorf("Unexpected sequence")
}
}

func init() {
pkg.NoAnalytics = true
}
41 changes: 40 additions & 1 deletion cft/pkg/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ package pkg

import (
"embed"
"encoding/json"
"errors"
"fmt"
"path/filepath"
Expand All @@ -44,6 +45,22 @@ import (

// Experimental must be set to true to enable !Rain::Module
var Experimental bool
var NoAnalytics bool
var HasRainSection bool

type analytics struct {
// Current Rain version
Version string

// Are we using experimental features?
Experimental bool

// Did we use any Rain modules?
HasModules bool

// Did the template have a Rain section?
HasRainSection bool
}

type transformContext struct {
nodeToTransform *yaml.Node
Expand Down Expand Up @@ -154,6 +171,8 @@ func processRainSection(t *cft.Template) bool {
// Now remove the Rain node from the template
t.RemoveSection(cft.Rain)

HasRainSection = true

return true
}

Expand Down Expand Up @@ -265,7 +284,27 @@ func Template(t cft.Template, rootDir string, fs *embed.FS) (cft.Template, error
retval.Node.Content = append(retval.Node.Content, templateNode)
}

return retval, err
// Add analytics to Metadata
if !NoAnalytics {
metadata, err := retval.GetSection(cft.Metadata)
if err != nil || metadata == nil {
metadata = node.AddMap(retval.Node.Content[0], string(cft.Metadata))
}
awsToolsMetrics := node.AddMap(metadata, "AWSToolsMetrics")
a := analytics{
Version: config.VERSION,
HasModules: HasModules,
Experimental: Experimental,
HasRainSection: HasRainSection,
}
s, _ := json.Marshal(&a)
awsToolsMetrics.Content = append(awsToolsMetrics.Content,
&yaml.Node{Kind: yaml.ScalarNode, Value: "Rain"})
awsToolsMetrics.Content = append(awsToolsMetrics.Content,
&yaml.Node{Kind: yaml.ScalarNode, Value: string(s)})
}

return retval, nil
}

// File opens path as a CloudFormation template and returns a cft.Template
Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ To list and delete changesets, use the ls and rm commands.
panic(err)
}

// Figure out how long we thing the stack will take to execute
// Figure out how long we think the stack will take to execute
//totalSeconds := forecast.PredictTotalEstimate(template, stackExists)
// TODO - Wait until the forecast command is GA and add this to output

Expand Down Expand Up @@ -339,4 +339,5 @@ func init() {
Cmd.Flags().StringVar(&format.NodeStyle, "node-style", "original", format.NodeStyleDocs)
Cmd.Flags().BoolVar(&experimental, "experimental", false, "Acknowledge that you want to deploy with an experimental feature")
Cmd.Flags().BoolVar(&includeNested, "nested-change-set", true, "Whether or not to include nested stacks in the change set")
Cmd.Flags().BoolVar(&cftpkg.NoAnalytics, "no-analytics", false, "Do not write analytics to Metadata")
}
1 change: 1 addition & 0 deletions internal/cmd/pkg/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,5 @@ func init() {
Cmd.Flags().BoolVar(&config.Debug, "debug", false, "Output debugging information")
Cmd.Flags().BoolVar(&dataModel, "datamodel", false, "Output the go yaml data model")
Cmd.Flags().StringVar(&format.NodeStyle, "node-style", "", format.NodeStyleDocs)
Cmd.Flags().BoolVar(&cftpkg.NoAnalytics, "no-analytics", false, "Do not include analytics in Metadata")
}
5 changes: 5 additions & 0 deletions test/templates/analytics.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Metadata:
Comment: abc
Resources:
Bucket:
Type: AWS::S3::Bucket

0 comments on commit 86ef206

Please sign in to comment.