Graphite、Carbon、CollectD、StatsD - Python 程序中的标记系列

Grahite, Carbon, CollectD, StatsD - Tagged Series in Python Program

我正在尝试 运行 一些 python/other 语言模块 modules/workflows/workloads 并使用 Grahite 收集它们的 CPU、Mem、I/O 等资源使用情况, Carbon、CollectD、StatsD。我已阅读有关创建标记系列的文档(请参阅:here),但我似乎无法找到如何标记特定模块的内容。比如我有两个模块

第一个模块

def firstModule:
    # Initialize a list
    primes = []

    for possiblePrime in range(2, 21):
       # Assume number is prime until shown it is not. 

       isPrime = True

       for num in range(2, possiblePrime):
          if possiblePrime % num == 0:
          isPrime = False

    if isPrime:
        primes.append(possiblePrime)

第二个模块

def secondModule:
    # Initialize a list

    primes = []

    for possiblePrime in range(2, 21):

        # Assume number is prime until shown it is not. 

        isPrime = True

        for num in range(2, possiblePrime):
            if possiblePrime % num == 0:
            isPrime = False
            break

    if isPrime:
        primes.append(possiblePrime)

这里我想调用这两个模块,然后标记资源使用指标,以便我可以将其发送到 whisper 数据库,如下代码所示:如何实现?

firstModule() # assign some tag say A
secondModule() # assign some tag say B

虽然我最终没有使用标记系列,但我通过编写 bash 脚本并使用石墨中的 render api 解决了上述问题。这是解决方案:

    #!/bin/bash   

    #Remove previous records. Be careful not to remove useful files!
    rm cpu.json, mem.json

    #clear variables for storing time 
    unset start
    unset finish

    #log start time of process using absolute time, if using relative time check doc at https://graphite.readthedocs.io/en/latest/render_api.html for rules
    start=$(date '+%H:%M_%Y%m%d')

    #call first module
    firstModule()

    #sleep 1 minute or more because you will get an error that start time and end time are equal
    sleep 100

    #call second module
    secondModule()

    #log finish date using absolute time, if using relative time check  doc at https://graphite.readthedocs.io/en/latest/render_api.html) for rules
    finish=$(date '+%H:%M_%Y%m%d')

    #collect data using curl here cpu usage and memusage, adjust to fit your setup and Voila!
    curl "http://host/render?target=carbon.agents.*.cpuUsage&width=500&height=300&from={$start}&until={$finish}&format=json" > cpu.json
    sudo curl "http://host/render?target=collectdlocalhost.memory.memory-used&width=500&height=300&from={$start}&until={$finish}&format=json" > mem.json