Repository: cloud-custodian/cloud-custodian

Issue:
cli metrics subcommand and azure - throws errors
@kapilt  what should the expected behavior be here?

```
(cloud-custodian) $ custodian metrics policies/policy.yml
2019-02-20 11:19:18,346: custodian.azure.session:INFO Creating session with Azure CLI Authentication
2019-02-20 11:19:18,347: custodian.azure.session:INFO Session using Subscription ID: <my sub redacted>
2019-02-20 11:19:18,347: custodian.commands:INFO Getting <Policy resource: azure.resourcegroup name: delete-empty-resource-groups region: > metrics
Traceback (most recent call last):
  File "/Users/andyluong/Projects/forks/cloud-custodian/bin/custodian", line 11, in <module>
    load_entry_point('c7n', 'console_scripts', 'custodian')()
  File "/Users/andyluong/Projects/forks/cloud-custodian/c7n/cli.py", line 368, in main
    command(config)
  File "/Users/andyluong/Projects/forks/cloud-custodian/c7n/commands.py", line 136, in _load_policies
    return f(options, list(policies))
  File "/Users/andyluong/Projects/forks/cloud-custodian/c7n/commands.py", line 491, in metrics_cmd
    data[p.name] = p.get_metrics(start, end, options.period)
  File "/Users/andyluong/Projects/forks/cloud-custodian/c7n/policy.py", line 912, in get_metrics
    return mode.get_metrics(start, end, period)
  File "/Users/andyluong/Projects/forks/cloud-custodian/c7n/policy.py", line 170, in get_metrics
    client = session.client('cloudwatch')
  File "/Users/andyluong/Projects/forks/cloud-custodian/tools/c7n_azure/c7n_azure/session.py", line 148, in client
    service_name, client_name = client.rsplit('.', 1)
ValueError: not enough values to unpack (expected 2, got 1)
```


Discussion:
Some better docs on what this functionality does would be good too.
So part of this wrapped up in cli compatibility which probably caused more
incompatibility tbh, so -m/—metrics used to be a Boolean to enable multi
cloud support it’s now supporting a provider name after, like aws, whatever
the metrics output was registered with.
On Wed, Feb 20, 2019 at 2:33 PM Andy Luong <notifications@github.com> wrote:

> Some better docs on what this functionality does would be good too.
>
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub
> <https://github.com/cloud-custodian/cloud-custodian/issues/3555#issuecomment-465722470>,
> or mute the thread
> <https://github.com/notifications/unsubscribe-auth/AABUktRQwC7W7_5b_3i-uUgRXXOtI4Tcks5vPaMXgaJpZM4bF7wv>
> .
>

The following works fine in Azure, ```custodian run --metrics azure://<resource_group_name>/<app_insights_name>```, but I am not sure what is expected when we do ```custodian metrics``` oppose to ```custodian run```.
oh.. i kinda of forgot about that subcommand :-)  looking over it now, its supposed to do api calls to retrieve metrics for a policy and dump json to stdout, but its not particular useful in itself, and its missing any notion of multi-cloud abstraction. i suggest we deprecate it and ditch it. i can't imagine it has many users given how little of use it does, compared to native metrics tooling and dashboarding.  we can poll some users on gitter/chat to double check, but i don't see what utility it provides.

The other way we could do this is to go ahead and wire in the multi-cloud support here, which would effectively become a delegation to the policy mode, and we need some specialization of the default 'poll' there by cloud provider, ie probably via switching modes registration to a tuple of provider, name. but its lot of work for little gain afaics.

so, i'm not really sure theres much value in supporting it going forward, the output format would vary widely by provider, and in some contexts (k8s) there isn't any native notion of a builtin metrics endpoint, so its a lot of churn for something thats not useful and that people aren't using.

@thisisshi ^ any comments?
Yeah, I've run into something similar here (not with the metrics command, rather with run) but it's the same failure. delegation to the policy sounds good to me for the `custodian run -m` command, for the metric subcommand I personally haven't used it either, I'd be curious to see if there's any usage in the community as well
to be clear this isn't about custodian run -m ... the reality on issues there is because its not a boolean anymore, it takes a parameter, ie `custodian run -m aws` or azure or gcp. the issue topic is primarily around metrics subcommand.. i pinged the room on chat, i got zero responses wrt, also given its implementation it seems less then useful.


Resolving patch:
diff --git a/c7n/commands.py b/c7n/commands.py
--- a/c7n/commands.py
+++ b/c7n/commands.py
@@ -487,6 +487,8 @@ def _metrics_get_endpoints(options):
 
 @policy_command
 def metrics_cmd(options, policies):
+    log.warning("metrics command is deprecated, and will be removed in future")
+    policies = [p for p in policies if p.provider_name == 'aws']
     start, end = _metrics_get_endpoints(options)
     data = {}
     for p in policies:


Test patch: