code: def test_log_metrics(mongo_obs, sample_run, logged_metrics):
      """
      Test storing scalar measurements
  
      Test whether measurements logged using _run.metrics.log_scalar_metric
      are being stored in the 'metrics' collection
      and that the experiment 'info' dictionary contains a valid reference
      to the metrics collection for each of the metric.
  
      Metrics are identified by name (e.g.: 'training.loss') and by the
      experiment run that produced them. Each metric contains a list of x values
      (e.g. iteration step), y values (measured values) and timestamps of when
      each of the measurements was taken.
      """
  
      # Start the experiment
      mongo_obs.started_event(**sample_run)
  
      # Initialize the info dictionary and standard output with arbitrary values
      info = {"my_info": [1, 2, 3], "nr": 7}
      outp = "some output"
  
      # Take first 6 measured events, group them by metric name
      # and store the measured series to the 'metrics' collection
      # and reference the newly created records in the 'info' dictionary.
      mongo_obs.log_metrics(linearize_metrics(logged_metrics[:6]), info)
      # Call standard heartbeat event (store the info dictionary to the database)
      mongo_obs.heartbeat_event(
          info=info, captured_out=outp, beat_time=T1, result=0
      )
      # Now, process the remaining events
      # The metrics shouldn't be overwritten, but appended instead.
      mongo_obs.log_metrics(linearize_metrics(logged_metrics[6:]), info)
      mongo_obs.heartbeat_event(
          info=info, captured_out=outp, beat_time=T2, result=0
      )
      mongo_obs.join()
  
      assert mongo_obs.runs.count_documents({}) == 1
      db_run = mongo_obs.runs.find_one()
      assert "metrics" in db_run["info"]
  
      # The newly added metrics belong to the same run and have the same names, so the total number
      # of metrics should not change.
      assert mongo_obs.metrics.count_documents({}) == 2
      loss = mongo_obs.metrics.find_one(
          {"name": "training.loss", "run_id": db_run["_id"]}
      )
      assert {
          "name": "training.loss",
          "id": str(loss["_id"]),
      } in db_run["info"]["metrics"]
      # ... but the values should be appended to the original list
      assert loss["steps"] == [10, 20, 30, 40, 50, 60]
      assert loss["values"] == [1, 2, 3, 10, 20, 30]
      for i in range(len(loss["timestamps"]) - 1):
          assert loss["timestamps"][i] <= loss["timestamps"][i + 1]
  
      accuracy = mongo_obs.metrics.find_one(
          {"name": "training.accuracy", "run_id": db_run["_id"]}
      )
      assert {
          "name": "training.accuracy",
          "id": str(accuracy["_id"]),
      } in db_run["info"]["metrics"]
      assert accuracy["steps"] == [10, 20, 30]
      assert accuracy["values"] == [100, 200, 300]
  
      # Make sure that when starting a new experiment, new records in metrics are created
      # instead of appending to the old ones.
      sample_run["_id"] = "NEWID"
      # Start the experiment
      mongo_obs.started_event(**sample_run)
      mongo_obs.log_metrics(linearize_metrics(logged_metrics[:4]), info)
      mongo_obs.heartbeat_event(
          info=info, captured_out=outp, beat_time=T1, result=0
      )
      mongo_obs.join()
      # A new run has been created
      assert mongo_obs.runs.count_documents({}) == 2
      # Another 2 metrics have been created
      assert mongo_obs.metrics.count_documents({}) == 4
  
imports: import datetime
  from sacred.observers.queue import QueueObserver
  import mock
  import pytest
  import sys
  from sacred.metrics_logger import ScalarMetricLogEntry, linearize_metrics
  import gridfs
  from mongomock.gridfs import enable_gridfs_integration
  import pymongo.errors
  from sacred.dependencies import get_digest
  from sacred.observers.mongo import QueuedMongoObserver, MongoObserver, QueueCompatibleMongoObserver
  from .failing_mongo_mock import ReconnectingMongoClient
label_desc: Use image node_id 13 for calling a global function with example usage: test_log_metrics(mongo_obs, sample_run, logged_metrics) without return types