code: def collect(self) -> Iterator[Metric]:
      ws = self.server.state
  
      tasks = GaugeMetricFamily(
          self.build_name("tasks"),
          "Number of tasks at worker.",
          labels=["state"],
      )
      for state, n in ws.task_counter.current_count(
          by_prefix=False
      ).items():
          if state == "memory" and hasattr(self.server.data, "slow"):
              n_spilled = len(self.server.data.slow)
              if n - n_spilled > 0:
                  tasks.add_metric(["memory"], n - n_spilled)
              if n_spilled > 0:
                  tasks.add_metric(["disk"], n_spilled)
          else:
              tasks.add_metric([state], n)
      yield tasks
  
      yield GaugeMetricFamily(
          self.build_name("concurrent_fetch_requests"),
          (
              "Deprecated: This metric has been renamed to transfer_incoming_count.\n"
              "Number of open fetch requests to other workers"
          ),
          value=ws.transfer_incoming_count,
      )
  
      if self.server.monitor.monitor_gil_contention:
          yield CounterMetricFamily(
              self.build_name("gil_contention"),
              "GIL contention metric",
              value=self.server.monitor._cumulative_gil_contention,
          )
  
      yield GaugeMetricFamily(
          self.build_name("threads"),
          "Number of worker threads",
          value=ws.nthreads,
      )
  
      yield GaugeMetricFamily(
          self.build_name("latency"),
          "Latency of worker connection",
          unit="seconds",
          value=self.server.latency,
      )
  
      try:
          spilled_memory, spilled_disk = self.server.data.spilled_total  # type: ignore
      except AttributeError:
          spilled_memory, spilled_disk = 0, 0  # spilling is disabled
      process_memory = self.server.monitor.get_process_memory()
      managed_memory = min(process_memory, ws.nbytes - spilled_memory)
  
      memory = GaugeMetricFamily(
          self.build_name("memory_bytes"),
          "Memory breakdown",
          labels=["type"],
      )
      memory.add_metric(["managed"], managed_memory)
      memory.add_metric(["unmanaged"], process_memory - managed_memory)
      memory.add_metric(["spilled"], spilled_disk)
      yield memory
  
      yield GaugeMetricFamily(
          self.build_name("transfer_incoming_bytes"),
          "Total size of open data transfers from other workers",
          value=ws.transfer_incoming_bytes,
      )
      yield GaugeMetricFamily(
          self.build_name("transfer_incoming_count"),
          "Number of open data transfers from other workers",
          value=ws.transfer_incoming_count,
      )
  
      yield CounterMetricFamily(
          self.build_name("transfer_incoming_count_total"),
          (
              "Total number of data transfers from other workers "
              "since the worker was started"
          ),
          value=ws.transfer_incoming_count_total,
      )
  
      yield GaugeMetricFamily(
          self.build_name("transfer_outgoing_bytes"),
          "Total size of open data transfers to other workers",
          value=self.server.transfer_outgoing_bytes,
      )
      yield GaugeMetricFamily(
          self.build_name("transfer_outgoing_count"),
          "Number of open data transfers to other workers",
          value=self.server.transfer_outgoing_count,
      )
  
      yield CounterMetricFamily(
          self.build_name("transfer_outgoing_bytes_total"),
          (
              "Total size of data transfers to other workers "
              "since the worker was started (including in-progress and failed transfers)"
          ),
          value=self.server.transfer_outgoing_bytes_total,
      )
  
      yield CounterMetricFamily(
          self.build_name("transfer_outgoing_count_total"),
          (
              "Total number of data transfers to other workers "
              "since the worker was started"
          ),
          value=self.server.transfer_outgoing_count_total,
      )
  
      yield from self.collect_crick()
      yield from self.collect_spillbuffer()
  
      now = time()
      max_tick_duration = max(
          self.server.digests_max.pop("tick_duration", 0),
          now - self.server._last_tick,
      )
      yield GaugeMetricFamily(
          self.build_name("tick_duration_maximum"),
          "Maximum tick duration observed since Prometheus last scraped metrics",
          unit="seconds",
          value=max_tick_duration,
      )
      yield CounterMetricFamily(
          self.build_name("tick_count"),
          "Total number of ticks observed since the server started",
          value=self.server._tick_counter,
      )
  
imports: from __future__ import annotations
  import logging
  from collections.abc import Iterator
  from time import time
  from typing import ClassVar
  import prometheus_client
  from prometheus_client.core import CounterMetricFamily, GaugeMetricFamily, Metric
  from distributed.http.prometheus import PrometheusCollector
  from distributed.http.utils import RequestHandler
  from distributed.worker import Worker
label_desc: Use image node_id 2 for calling the WorkerMetricCollector obj's underlying member method code with example usage: obj.collect() without return types