Skip to content

Commit

Permalink
fix(OTEL): test tracer for span
Browse files Browse the repository at this point in the history
Signed-off-by: devopstales <42894256+devopstales@users.noreply.github.com>
  • Loading branch information
devopstales committed Jun 5, 2023
1 parent 7b9c824 commit a24c31e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
30 changes: 15 additions & 15 deletions src/kubedash/functions/k8s.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,28 +172,28 @@ def k8sServerContextsList():

def k8sListNamespaces(username_role, user_token):
with tracer.start_as_current_span("list-namespaces") if tracer else nullcontext() as span:
if span.is_recording():
if tracer and span.is_recording():
span.set_attribute("user.role", username_role)
k8sClientConfigGet(username_role, user_token)
try:
namespace_list = k8s_client.CoreV1Api().list_namespace(_request_timeout=1)
return namespace_list, None
except ApiException as error:
ErrorHandler(error, "list namespaces")
if span.is_recording():
if tracer and span.is_recording():
span.set_status(Status(StatusCode.ERROR, "%s list namespaces" % error))
namespace_list = ""
return namespace_list, error
except Exception as error:
ErrorHandler("CannotConnect", "k8sListNamespaces: %s" % error)
if span.is_recording():
if tracer and span.is_recording():
span.set_status(Status(StatusCode.ERROR, "k8sListNamespaces: %s" % error))
namespace_list = ""
return namespace_list, "CannotConnect"

def k8sNamespaceListGet(username_role, user_token):
with tracer.start_as_current_span("get-namespace-list") if tracer else nullcontext() as span:
if span.is_recording():
if tracer and span.is_recording():
span.set_attribute("user.role", username_role)
k8sClientConfigGet(username_role, user_token)
namespace_list = []
Expand All @@ -207,13 +207,13 @@ def k8sNamespaceListGet(username_role, user_token):
return namespace_list, error
except Exception as error:
ErrorHandler("CannotConnect", "k8sNamespaceListGet: %s" % error)
if span.is_recording():
if tracer and span.is_recording():
span.set_status(Status(StatusCode.ERROR, "k8sNamespaceListGet: %s" % error))
return namespace_list, "CannotConnect"

def k8sNamespacesGet(username_role, user_token):
with tracer.start_as_current_span("get-namespace") if tracer else nullcontext() as span:
if span.is_recording():
if tracer and span.is_recording():
span.set_attribute("user.role", username_role)
k8sClientConfigGet(username_role, user_token)
NAMESPACE_LIST = []
Expand All @@ -232,15 +232,15 @@ def k8sNamespacesGet(username_role, user_token):
for key, value in ns.metadata.labels.items():
NAMESPACE_DADTA['labels'].append(key + "=" + value)
NAMESPACE_LIST.append(NAMESPACE_DADTA)
if span.is_recording():
if tracer and span.is_recording():
span.set_attribute("namespace.name", ns.metadata.name)
span.set_attribute("namespace.role", ns.status.__dict__['_phase'])
return NAMESPACE_LIST
else:
return NAMESPACE_LIST
except Exception as error:
ErrorHandler("CannotConnect", "k8sNamespacesGet: %s" % error)
if span.is_recording():
if tracer and span.is_recording():
span.set_status(Status(StatusCode.ERROR, "k8sNamespacesGet: %s" % error))
return NAMESPACE_LIST

Expand Down Expand Up @@ -287,21 +287,21 @@ def k8sNamespaceDelete(username_role, user_token, ns_name):

def k8sClientConfigGet(username_role, user_token):
with tracer.start_as_current_span("load-client-configs") if tracer else nullcontext() as span:
if span.is_recording():
if tracer and span.is_recording():
span.set_attribute("user.role", username_role)
if username_role == "Admin":
try:
k8s_config.load_kube_config()
if span.is_recording():
if tracer and span.is_recording():
span.set_attribute("client.config", "local")
except Exception as error:
try:
k8s_config.load_incluster_config()
if span.is_recording():
if tracer and span.is_recording():
span.set_attribute("client.config", "incluster")
except k8s_config.ConfigException as error:
ErrorHandler(error, "Could not configure kubernetes python client")
if span.is_recording():
if tracer and span.is_recording():
span.set_status(Status(StatusCode.ERROR, "Could not configure kubernetes python client: %s" % error))
elif username_role == "User":
k8sConfig = k8sServerConfigGet()
Expand All @@ -319,7 +319,7 @@ def k8sClientConfigGet(username_role, user_token):
configuration.debug = False
configuration.api_key_prefix['authorization'] = 'Bearer'
configuration.api_key["authorization"] = str(user_token["id_token"])
if span.is_recording():
if tracer and span.is_recording():
span.set_attribute("client.config", "oidc")
k8s_client.Configuration.set_default(configuration)

Expand Down Expand Up @@ -486,12 +486,12 @@ def k8sGetClusterMetric():
return clusterMetric
except ApiException as error:
ErrorHandler(error, "Cannot Connect to Kubernetes")
if span.is_recording():
if tracer and span.is_recording():
span.set_status(Status(StatusCode.ERROR, "Cannot Connect to Kubernetes: %s" % error))
return bad_clusterMetric
except Exception as error:
ErrorHandler("CannotConnect", "Cannot Connect to Kubernetes")
if span.is_recording():
if tracer and span.is_recording():
span.set_status(Status(StatusCode.ERROR, "Cannot Connect to Kubernetes: %s" % error))
return bad_clusterMetric

Expand Down
6 changes: 3 additions & 3 deletions src/kubedash/functions/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def cluster_metrics():
user = User.query.filter_by(username="admin", user_type = "Local").first()
if username == "admin" and check_password_hash(user.password_hash, "admin"):
flash('<a href="/profile">You should change the default password!</a>', "warning")
if span.is_recording():
if tracer and span.is_recording():
span.add_event("log", {
"log.severity": "warning",
"log.message": "You should change the default password!",
Expand All @@ -242,7 +242,7 @@ def workloads():
) if tracer else nullcontext() as span:
if request.method == 'POST':
session['ns_select'] = request.form.get('ns_select')
if span.is_recording():
if tracer and span.is_recording():
span.set_attribute("namespace.selected", request.form.get('ns_select'))


Expand All @@ -258,7 +258,7 @@ def workloads():
nodes = []
edges = []

if span.is_recording():
if tracer and span.is_recording():
span.set_attribute("workloads.nodes", nodes)
span.set_attribute("workloads.edges", edges)

Expand Down

0 comments on commit a24c31e

Please sign in to comment.