github.com/muhammadn/cortex@v1.9.1-0.20220510110439-46bb7000d03d/docs/contributing/design-patterns-and-conventions.md (about)

     1  ---
     2  title: "Design patterns and Code conventions"
     3  linkTitle: "Design patterns and Code conventions"
     4  weight: 2
     5  slug: design-patterns-and-code-conventions
     6  ---
     7  
     8  Cortex adopts some design patterns and code conventions that we ask you to follow when contributing to the project. These conventions have been adopted based on the experience gained over the time and aim to enforce good coding practices and keep a consistent UX (ie. config).
     9  
    10  ## Go coding style
    11  
    12  Cortex follows the [Go Code Review Comments](https://github.com/golang/go/wiki/CodeReviewComments) styleguide and the [Formatting and style](https://peter.bourgon.org/go-in-production/#formatting-and-style) section of Peter Bourgon's [Go: Best Practices for Production Environments](https://peter.bourgon.org/go-in-production/).
    13  
    14  ## No global variables
    15  
    16  - Do not use global variables
    17  
    18  ## Prometheus metrics
    19  
    20  When registering a metric:
    21  
    22  - Do not use a global variable for the metric
    23  - Create and register the metric with `promauto.With(reg)`
    24  - In any internal Cortex component, do not register the metric to the default prometheus registerer, but pick the registerer in input (ie. `NewComponent(reg prometheus.Registerer)`)
    25  
    26  Testing metrics:
    27  
    28  - When writing using tests, test exported metrics using `testutil.GatherAndCompare()`
    29  
    30  ## Config file and CLI flags conventions
    31  
    32  Naming:
    33  
    34  - Config file options should be lowercase with words `_` (underscore) separated (ie. `memcached_client`)
    35  - CLI flags should be lowercase with words `-` (dash) separated (ie. `memcached-client`)
    36  - When adding a new config option, look if a similar one already exists within the [config](../configuration/config-file-reference.md) and keep the same naming (ie. `addresses` for a list of network endpoints)
    37  
    38  Documentation:
    39  
    40  - A CLI flag mentioned in the documentation or changelog should be always prefixed with a single `-` (dash)