Skip to content

Commit

Permalink
add register-metrics-endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: Jesse Weaver <[email protected]>
  • Loading branch information
Ben Fuller authored and pianohacker committed Aug 14, 2018
1 parent ee651df commit a969ed2
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import (
"fmt"
"os"
"errors"
"strings"
)

const pluginName = "pm-please-add-details" // also in scripts/reinstall.sh
const registerLogFormatCommand = "register-log-format"
const registerMetricsEndpointCommand = "register-metrics-endpoint"
const registerLogFormatUsage = "cf register-log-format APPNAME FORMAT"
const registerMetricsEndpointUsage = "cf register-metrics-endpoint APPNAME PATH"

type PrismCli struct {}
type PrismCli struct{}

type cliCommandRunner interface {
CliCommandWithoutTerminalOutput(args ...string) ([]string, error)
Expand All @@ -24,6 +27,9 @@ func (c PrismCli) Run(cliConnection plugin.CliConnection, args []string) {
case registerLogFormatCommand:
err := RegisterLogFormat(cliConnection, args[1:])
exitIfErr(err)
case registerMetricsEndpointCommand:
err := RegisterMetricsEndpoint(cliConnection, args[1:])
exitIfErr(err)
case "CLI-MESSAGE-UNINSTALL":
// do nothing
}
Expand All @@ -45,6 +51,13 @@ func (c PrismCli) GetMetadata() plugin.PluginMetadata {
Usage: registerLogFormatUsage,
},
},
{
Name: registerMetricsEndpointCommand,
HelpText: "This will register your metrics endpoint which will then be scraped at the interval defined at deploy",
UsageDetails: plugin.Usage{
Usage: registerMetricsEndpointUsage,
},
},
},
}
}
Expand All @@ -55,14 +68,30 @@ func RegisterLogFormat(cliConn cliCommandRunner, args []string) error {
}
appName := args[0]
logFormat := args[1]
serviceName := "structured-format-" + logFormat

return EnsureServiceAndBind(cliConn, appName, "structured-format", logFormat)
}

func RegisterMetricsEndpoint(cliConn cliCommandRunner, args []string) error {
if len(args) != 2 {
return errors.New("Usage: " + registerMetricsEndpointUsage)
}
appName := args[0]
path := args[1]

return EnsureServiceAndBind(cliConn, appName, "metrics-endpoint", path)
}

