Skip to content

Commit

Permalink
Add jupyterlab-pioneer (#2127)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Chuck McAndrew <[email protected]>
  • Loading branch information
3 people authored Jan 9, 2024
1 parent 507d1ae commit d4fcdf2
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 5 deletions.
23 changes: 23 additions & 0 deletions src/_nebari/stages/kubernetes_services/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,15 @@ class Monitoring(schema.Base):
enabled: bool = True


class JupyterLabPioneer(schema.Base):
enabled: bool = False
log_format: typing.Optional[str] = None


class Telemetry(schema.Base):
jupyterlab_pioneer: JupyterLabPioneer = JupyterLabPioneer()


class JupyterHub(schema.Base):
overrides: typing.Dict = {}

Expand Down Expand Up @@ -284,6 +293,7 @@ class InputSchema(schema.Base):
conda_store: CondaStore = CondaStore()
argo_workflows: ArgoWorkflows = ArgoWorkflows()
monitoring: Monitoring = Monitoring()
telemetry: Telemetry = Telemetry()
jupyterhub: JupyterHub = JupyterHub()
jupyterlab: JupyterLab = JupyterLab()
jhub_apps: JHubApps = JHubApps()
Expand Down Expand Up @@ -354,6 +364,13 @@ class MonitoringInputVars(schema.Base):
monitoring_enabled: bool = Field(alias="monitoring-enabled")


class TelemetryInputVars(schema.Base):
jupyterlab_pioneer_enabled: bool = Field(alias="jupyterlab-pioneer-enabled")
jupyterlab_pioneer_log_format: typing.Optional[str] = Field(
alias="jupyterlab-pioneer-log-format"
)


class ArgoWorkflowsInputVars(schema.Base):
argo_workflows_enabled: bool = Field(alias="argo-workflows-enabled")
argo_workflows_overrides: List[str] = Field(alias="argo-workflows-overrides")
Expand Down Expand Up @@ -484,6 +501,11 @@ def input_vars(self, stage_outputs: Dict[str, Dict[str, Any]]):
monitoring_enabled=self.config.monitoring.enabled,
)

telemetry_vars = TelemetryInputVars(
jupyterlab_pioneer_enabled=self.config.telemetry.jupyterlab_pioneer.enabled,
jupyterlab_pioneer_log_format=self.config.telemetry.jupyterlab_pioneer.log_format,
)

argo_workflows_vars = ArgoWorkflowsInputVars(
argo_workflows_enabled=self.config.argo_workflows.enabled,
argo_workflows_overrides=[json.dumps(self.config.argo_workflows.overrides)],
Expand All @@ -499,6 +521,7 @@ def input_vars(self, stage_outputs: Dict[str, Dict[str, Any]]):
**dask_gateway_vars.dict(by_alias=True),
**monitoring_vars.dict(by_alias=True),
**argo_workflows_vars.dict(by_alias=True),
**telemetry_vars.dict(by_alias=True),
}

