Skip to content
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

Stimlog #9

Merged
merged 7 commits into from
Apr 19, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func main() {
stim := stim.New()
// stim.AddStimpack(discover.New())
// stim.AddStimpack(discover.New())
stim.AddStimpack(aws.New())
stim.AddStimpack(kubernetes.New())
stim.AddStimpack(pagerduty.New())
Expand Down
12 changes: 8 additions & 4 deletions pkg/aws/aws.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package aws

import (
"github.com/readytalk/stim/pkg/log"
"github.com/readytalk/stim/pkg/stimlog"
// "github.com/aws/aws-sdk-go/aws"
// "github.com/aws/aws-sdk-go/aws/awserr"
// "github.com/aws/aws-sdk-go/aws/session"
Expand All @@ -13,20 +13,24 @@ import (
type Aws struct {
// client *slack.Client
config *Config
log log.Logger
log *stimlog.StimLogger
}

type Config struct {
Token string
}

// New builds a client from the provided config
func New(config *Config) (*Aws, error) {
func New(config *Config, sl *stimlog.StimLogger) (*Aws, error) {

// client := slack.New(config.Token)

s := &Aws{config: config}

if sl != nil {
s.log = sl
} else {
s.log = stimlog.GetLogger()
}
return s, nil
}

Expand Down
16 changes: 8 additions & 8 deletions pkg/kubernetes/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (k *Kubernetes) modifyKubeconfig(o *KubeConfigOptions) error {
return err
}

k.Debug("Kubernetes config modified...")
k.log.Debug("Kubernetes config modified...")

return nil

Expand All @@ -53,10 +53,10 @@ func (k *Kubernetes) modifyKubeconfigCluster(kubeConfig *clientcmdapi.Config, o

stanza, exists := kubeConfig.Clusters[o.ClusterName]
if !exists {
k.Debug("Kubernetes cluster `" + o.ClusterName + "` not found in config, creating...")
k.log.Debug("Kubernetes cluster `" + o.ClusterName + "` not found in config, creating...")
stanza = clientcmdapi.NewCluster()
} else {
k.Debug("Kubernetes cluster `" + o.ClusterName + "` found, modifying...")
k.log.Debug("Kubernetes cluster `" + o.ClusterName + "` found, modifying...")
}

stanza.Server = o.ClusterServer
Expand All @@ -71,10 +71,10 @@ func (k *Kubernetes) modifyKubeconfigAuth(kubeConfig *clientcmdapi.Config, o *Ku

stanza, exists := kubeConfig.AuthInfos[o.AuthName]
if !exists {
k.Debug("Kubernetes auth `" + o.AuthName + "` not found in config, creating...")
k.log.Debug("Kubernetes auth `" + o.AuthName + "` not found in config, creating...")
stanza = clientcmdapi.NewAuthInfo()
} else {
k.Debug("Kubernetes auth `" + o.AuthName + "` found, modifying...")
k.log.Debug("Kubernetes auth `" + o.AuthName + "` found, modifying...")
}

stanza.Token = o.AuthToken
Expand All @@ -86,10 +86,10 @@ func (k *Kubernetes) modifyKubeconfigContext(kubeConfig *clientcmdapi.Config, o

stanza, exists := kubeConfig.Contexts[o.ContextName]
if !exists {
k.Debug("Kubernetes context `" + o.ContextName + "` not found in config, creating...")
k.log.Debug("Kubernetes context `" + o.ContextName + "` not found in config, creating...")
stanza = clientcmdapi.NewContext()
} else {
k.Debug("Kubernetes context `" + o.ContextName + "` found, modifying...")
k.log.Debug("Kubernetes context `" + o.ContextName + "` found, modifying...")
}

stanza.Cluster = o.ClusterName
Expand All @@ -99,7 +99,7 @@ func (k *Kubernetes) modifyKubeconfigContext(kubeConfig *clientcmdapi.Config, o

if o.ContextSetCurrent {
kubeConfig.CurrentContext = o.ContextName
k.Debug("Kubernetes current-context set to `" + o.ContextName + "`")
k.log.Debug("Kubernetes current-context set to `" + o.ContextName + "`")
}

}
31 changes: 9 additions & 22 deletions pkg/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
@@ -1,46 +1,33 @@
package kubernetes

import (
"fmt"
"github.com/readytalk/stim/pkg/stimlog"
"k8s.io/client-go/tools/clientcmd"
)

type Kubernetes struct {
// client *api.Client
config *Config

log *stimlog.StimLogger
// This allows us to read/write the kube config
// It takes into account KUBECONFIG env var for setting the location
configAccess clientcmd.ConfigAccess
}

type Config struct {
// Address string
Logger
}

type Logger interface {
Debug(args ...interface{})
Info(args ...interface{})
}

func (k *Kubernetes) Debug(message string) {
if k.config.Logger != nil {
k.config.Debug(message)
}
}
func New(kconf *Config, sl *stimlog.StimLogger) (*Kubernetes, error) {

func (k *Kubernetes) Info(message string) {
if k.config.Logger != nil {
k.config.Info(message)
k := &Kubernetes{config: kconf}

if sl != nil {
k.log = sl
} else {
fmt.Println(message)
k.log = stimlog.GetLogger()
}
}

func New(config *Config) (*Kubernetes, error) {

k := &Kubernetes{config: config}

return k, nil
}
Expand All @@ -49,7 +36,7 @@ func (k *Kubernetes) SetKubeconfig(kubeConfigOptions *KubeConfigOptions) error {

// configAccess is used by subcommands and methods in this package to load and modify the appropriate config files
k.configAccess = clientcmd.NewDefaultPathOptions()
k.Debug("Using kubeconfig file: " + k.configAccess.GetDefaultFilename())
k.log.Debug("Using kubeconfig file: " + k.configAccess.GetDefaultFilename())

err := k.modifyKubeconfig(kubeConfigOptions)
if err != nil {
Expand Down
22 changes: 7 additions & 15 deletions pkg/log/logger.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package log

import (
"fmt"
)

// Logger is a interface for creating standard logging calls.
// This will enable depended code log.Debug("Woot")
// Avoid all other packages from declaring their own loggers
Expand All @@ -10,28 +14,16 @@ type Logger interface {
Fatal(...interface{})
}

var logger Logger

// SetLogger takes a structured logger to interface with.
// After the logger is setup it will be available across your packages
// If SetLogger is not used Debug will not create output
func SetLogger(givenLogger Logger) {
logger = givenLogger
}

// Debug logs a message at level Debug on the standard logger.
func Debug(message ...interface{}) {
if logger != nil {
logger.Debug(message...)
}
fmt.Println(message)
}

// Warn logs a message at level Warn on the standard logger.
func Warn(message ...interface{}) {
logger.Warn(message...)
fmt.Println(message)
}

// Fatal logs a message at level Fatal on the standard logger then the process will exit with status set to 1.
func Fatal(message ...interface{}) {
logger.Fatal(message...)
fmt.Println(message)
}
14 changes: 7 additions & 7 deletions pkg/pagerduty/pagerduty.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ package pagerduty

import (
"errors"
pdApi "github.com/PagerDuty/go-pagerduty"
"github.com/readytalk/stim/pkg/log"
"github.com/readytalk/stim/pkg/utils"
"os"
"strings"

pdApi "github.com/PagerDuty/go-pagerduty"
"github.com/readytalk/stim/pkg/stimlog"
"github.com/readytalk/stim/pkg/utils"
)

// Pagerduty is the main object
type Pagerduty struct {
client *pdApi.Client
log log.Logger
log *stimlog.StimLogger
}

// Event contains the required and optional fields to sent an event
Expand All @@ -30,12 +31,11 @@ type Event struct {
}

// New returns a new Pagerduty "instance"
func New(apiKey string, givenLog log.Logger) *Pagerduty {
func New(apiKey string, givenLog *stimlog.StimLogger) *Pagerduty {

// Initialize client
client := pdApi.NewClient(apiKey)
p := &Pagerduty{client: client}
log.SetLogger(givenLog)
p := &Pagerduty{client: client, log: givenLog}

return p
}
Expand Down
34 changes: 10 additions & 24 deletions pkg/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,26 @@ package prometheus

import (
"context"
"fmt"
"time"

"github.com/prometheus/client_golang/api"
"github.com/prometheus/client_golang/api/prometheus/v1"
"time"
"github.com/readytalk/stim/pkg/stimlog"
)

type Prometheus struct {
client *api.Client
config *Config
context context.Context
API v1.API
log *stimlog.StimLogger
}

type Config struct {
Address string
Logger
}

type Logger interface {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure I agree with removing the interface and forcing people using this package to use "github.com/readytalk/stim/pkg/stimlog". The whole idea was to allow a consumer of this package to pass in any logger they wanted as long as it has the Debug and Info methods.

Copy link
Member Author

Choose a reason for hiding this comment

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

I dont completely disagree, but its a little silly to require Stim, which has its logger, then not use its logger. Right now because of that there is no way these packages can be independent from stim.

Actually, let me put out the changes I have around redoing stimpacks to make them independent from stim, just didnt want to do it all in one shot.

Debug(args ...interface{})
Info(args ...interface{})
}

func (p *Prometheus) Debug(message string) {
if p.config.Logger != nil {
p.config.Debug(message)
}
}

func (p *Prometheus) Info(message string) {
if p.config.Logger != nil {
p.config.Info(message)
} else {
fmt.Println(message)
}
}

func New(config *Config) (*Prometheus, error) {
func New(config *Config, sl *stimlog.StimLogger) (*Prometheus, error) {

apiConfig := api.Config{Address: config.Address}
client, err := api.NewClient(apiConfig)
Expand All @@ -50,7 +32,11 @@ func New(config *Config) (*Prometheus, error) {
api := v1.NewAPI(client)

p := &Prometheus{client: &client, API: api, context: context.Background()}

if sl != nil {
p.log = sl
} else {
p.log = stimlog.GetLogger()
}
return p, nil
}

Expand Down
34 changes: 10 additions & 24 deletions pkg/slack/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@ package slack

import (
"errors"
"fmt"

"github.com/nlopes/slack"
"github.com/readytalk/stim/pkg/stimlog"
)

// Slack is the main object
type Slack struct {
client *slack.Client
log *stimlog.StimLogger
config *Config
}

// Config contains information about setting up a new slack client
type Config struct {
Token string
Logger
}

// Message contains information about a slack message
Expand All @@ -26,32 +27,17 @@ type Message struct {
IconUrl string
}

// Logger is an interface for passing a custom logger to be used by this package
type Logger interface {
Debug(args ...interface{})
Info(args ...interface{})
}

func (s *Slack) debug(message string) {
if s.config.Logger != nil {
s.config.Debug(message)
}
}

func (s *Slack) info(message string) {
if s.config.Logger != nil {
s.config.Info(message)
} else {
fmt.Println(message)
}
}

// New builds a slack client from the provided config
func New(config *Config) (*Slack, error) {
func New(config *Config, sl *stimlog.StimLogger) (*Slack, error) {

client := slack.New(config.Token)

s := &Slack{config: config, client: client}
if sl != nil {
s.log = sl
} else {
s.log = stimlog.GetLogger()
}

return s, nil
}
Expand Down Expand Up @@ -112,7 +98,7 @@ func (s *Slack) PostMessage(msg *Message) error {
return err
}

s.debug("Slack message successfully sent at " + timestamp + " to channel " + msg.Channel + " (" + channelId + ")")
s.log.Debug("Slack message successfully sent at " + timestamp + " to channel " + msg.Channel + " (" + channelId + ")")

return nil
}
Loading