func EnsureServiceAndBind(cliConn cliCommandRunner, appName, serviceProtocol, config string) error {
cleanedConfig := strings.Trim(strings.Replace(config, "/", "-", -1), "-")
serviceName := serviceProtocol + "-" + cleanedConfig
exists, err := findExistingService(cliConn, serviceName)
if err != nil {
return err
}

if !exists {
binding := "structured-format://" + logFormat
binding := serviceProtocol + "://" + config
_, err := cliConn.CliCommandWithoutTerminalOutput("create-user-provided-service", serviceName, "-l", binding)
if err != nil {
return err
Expand Down
155 changes: 155 additions & 0 deletions src/cliplugin/command/prism_cli_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package command_test

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"cliplugin/command"
"errors"
"code.cloudfoundry.org/cli/plugin/models"
)

var _ = Describe("PrismCli", func() {
Context("RegisterLogFormat", func() {
It("creates a service", func() {
cliConnection := newMockCliConnection()

err := command.RegisterLogFormat(cliConnection, []string{"app-name", "format-name"})
Expect(err).ToNot(HaveOccurred())
Expect(cliConnection.cliCommandsCalled).To(Receive(ConsistOf(
"create-user-provided-service",
"structured-format-format-name",
"-l",
"structured-format://format-name",
)))
})

It("returns error if number of arguments is wrong", func() {
cliConnection := newMockCliConnection()

Expect(command.RegisterLogFormat(cliConnection, []string{"app-name", "format-name", "some-garbage"})).To(HaveOccurred())
})
})

Context("RegisterMetricsEndpoint", func() {
It("creates a service", func() {
cliConnection := newMockCliConnection()

err := command.RegisterMetricsEndpoint(cliConnection, []string{"app-name", "endpoint"})
Expect(err).ToNot(HaveOccurred())
Expect(cliConnection.cliCommandsCalled).To(Receive(ConsistOf(
"create-user-provided-service",
"metrics-endpoint-endpoint",
"-l",
"metrics-endpoint://endpoint",
)))
})

It("returns error if number of arguments is wrong", func() {
cliConnection := newMockCliConnection()

Expect(command.RegisterMetricsEndpoint(cliConnection, []string{"app-name", "endpoint", "some-garbage"})).To(HaveOccurred())
})
})

Context("EnsureServiceAndBind", func() {
It("creates a service and binds it to the application", func() {
cliConnection := newMockCliConnection()

err := command.EnsureServiceAndBind(cliConnection, "app-name", "protocol", "config")
Expect(err).ToNot(HaveOccurred())
Expect(cliConnection.cliCommandsCalled).To(Receive(ConsistOf(
"create-user-provided-service",
"protocol-config",
"-l",
"protocol://config",
)))

Expect(cliConnection.cliCommandsCalled).To(Receive(ConsistOf(
"bind-service",
"app-name",
"protocol-config",
)))
})

It("doesn't create a service if service already present", func() {
cliConnection := newMockCliConnection()
cliConnection.serviceName = "protocol-config"

err := command.EnsureServiceAndBind(cliConnection, "app-name", "protocol", "config")
Expect(err).ToNot(HaveOccurred())
Expect(cliConnection.cliCommandsCalled).To(Receive(ContainElement("bind-service")))
Expect(cliConnection.cliCommandsCalled).ToNot(Receive())
})

It("replaces slashes in the service name", func() {
cliConnection := newMockCliConnection()

err := command.EnsureServiceAndBind(cliConnection, "app-name", "protocol", "/v2/path/")
Expect(err).ToNot(HaveOccurred())
Expect(cliConnection.cliCommandsCalled).To(Receive(ConsistOf(
"create-user-provided-service",
"protocol-v2-path",
"-l",
"protocol:///v2/path/",
)))
})

It("returns error if creating the service fails", func() {
cliConnection := newMockCliConnection()
cliConnection.getServicesError = errors.New("error")

Expect(command.EnsureServiceAndBind(cliConnection, "app-name", "protocol", "config")).Should(HaveOccurred())
Expect(cliConnection.cliCommandsCalled).ToNot(Receive())
})

It("returns error if creating the service fails", func() {
cliConnection := newMockCliConnection()
cliConnection.cliCommandErrorCommand = "create-user-provided-service"

Expect(command.EnsureServiceAndBind(cliConnection, "app-name", "protocol", "config")).Should(HaveOccurred())

Expect(cliConnection.cliCommandsCalled).To(Receive(ContainElement("create-user-provided-service")))
Expect(cliConnection.cliCommandsCalled).ToNot(Receive())
})

It("returns error if binding fails", func() {
cliConnection := newMockCliConnection()
cliConnection.cliCommandErrorCommand = "bind-service"

Expect(command.EnsureServiceAndBind(cliConnection, "app-name", "protocol", "config")).Should(HaveOccurred())

Expect(cliConnection.cliCommandsCalled).To(Receive(ContainElement("create-user-provided-service")))
Expect(cliConnection.cliCommandsCalled).To(Receive(ContainElement("bind-service")))
})
})

})

type mockCliConnection struct {
cliCommandsCalled chan []string
serviceName string
cliCommandErrorCommand string
getServicesError error
}

func (c *mockCliConnection) GetServices() ([]plugin_models.GetServices_Model, error) {
if c.serviceName != "" {
return []plugin_models.GetServices_Model{{Name: c.serviceName}}, nil
}
return nil, c.getServicesError
}

func newMockCliConnection() *mockCliConnection {
return &mockCliConnection{
cliCommandsCalled: make(chan []string, 10),
}
}

func (c *mockCliConnection) CliCommandWithoutTerminalOutput(args ...string) ([]string, error) {
c.cliCommandsCalled <- args
if args[0] == c.cliCommandErrorCommand {
return nil, errors.New("error")
}
return nil, nil
}
103 changes: 0 additions & 103 deletions src/cliplugin/command/register-log-format_test.go

This file was deleted.

0 comments on commit a969ed2

Please sign in to comment.