Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
code with decorator
// logic with decorator
// logging functions are exported from github.com/opentracing/opentracing-go/log
func Objdecorator(f func(key string, obj interface{}) log.Field) func(string, interface{}) log.Field {
return func(key string, obj interface{}) log.Field {
return log.Object(key, obj)
}
}
func Strdecorator(f func(key, val string) log.Field) func(string, string) log.Field {
return func(key, value string) log.Field {
return log.String(key, value)
}
}
// NewOpenEBSProvisioner creates a new openebs provisioner
func NewOpenEBSProvisioner(client kubernetes.Interface) controller.Provisioner {
tracer := tracing.Init("provisioner")
opentracing.SetGlobalTracer(tracer)
span := tracer.StartSpan("Return OpenEBS Provisioner instance")
ctx := opentracing.ContextWithSpan(context.Background(), span)
defer span.Finish()
nodeName := os.Getenv("NODE_NAME")
if nodeName == "" {
span.LogFields(
Strdecorator(log.String)("event", "Getenv"),
log.Error(fmt.Errorf("ENV variable NODE_NAME not set NODE_NAME : %v", nodeName)),
)
glog.Errorf("ENV variable 'NODE_NAME' is not set")
}
var openebsObj mApiv1.OpenEBSVolume
span.LogFields(
Strdecorator(log.String)("event", "get node name"),
Strdecorator(log.String)("NODE_NAME", nodeName),
)
//Get maya-apiserver IP address from cluster
addr, err := openebsObj.GetMayaClusterIP(ctx, client)
if err != nil {
span.LogFields(
Strdecorator(log.String)("event", "get maya cluster IP"),
log.Error(err),
)
glog.Errorf("Error getting maya-apiserver IP Address: %v", err)
return nil
}
mayaServiceURI := "http://" + addr + ":5656"
span.LogFields(
Strdecorator(log.Object)("event", "get maya service URI", "mayaServiceURI", mayaServiceURI),
)
//Set maya-apiserver IP address along with default port
os.Setenv("MAPI_ADDR", mayaServiceURI)
return &openEBSProvisioner{
mapiURI: mayaServiceURI,
identity: nodeName,
tracer: tracer,
}
}
// logic without decorator
// NewOpenEBSProvisioner creates a new openebs provisioner
func NewOpenEBSProvisioner(client kubernetes.Interface) controller.Provisioner {
tracer := tracing.Init("provisioner")
opentracing.SetGlobalTracer(tracer)
span := tracer.StartSpan("Return OpenEBS Provisioner instance")
ctx := opentracing.ContextWithSpan(context.Background(), span)
defer span.Finish()
nodeName := os.Getenv("NODE_NAME")
if nodeName == "" {
span.LogFields(
log.String("event", "Getenv"),
log.Error(fmt.Errorf("ENV variable NODE_NAME not set NODE_NAME : %v", nodeName)),
)
glog.Errorf("ENV variable 'NODE_NAME' is not set")
}
var openebsObj mApiv1.OpenEBSVolume
span.LogFields(
log.String("event", "get node name"),
log.String("NODE_NAME", nodeName),
)
//Get maya-apiserver IP address from cluster
addr, err := openebsObj.GetMayaClusterIP(ctx, client)
if err != nil {
span.LogFields(
log.String("event", "get maya cluster IP"),
log.Error(err),
)
glog.Errorf("Error getting maya-apiserver IP Address: %v", err)
return nil
}
mayaServiceURI := "http://" + addr + ":5656"
span.LogFields(
log.Object("event", "get maya service URI", "mayaServiceURI", mayaServiceURI),
)
//Set maya-apiserver IP address along with default port
os.Setenv("MAPI_ADDR", mayaServiceURI)
return &openEBSProvisioner{
mapiURI: mayaServiceURI,
identity: nodeName,
tracer: tracer,
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.