graphite-project / graphite-web

A highly scalable real-time graphing system
http://graphite.readthedocs.org/
Apache License 2.0
5.89k stars 1.26k forks source link

[Q] Adding custom function #2687

Closed rdcm closed 3 years ago

rdcm commented 3 years ago

Hi!

I'm trying to add a custom function to graphite as described in the documentation, and after all steps added function not displayed in grafana. https://graphite.readthedocs.io/en/latest/functions.html#function-plugins

docker-compose.yml:

version: '3.6'

services:
  graphite:
    image: graphiteapp/graphite-statsd
    container_name: graphite
    restart: always
    ports:
      - "80:80"
      - "2003-2004:2003-2004"
      - "2023-2024:2023-2024"
      - "8125:8125/udp"
      - "8126:8126"
    volumes:
      - graphite_data:/opt/graphite/storage
      - ./to_upper.py:/opt/graphite/webapp/graphite/functions/custom/to_upper.py
    networks:
      - dev

  grafana:
    image: grafana/grafana
    container_name: grafana
    restart: always
    depends_on:
      - graphite
    ports:
      - "3000:3000"
    volumes:
      - grafana_data:/var/lib/grafana
    networks:
      - dev

networks:
  dev:
    name: dev
    driver: bridge

volumes:
  grafana_data:
    name: grafana_data

  graphite_data:
    name: graphite_data

to_upper.py:

from graphite.functions.params import Param, ParamTypes

def toUpperCase(requestContext, seriesList):
  """Custom function that changes series names to UPPERCASE"""
  for series in seriesList:
    series.name = series.name.upper()
  return seriesList

toUpperCase.group = 'Custom'
toUpperCase.params = [
  Param('seriesList', ParamTypes.seriesList, required=True),
]

SeriesFunctions = {
  'upper': toUpperCase,
}

As mentioned in the documentation:

Custom plugin files may be placed in the /opt/graphite/webapp/graphite/functions/custom folder and will be loaded automatically when graphite starts.

Should I explicitly adding my function to FUNCTION_PLUGINS?

Thanks.

deniszh commented 3 years ago

Hi @rdcm ,

If you open http://localhost/functions?pretty=1 - is it shows your function?

rdcm commented 3 years ago

Hi @deniszh

Yes, the list of functions contains following:

  "upper": {
    "description": "Custom function that changes series names to UPPERCASE",
    "function": "upper(seriesList)",
    "group": "Custom",
    "module": "graphite.functions.custom.to_upper",
    "name": "upper",
    "params": [
      {
        "name": "seriesList",
        "required": true,
        "type": "seriesList"
      }
    ]
  },

How can I use it with grafana? In my mind upper() should be listed here: image

deniszh commented 3 years ago

According to https://grafana.com/blog/2018/01/11/graphite-1.1-teaching-an-old-dog-new-tricks/ Grafana should use exact that API to get list of functions - https://graphite.readthedocs.io/en/1.1.1/functions.html#function-api Which Graphite version you have defined in Graphite datasource - https://grafana.com/docs/grafana/latest/datasources/graphite/ ? Should be 1.1.x or exact version, not 1.0.x or 0.9.x

rdcm commented 3 years ago

Should be 1.1.x or exact version, not 1.0.x or 0.9.x

Specified 1.1.x version: image

deniszh commented 3 years ago

Then I can only suggest open issue in Grafana repo - https://github.com/grafana/grafana because according their docs it should work 🤷

rdcm commented 3 years ago

Thanks for the additional info, I need a little bit more time to investigate this.

ifrost commented 3 years ago

If you're usingGraphite 1.7 it might be caused by a bug that should get fixed in Grafana 8: https://github.com/grafana/grafana/pull/32305

If you're using Graphite <1.7 you might be affected by a different bug: https://github.com/grafana/grafana/pull/31680. If your data source was created in Grafana <7.4.0 it might be incorrectly recognised as Graphite 0.9 (with no custom functions support) even when data source config shows 1.1.x. The easiest way to fix it is to change the version of Graphite datasource from 1.1.x to something else and then change it back to 1.1.x.

deniszh commented 3 years ago

Right. Looks like I need to carve out time and release 1.1.8, though. Thanks for info, @ifrost !

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.