Skip to content

Improve metrics for _entities and fields #1297

@rickbijkerk

Description

@rickbijkerk

There's 2 improvements i see:

  1. Currently metrics like: graphql_datafetcher_seconds_count group all _entities interactions under _entities. For applications which have several entityMappings defined you cant differentiate between which entity is being resolved.
  2. In applications which have multiple schema mappings for the same field but different objects, it currently all gets combined under the field name, which makes it impossible to separate which type this field was being called on.

I have done some testing and the following seems to solve both issues

@Configuration
class GraphQLObservationConfig {

    @Bean
    fun dataFetcherObservationConvention(): DataFetcherObservationConvention = object : DefaultDataFetcherObservationConvention() {

        override fun fieldName(context: DataFetcherObservationContext): KeyValue {
            val env = context.environment
            val parent = (env.parentType as? GraphQLNamedType)?.name
            val field = env.field.name

            val enrichedField = if (field == "_entities") {
                // Normally all _entities queries are processed as _entities but since we have so many different entities we want
                // to know exactly which entities is being called
                (env.getArgument<Any?>("representations") as? List<*>)
                    ?.firstNotNullOfOrNull { rep -> (rep as? Map<*, *>)?.get("__typename") as? String }
                    ?.let { "$field($it)" } ?: field
            } else {
                // Converts a field to a <parent>.<field> metric for cases where we want to differentiate between fields
                // with the same name defined on different types, which would otherwise all be aggregated under the field name
                parent?.let { "$it.$field" } ?: field
            }

            return KeyValue.of(DataFetcherLowCardinalityKeyNames.FIELD_NAME, enrichedField)
        }
    }
}

it changes

  • e.g. the graphql_field_name for _entities queries to _entities(<entityName>)
  • the graphql_field_name for fields to <parent>.<fieldName>

If this could be made part of Spring Graphql it'd be greatly appreciated!

Let me know if any clarifications are needed!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions