class_docstr: Scheduler extension for spans support
class_docstr_tok: ["Scheduler","extension","for","spans","support"]
docstr: Create Span if it doesn't exist and return it
docstr_tok: ["Create","Span","if","it","does","n't","exist","and","return","it"]
code: def _ensure_span(
      self, name: tuple[str, ...], ids: tuple[str, ...]
  ) -> Span:
      """Create Span if it doesn't exist and return it"""
      try:
          return self.spans[ids[-1]]
      except KeyError:
          pass
  
      assert len(name) == len(ids)
      assert len(name) > 0
  
      parent = None
      for i in range(1, len(name)):
          parent = self._ensure_span(name[:i], ids[:i])
  
      span = Span(
          name=name,
          id_=ids[-1],
          parent=parent,
          total_nthreads_history=self.scheduler.total_nthreads_history,
      )
      self.spans[span.id] = span
      self.spans_search_by_name[name].append(span)
      for tag in name:
          self.spans_search_by_tag[tag].append(span)
      if parent:
          parent.children.append(span)
      else:
          self.root_spans.append(span)
  
      return span
  
imports: from __future__ import annotations
  import uuid
  import weakref
  from collections import defaultdict
  from collections.abc import Hashable, Iterable, Iterator
  from contextlib import contextmanager
  from itertools import islice
  from typing import TYPE_CHECKING, Any
  import dask.config
  from dask.typing import Key
  from distributed.collections import sum_mappings
  from distributed.itertools import ffill
  from distributed.metrics import time
label_desc: Use image node_id 4 for calling the SpansSchedulerExtension obj's underlying member method code with example usage: obj._ensure_span(name, ids) and returns: span, self