def _addAutostackMetric(self, conn, autostackObj, name=None, **kwargs):
    name = name or "AWS/EC2/CPUUtilization"

    modelSpec = {"modelParams": {},
                 "datasource": "autostack",
                 "metricSpec": {"slaveDatasource": "cloudwatch" if name.startswith("AWS/EC2") else "autostack",
                                "slaveMetric": {"metric": name,
                                                "namespace": "AWS/EC2"},
                                "autostackId": autostackObj.uid}}

    metricDict = repository.addMetric(
      conn,
      datasource="autostack",
      name=name,
      description=("{0} on HTM-IT Autostack {1} in {2} "
                   "region").format(name, autostackObj.name, autostackObj.region),
      server="Autostacks/{0}".format(autostackObj.uid),
      location=autostackObj.region,
      tag_name=name,
      parameters=htmengine.utils.jsonEncode(modelSpec),
      poll_interval=300,
      status=MetricStatus.UNMONITORED)

    metricObj = repository.getMetric(conn, metricDict["uid"])

    repository.addMetricToAutostack(conn, autostackObj.uid, metricObj.uid)

    metricObj = type("MutableMetric", (object,), dict(metricObj.items()))()

    return metricObj
    def startMonitoringWithRetries():
      """
      :returns: metricId
      """
      with self.connectionFactory() as conn:
        with conn.begin():
          repository.lockOperationExclusive(conn,
                                            repository.OperationLock.METRICS)

          # Check if the metric is already monitored
          matchingMetrics = repository.getAutostackMetricsWithMetricName(
            conn,
            autostackId,
            nameColumnValue,
            fields=[schema.metric.c.uid])

          matchingMetric = next(iter(matchingMetrics), None)

          if matchingMetric:
            msg = ("monitorMetric: Autostack modelId=%s is already "
                   "monitoring metric=%s on resource=%s; model=%r"
                   % (matchingMetric.uid, nameColumnValue,
                      canonicalResourceName, matchingMetric))
            self._log.warning(msg)
            raise htm.it.app.exceptions.MetricAlreadyMonitored(
                    msg,
                    uid=matchingMetric.uid)

          # Add a metric row for the requested metric
          metricDict = repository.addMetric(
            conn,
            datasource=self._DATASOURCE,
            name=nameColumnValue,
            description=metricDescription,
            server=canonicalResourceName,
            location=autostack.region,
            tag_name=autostack.name,
            parameters=htmengine.utils.jsonEncode(modelSpec),
            poll_interval=period,
            status=MetricStatus.UNMONITORED)

          metricId = metricDict["uid"]

          repository.addMetricToAutostack(conn, autostackId, metricId)

          # Start monitoring
          scalar_metric_utils.startMonitoring(
            conn=conn,
            metricId=metricId,
            swarmParams=swarmParams,
            logger=self._log)

      self._log.info("monitorMetric: monitoring metric=%s, stats=%r",
                     metricId, stats)

      return metricId
        def _createAutostackMetric(conn, name, region, filters):
            autostackDict = repository.addAutostack(
                conn, name=name, region=region, filters=json.dumps(filters))

            modelSpec = {
                "modelParams": {},
                "datasource": "autostack",
                "metricSpec": {
                    "slaveDatasource": "cloudwatch",
                    "slaveMetric": {
                        "metric": "CPUUtilization",
                        "namespace": "AWS/EC2"
                    },
                    "autostackId": autostackDict["uid"]
                }
            }

            metricDict = repository.addMetric(
                conn,
                datasource="autostack",
                name="CPUUtilization",
                description=(
                    "CPUUtilization on HTM-IT Autostack {0} in us-west-2 "
                    "region").format(name),
                server="Autostacks/{0}".format(autostackDict["uid"]),
                location=region,
                tag_name=name,
                parameters=htmengine.utils.jsonEncode(modelSpec),
                poll_interval=300,
                status=MetricStatus.UNMONITORED)

            repository.addMetricToAutostack(conn, autostackDict["uid"],
                                            metricDict["uid"])

            autostackObj = type("MutableAutostack", (object, ),
                                autostackDict)()
            autostackObj.filters = json.loads(autostackObj.filters)

            metricObj = type("MutableMetric", (object, ), metricDict)()

            return autostackObj, metricObj
    def _addAutostackMetric(self, conn, autostackObj, name=None, **kwargs):
        name = name or "AWS/EC2/CPUUtilization"

        modelSpec = {
            "modelParams": {},
            "datasource": "autostack",
            "metricSpec": {
                "slaveDatasource":
                "cloudwatch" if name.startswith("AWS/EC2") else "autostack",
                "slaveMetric": {
                    "metric": name,
                    "namespace": "AWS/EC2"
                },
                "autostackId": autostackObj.uid
            }
        }

        metricDict = repository.addMetric(
            conn,
            datasource="autostack",
            name=name,
            description=("{0} on HTM-IT Autostack {1} in {2} "
                         "region").format(name, autostackObj.name,
                                          autostackObj.region),
            server="Autostacks/{0}".format(autostackObj.uid),
            location=autostackObj.region,
            tag_name=name,
            parameters=htmengine.utils.jsonEncode(modelSpec),
            poll_interval=300,
            status=MetricStatus.UNMONITORED)

        metricObj = repository.getMetric(conn, metricDict["uid"])

        repository.addMetricToAutostack(conn, autostackObj.uid, metricObj.uid)

        metricObj = type("MutableMetric", (object, ),
                         dict(metricObj.items()))()

        return metricObj
    def _createAutostackMetric(conn, name, region, filters):
      autostackDict = repository.addAutostack(conn,
                                              name=name,
                                              region=region,
                                              filters=json.dumps(filters))

      modelSpec = {"modelParams": {},
                   "datasource": "autostack",
                   "metricSpec": {"slaveDatasource": "cloudwatch",
                                  "slaveMetric": {"metric": "CPUUtilization",
                                                  "namespace": "AWS/EC2"},
                                  "autostackId": autostackDict["uid"]}}

      metricDict = repository.addMetric(
          conn,
          datasource="autostack",
          name="CPUUtilization",
          description=("CPUUtilization on HTM-IT Autostack {0} in us-west-2 "
                       "region").format(name),
          server="Autostacks/{0}".format(autostackDict["uid"]),
          location=region,
          tag_name=name,
          parameters=htmengine.utils.jsonEncode(modelSpec),
          poll_interval=300,
          status=MetricStatus.UNMONITORED)

      repository.addMetricToAutostack(conn,
                                      autostackDict["uid"],
                                      metricDict["uid"])

      autostackObj = type("MutableAutostack", (object,), autostackDict)()
      autostackObj.filters = json.loads(autostackObj.filters)

      metricObj = type("MutableMetric", (object,), metricDict)()

      return autostackObj, metricObj