metaflow 2.15.3__py2.py3-none-any.whl → 2.15.5__py2.py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -256,12 +256,19 @@ class ArgoClient(object):
256
256
  json.loads(e.body)["message"] if e.body is not None else e.reason
257
257
  )
258
258
 
259
- def trigger_workflow_template(self, name, parameters={}):
259
+ def trigger_workflow_template(self, name, usertype, username, parameters={}):
260
260
  client = self._client.get()
261
261
  body = {
262
262
  "apiVersion": "argoproj.io/v1alpha1",
263
263
  "kind": "Workflow",
264
- "metadata": {"generateName": name + "-"},
264
+ "metadata": {
265
+ "generateName": name + "-",
266
+ "annotations": {
267
+ "metaflow/triggered_by_user": json.dumps(
268
+ {"type": usertype, "name": username}
269
+ )
270
+ },
271
+ },
265
272
  "spec": {
266
273
  "workflowTemplateRef": {"name": name},
267
274
  "arguments": {
@@ -64,6 +64,7 @@ from metaflow.util import (
64
64
  )
65
65
 
66
66
  from .argo_client import ArgoClient
67
+ from metaflow.util import resolve_identity
67
68
 
68
69
 
69
70
  class ArgoWorkflowsException(MetaflowException):
@@ -108,8 +109,7 @@ class ArgoWorkflows(object):
108
109
  notify_slack_webhook_url=None,
109
110
  notify_pager_duty_integration_key=None,
110
111
  notify_incident_io_api_key=None,
111
- incident_io_success_severity_id=None,
112
- incident_io_error_severity_id=None,
112
+ incident_io_alert_source_config_id=None,
113
113
  enable_heartbeat_daemon=True,
114
114
  enable_error_msg_capture=False,
115
115
  ):
@@ -160,8 +160,7 @@ class ArgoWorkflows(object):
160
160
  self.notify_slack_webhook_url = notify_slack_webhook_url
161
161
  self.notify_pager_duty_integration_key = notify_pager_duty_integration_key
162
162
  self.notify_incident_io_api_key = notify_incident_io_api_key
163
- self.incident_io_success_severity_id = incident_io_success_severity_id
164
- self.incident_io_error_severity_id = incident_io_error_severity_id
163
+ self.incident_io_alert_source_config_id = incident_io_alert_source_config_id
165
164
  self.enable_heartbeat_daemon = enable_heartbeat_daemon
166
165
  self.enable_error_msg_capture = enable_error_msg_capture
167
166
  self.parameters = self._process_parameters()
@@ -315,8 +314,16 @@ class ArgoWorkflows(object):
315
314
  "Workflows before proceeding." % name
316
315
  )
317
316
  try:
317
+ id_parts = resolve_identity().split(":")
318
+ parts_size = len(id_parts)
319
+ usertype = id_parts[0] if parts_size > 0 else "unknown"
320
+ username = id_parts[1] if parts_size > 1 else "unknown"
321
+
318
322
  return ArgoClient(namespace=KUBERNETES_NAMESPACE).trigger_workflow_template(
319
- name, parameters
323
+ name,
324
+ usertype,
325
+ username,
326
+ parameters,
320
327
  )
321
328
  except Exception as e:
322
329
  raise ArgoWorkflowsException(str(e))
@@ -2505,25 +2512,49 @@ class ArgoWorkflows(object):
2505
2512
  def _incident_io_alert_template(self):
2506
2513
  if self.notify_incident_io_api_key is None:
2507
2514
  return None
2508
- if self.incident_io_error_severity_id is None:
2515
+ if self.incident_io_alert_source_config_id is None:
2509
2516
  raise MetaflowException(
2510
- "Creating incidents for errors requires a severity id."
2517
+ "Creating alerts for errors requires a alert source config ID."
2511
2518
  )
2519
+ ui_links = self._incident_io_ui_urls_for_run()
2512
2520
  return Template("notify-incident-io-on-error").http(
2513
2521
  Http("POST")
2514
- .url("https://api.incident.io/v2/incidents")
2522
+ .url(
2523
+ "https://api.incident.io/v2/alert_events/http/%s"
2524
+ % self.incident_io_alert_source_config_id
2525
+ )
2515
2526
  .header("Content-Type", "application/json")
2516
2527
  .header("Authorization", "Bearer %s" % self.notify_incident_io_api_key)
2517
2528
  .body(
2518
2529
  json.dumps(
2519
2530
  {
2520
2531
  "idempotency_key": "argo-{{workflow.name}}", # use run id to deduplicate alerts.
2521
- "visibility": "public",
2522
- "severity_id": self.incident_io_error_severity_id,
2523
- "name": "Flow %s has failed." % self.flow.name,
2524
- "summary": "Metaflow run %s/argo-{{workflow.name}} failed! %s"
2525
- % (self.flow.name, self._incident_io_ui_urls_for_run()),
2526
- # TODO: Add support for custom field entries.
2532
+ "status": "firing",
2533
+ "title": "Flow %s has failed." % self.flow.name,
2534
+ "description": "Metaflow run {run_pathspec} failed!{urls}".format(
2535
+ run_pathspec="%s/argo-{{workflow.name}}" % self.flow.name,
2536
+ urls=(
2537
+ "\n\nSee details for the run at:\n\n"
2538
+ + "\n\n".join(ui_links)
2539
+ if ui_links
2540
+ else ""
2541
+ ),
2542
+ ),
2543
+ "source_url": (
2544
+ "%s/%s/%s"
2545
+ % (
2546
+ UI_URL.rstrip("/"),
2547
+ self.flow.name,
2548
+ "argo-{{workflow.name}}",
2549
+ )
2550
+ if UI_URL
2551
+ else None
2552
+ ),
2553
+ "metadata": {
2554
+ "run_status": "failed",
2555
+ "flow_name": self.flow.name,
2556
+ "run_id": "argo-{{workflow.name}}",
2557
+ },
2527
2558
  }
2528
2559
  )
2529
2560
  )
@@ -2532,27 +2563,49 @@ class ArgoWorkflows(object):
2532
2563
  def _incident_io_change_template(self):
2533
2564
  if self.notify_incident_io_api_key is None:
2534
2565
  return None
2535
- if self.incident_io_success_severity_id is None:
2566
+ if self.incident_io_alert_source_config_id is None:
2536
2567
  raise MetaflowException(
2537
- "Creating incidents for successes requires a severity id."
2568
+ "Creating alerts for successes requires an alert source config ID."
2538
2569
  )
2570
+ ui_links = self._incident_io_ui_urls_for_run()
2539
2571
  return Template("notify-incident-io-on-success").http(
2540
2572
  Http("POST")
2541
- .url("https://api.incident.io/v2/incidents")
2573
+ .url(
2574
+ "https://api.incident.io/v2/alert_events/http/%s"
2575
+ % self.incident_io_alert_source_config_id
2576
+ )
2542
2577
  .header("Content-Type", "application/json")
2543
2578
  .header("Authorization", "Bearer %s" % self.notify_incident_io_api_key)
2544
2579
  .body(
2545
2580
  json.dumps(
2546
2581
  {
2547
2582
  "idempotency_key": "argo-{{workflow.name}}", # use run id to deduplicate alerts.
2548
- "visibility": "public",
2549
- "severity_id": self.incident_io_success_severity_id,
2550
- # TODO: Do we need to make incident type configurable for successes? otherwise they are created as 'investigating'
2551
- # "incident_type_id": ""
2552
- "name": "Flow %s has succeeded." % self.flow.name,
2553
- "summary": "Metaflow run %s/argo-{{workflow.name}} succeeded!%s"
2554
- % (self.flow.name, self._incident_io_ui_urls_for_run()),
2555
- # TODO: Add support for custom field entries.
2583
+ "status": "firing",
2584
+ "title": "Flow %s has succeeded." % self.flow.name,
2585
+ "description": "Metaflow run {run_pathspec} succeeded!{urls}".format(
2586
+ run_pathspec="%s/argo-{{workflow.name}}" % self.flow.name,
2587
+ urls=(
2588
+ "\n\nSee details for the run at:\n\n"
2589
+ + "\n\n".join(ui_links)
2590
+ if ui_links
2591
+ else ""
2592
+ ),
2593
+ ),
2594
+ "source_url": (
2595
+ "%s/%s/%s"
2596
+ % (
2597
+ UI_URL.rstrip("/"),
2598
+ self.flow.name,
2599
+ "argo-{{workflow.name}}",
2600
+ )
2601
+ if UI_URL
2602
+ else None
2603
+ ),
2604
+ "metadata": {
2605
+ "run_status": "succeeded",
2606
+ "flow_name": self.flow.name,
2607
+ "run_id": "argo-{{workflow.name}}",
2608
+ },
2556
2609
  }
2557
2610
  )
2558
2611
  )
@@ -2574,9 +2627,7 @@ class ArgoWorkflows(object):
2574
2627
  "{{workflow.name}}",
2575
2628
  )
2576
2629
  links.append(url)
2577
- if links:
2578
- links = ["See details for the run at: ", *links]
2579
- return "\n\n".join(links)
2630
+ return links
2580
2631
 
2581
2632
  def _pager_duty_change_template(self):
2582
2633
  # https://developer.pagerduty.com/docs/ZG9jOjExMDI5NTgy-send-a-change-event
@@ -130,6 +130,7 @@ def argo_workflows(obj, name=None):
130
130
  is_flag=True,
131
131
  default=False,
132
132
  help="Only print out JSON sent to Argo Workflows. Do not deploy anything.",
133
+ hidden=True,
133
134
  )
134
135
  @click.option(
135
136
  "--max-workers",
@@ -182,14 +183,9 @@ def argo_workflows(obj, name=None):
182
183
  help="Incident.io API V2 key for workflow success/failure notifications.",
183
184
  )
184
185
  @click.option(
185
- "--incident-io-success-severity-id",
186
- default=None,
187
- help="Incident.io severity id for success alerts.",
188
- )
189
- @click.option(
190
- "--incident-io-error-severity-id",
186
+ "--incident-io-alert-source-config-id",
191
187
  default=None,
192
- help="Incident.io severity id for error alerts.",
188
+ help="Incident.io Alert source config ID. Example '01GW2G3V0S59R238FAHPDS1R66'",
193
189
  )
194
190
  @click.option(
195
191
  "--enable-heartbeat-daemon/--no-enable-heartbeat-daemon",
@@ -229,8 +225,7 @@ def create(
229
225
  notify_slack_webhook_url=None,
230
226
  notify_pager_duty_integration_key=None,
231
227
  notify_incident_io_api_key=None,
232
- incident_io_success_severity_id=None,
233
- incident_io_error_severity_id=None,
228
+ incident_io_alert_source_config_id=None,
234
229
  enable_heartbeat_daemon=True,
235
230
  deployer_attribute_file=None,
236
231
  enable_error_msg_capture=False,
@@ -287,8 +282,7 @@ def create(
287
282
  notify_slack_webhook_url,
288
283
  notify_pager_duty_integration_key,
289
284
  notify_incident_io_api_key,
290
- incident_io_success_severity_id,
291
- incident_io_error_severity_id,
285
+ incident_io_alert_source_config_id,
292
286
  enable_heartbeat_daemon,
293
287
  enable_error_msg_capture,
294
288
  )
@@ -464,8 +458,7 @@ def make_flow(
464
458
  notify_slack_webhook_url,
465
459
  notify_pager_duty_integration_key,
466
460
  notify_incident_io_api_key,
467
- incident_io_success_severity_id,
468
- incident_io_error_severity_id,
461
+ incident_io_alert_source_config_id,
469
462
  enable_heartbeat_daemon,
470
463
  enable_error_msg_capture,
471
464
  ):
@@ -488,19 +481,18 @@ def make_flow(
488
481
  "https://api.slack.com/messaging/webhooks to generate a webhook url.\n"
489
482
  " For notifications through PagerDuty, generate an integration key by following the instructions at "
490
483
  "https://support.pagerduty.com/docs/services-and-integrations#create-a-generic-events-api-integration\n"
491
- " For notifications through Incident.io, generate an API key with a permission to create incidents."
484
+ " For notifications through Incident.io, generate an alert source config."
492
485
  )
493
486
 
494
- if notify_incident_io_api_key:
495
- if notify_on_error and incident_io_error_severity_id is None:
496
- raise MetaflowException(
497
- "Incident.io error notifications require a severity id. Please set one with --incident-io-error-severity-id"
498
- )
487
+ if (
488
+ (notify_on_error or notify_on_success)
489
+ and notify_incident_io_api_key
490
+ and incident_io_alert_source_config_id is None
491
+ ):
492
+ raise MetaflowException(
493
+ "Incident.io alerts require an alert source configuration ID. Please set one with --incident-io-alert-source-config-id"
494
+ )
499
495
 
500
- if notify_on_success and incident_io_success_severity_id is None:
501
- raise MetaflowException(
502
- "Incident.io success notifications require a severity id. Please set one with --incident-io-success-severity-id"
503
- )
504
496
  # Attach @kubernetes and @environment decorator to the flow to
505
497
  # ensure that the related decorator hooks are invoked.
506
498
  decorators._attach_decorators(
@@ -545,8 +537,7 @@ def make_flow(
545
537
  notify_slack_webhook_url=notify_slack_webhook_url,
546
538
  notify_pager_duty_integration_key=notify_pager_duty_integration_key,
547
539
  notify_incident_io_api_key=notify_incident_io_api_key,
548
- incident_io_success_severity_id=incident_io_success_severity_id,
549
- incident_io_error_severity_id=incident_io_error_severity_id,
540
+ incident_io_alert_source_config_id=incident_io_alert_source_config_id,
550
541
  enable_heartbeat_daemon=enable_heartbeat_daemon,
551
542
  enable_error_msg_capture=enable_error_msg_capture,
552
543
  )
@@ -171,12 +171,16 @@ class ArgoWorkflowsTriggeredRun(TriggeredRun):
171
171
  command_obj.sync_wait()
172
172
  return command_obj.process.returncode == 0
173
173
 
174
- def wait_for_completion(self, timeout: Optional[int] = None):
174
+ def wait_for_completion(
175
+ self, check_interval: int = 5, timeout: Optional[int] = None
176
+ ):
175
177
  """
176
178
  Wait for the workflow to complete or timeout.
177
179
 
178
180
  Parameters
179
181
  ----------
182
+ check_interval: int, default: 5
183
+ Frequency of checking for workflow completion, in seconds.
180
184
  timeout : int, optional, default None
181
185
  Maximum time in seconds to wait for workflow completion.
182
186
  If None, waits indefinitely.
@@ -187,7 +191,6 @@ class ArgoWorkflowsTriggeredRun(TriggeredRun):
187
191
  If the workflow does not complete within the specified timeout period.
188
192
  """
189
193
  start_time = time.time()
190
- check_interval = 5
191
194
  while self.is_running:
192
195
  if timeout is not None and (time.time() - start_time) > timeout:
193
196
  raise TimeoutError(