def check(
Expand Down
2 changes: 2 additions & 0 deletions src/_nebari/stages/kubernetes_services/template/jupyterhub.tf
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,6 @@ module "jupyterhub" {

idle-culler-settings = var.idle-culler-settings

jupyterlab-pioneer-enabled = var.jupyterlab-pioneer-enabled
jupyterlab-pioneer-log-format = var.jupyterlab-pioneer-log-format
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,24 @@ locals {
)
}

locals {
jupyter-pioneer-config-py-template = templatefile("${path.module}/files/jupyter/jupyter_jupyterlab_pioneer_config.py.tpl", {
log_format = var.jupyterlab-pioneer-log-format != null ? var.jupyterlab-pioneer-log-format : ""
}
)
}


resource "local_file" "jupyter_server_config_py" {
content = local.jupyter-notebook-config-py-template
filename = "${path.module}/files/jupyter/jupyter_server_config.py"
}

resource "local_file" "jupyter_jupyterlab_pioneer_config_py" {
content = local.jupyter-pioneer-config-py-template
filename = "${path.module}/files/jupyter/jupyter_jupyterlab_pioneer_config.py"
}


resource "kubernetes_config_map" "etc-ipython" {
metadata {
Expand All @@ -31,19 +43,31 @@ resource "kubernetes_config_map" "etc-ipython" {
}


locals {
etc-jupyter-config-data = merge(
{
"jupyter_server_config.py" = local_file.jupyter_server_config_py.content,
},
var.jupyterlab-pioneer-enabled ? {
# quotes are must here, as terraform would otherwise think py is a property of
# a defined resource jupyter_jupyterlab_pioneer_config
"jupyter_jupyterlab_pioneer_config.py" = local_file.jupyter_jupyterlab_pioneer_config_py.content
} : {}
)
}

resource "kubernetes_config_map" "etc-jupyter" {
depends_on = [
local_file.jupyter_server_config_py
local_file.jupyter_server_config_py,
local_file.jupyter_jupyterlab_pioneer_config_py
]

metadata {
name = "etc-jupyter"
namespace = var.namespace
}

data = {
"jupyter_server_config.py" : local_file.jupyter_server_config_py.content
}
data = local.etc-jupyter-config-data
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import logging
import json


default_log_format = "%(asctime)s %(levelname)9s %(lineno)4s %(module)s: %(message)s"
log_format = ${log_format}

logging.basicConfig(
level=logging.INFO,
format=log_format if log_format else default_log_format
)

logger = logging.getLogger(__name__)

CUSTOM_EXPORTER_NAME = "MyCustomExporter"


def my_custom_exporter(args):
"""Custom exporter to log JupyterLab events to command line."""
logger.info(json.dumps(args.get("data")))
return {
"exporter": CUSTOM_EXPORTER_NAME,
"message": ""
}


c.JupyterLabPioneerApp.exporters = [
{
# sends telemetry data to the browser console
"type": "console_exporter",
},
{
# sends telemetry data (json) to the python console jupyter is running on
"type": "custom_exporter",
"args": {
"id": CUSTOM_EXPORTER_NAME
# add additional args for your exporter function here
},
}
]

c.JupyterLabPioneerApp.custom_exporter = {
CUSTOM_EXPORTER_NAME: my_custom_exporter,
}

c.JupyterLabPioneerApp.activeEvents = [
{"name": "ActiveCellChangeEvent", "logWholeNotebook": False},
{"name": "CellAddEvent", "logWholeNotebook": False},
{"name": "CellEditEvent", "logWholeNotebook": False},
{"name": "CellExecuteEvent", "logWholeNotebook": False},
{"name": "CellRemoveEvent", "logWholeNotebook": False},
{"name": "ClipboardCopyEvent", "logWholeNotebook": False},
{"name": "ClipboardCutEvent", "logWholeNotebook": False},
{"name": "ClipboardPasteEvent", "logWholeNotebook": False},
{"name": "NotebookHiddenEvent", "logWholeNotebook": False},
{"name": "NotebookOpenEvent", "logWholeNotebook": False},
{"name": "NotebookSaveEvent", "logWholeNotebook": False},
{"name": "NotebookScrollEvent", "logWholeNotebook": False},
{"name": "NotebookVisibleEvent", "logWholeNotebook": False},
]
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,13 @@ variable "argo-workflows-enabled" {
description = "Enable Argo Workflows"
type = bool
}

variable "jupyterlab-pioneer-enabled" {
description = "Enable JupyterLab Pioneer for telemetry"
type = bool
}

variable "jupyterlab-pioneer-log-format" {
description = "Logging format for JupyterLab Pioneer"
type = string
}
10 changes: 9 additions & 1 deletion src/_nebari/stages/kubernetes_services/template/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,20 @@ variable "conda-store-default-namespace" {
type = string
}


variable "argo-workflows-enabled" {
description = "Enable Argo Workflows"
type = bool
}

variable "jupyterlab-pioneer-enabled" {
description = "Enable JupyterLab Pioneer for telemetry"
type = bool
}

variable "jupyterlab-pioneer-log-format" {
description = "Logging format for JupyterLab Pioneer"
type = string
}
variable "jhub-apps-enabled" {
description = "Enable JupyterHub Apps"
type = bool
Expand Down

0 comments on commit d4fcdf2

Please sign in to comment.