envelop icon indicating copy to clipboard operation
envelop copied to clipboard

Configure context derived labels for all metrics

Open ethanrcohen opened this issue 8 months ago • 3 comments

Is your feature request related to a problem? Please describe.

Users may wish to apply some labels to all metrics based on the specific request context -- for example, a server that acts as a backend for multiple front-end clients may wish to label metrics according to the client type.

RIght now, the best way that I can see to do this is to provide createHistogram/createSummary/createCounter functions for each metric collected, ex:

usePrometheus({
            endpoint: false,
            requestCount: createCounter({
                registry,
                counter: {
                    name: "graphql_request_count",
                    help: "Number of GraphQL requests",
                    labelNames: ["operationName", "operationType"],
                },
                fillLabelsFn: (params, rawContext) => {
                    return {
                        operationName: params.operationName,
                        operationType: params.operationType,
                        clientType: getClientTypeFromContext(rawContext),
                    };
                },
            }),
            requestTotalDuration: createHistogram({
                registry,
                histogram: {
                    name: "graphql_request_duration_seconds",
                    help: "Duration of GraphQL requests in seconds",
                    labelNames: ["operationName", "operationType"],
                },
                fillLabelsFn: (params, rawContext) => {
                    return {
                        operationName: params.operationName,
                        operationType: params.operationType,
                        clientType: getClientTypeFromContext(rawContext),
                    };
                },
            }),
            ...

Describe the solution you'd like

It would be nice if there were a configuration option on the plugin that allowed the user to provide a function that takes the context and returns some labels.

contextLabels?: (context: TUserContext) =>  Record<LabelNames, string | number>;

Describe alternatives you've considered

It's possible that I'm missing something that allows one to do this already. If so, apologies.

Additional context

ethanrcohen avatar May 29 '24 19:05 ethanrcohen