Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Newbie: Defining Metrics Programmatically Not Working #920

Closed
@prime-minister-of-fun

Description

Hi,
I'm attempting to build a scraper that returns both values and the metric definition dynamically.

If I hardcode two metrics at the global level of the script:
DEMO_EVENTS = Gauge('demo_notification', 'A value')
OTHER_EVENTS = Gauge('other_notification', 'A value', ,['server', 'region'])
def foo (a_dictionary):
 for key in a_dictionary:
 ..parse dictionary into commands..
 exec(DEMO_EVENTS.set(2))
 exec(OTHER_EVENTS.labels( server=tomato, region=us-west).set(3)
 exec(OTHER_EVENTS.labels( server=orange, region=us-east).set(3)
 return()
 foo(a_dictionary)

Everything works as expected.
If I add the below, and remove the global declaration, then python returns an error.

def bar (a_dictionary)
 for key in a_dictionary:
 .... parse dictionary into commands....
 exec("global DEMO_EVENTS")
 exec("OTHER_EVENTS = Gauge('other_notification', 'A value', ,['server', 'region'])")
 
 bar(a_dictionary)
 foo(a_dictionary)

This example returns an error on attempting .set(). The error:"name 'OTHER_EVENTS' is not defined". Python does not register the global object. You can delete the global command and the error is the same.

My gut feeling is, I'm missing something. Is dynamically generating the metric not supported?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

        AltStyle によって変換されたページ (->オリジナル) /