github.com/hellofresh/janus@v0.0.0-20230925145208-ce8de8183c67/pkg/observability/observability.go (about)

     1  package observability
     2  
     3  import (
     4  	"contrib.go.opencensus.io/exporter/prometheus"
     5  	"go.opencensus.io/plugin/ochttp"
     6  	"go.opencensus.io/stats"
     7  	"go.opencensus.io/stats/view"
     8  	"go.opencensus.io/tag"
     9  )
    10  
    11  const (
    12  	by            = "By"
    13  	ms            = "ms"
    14  	dimensionless = "1"
    15  )
    16  
    17  // Known exporters
    18  const (
    19  	AzureMonitor = "azure_monitor"
    20  	Datadog      = "datadog"
    21  	Jaeger       = "jaeger"
    22  	Prometheus   = "prometheus"
    23  	Stackdriver  = "stackdriver"
    24  	Zipkin       = "zipkin"
    25  )
    26  
    27  // PrometheusExporter is the prometheus exporter containing HTTP handler for "/metrics"
    28  var PrometheusExporter *prometheus.Exporter
    29  
    30  // Tags
    31  var (
    32  	KeyListenPath, _             = tag.NewKey("path")
    33  	KeyUpstreamPath, _           = tag.NewKey("upstream_path")
    34  	KeyJWTValidationErrorType, _ = tag.NewKey("error")
    35  )
    36  
    37  // Metrics
    38  var (
    39  	MJWTManagerValidationErrors = stats.Int64("plugin_jwt_manager_validation_error_total", "Number of validation errors by error type", dimensionless)
    40  	MOAuth2MissingHeader        = stats.Int64("plugin_oauth2_missing_header_total", "Number of failed oauth2 authentication due to missing header", dimensionless)
    41  	MOAuth2MalformedHeader      = stats.Int64("plugin_oauth2_malformed_header_total", "Number of failed oauth2 authentication due to malformed bearer header", dimensionless)
    42  	MOAuth2Authorized           = stats.Int64("plugin_oauth2_authorized_request_total", "Number of successful and authorized oauth2 authentication", dimensionless)
    43  	MOAuth2Unauthorized         = stats.Int64("plugin_oauth2_unauthorized_request_total", "Number of successful but unauthorized oauth2 authentication", dimensionless)
    44  )
    45  
    46  // AllViews aggregates the metrics
    47  var AllViews = []*view.View{
    48  	{
    49  		Name:        "plugin_jwt_manager_validation_error_total",
    50  		TagKeys:     []tag.Key{KeyJWTValidationErrorType},
    51  		Measure:     MJWTManagerValidationErrors,
    52  		Aggregation: view.Count(),
    53  	},
    54  	{
    55  		Name:        "plugin_oauth2_missing_header_total",
    56  		Measure:     MOAuth2MissingHeader,
    57  		Aggregation: view.Count(),
    58  	},
    59  	{
    60  		Name:        "plugin_oauth2_malformed_header_total",
    61  		Measure:     MOAuth2MalformedHeader,
    62  		Aggregation: view.Count(),
    63  	},
    64  	{
    65  		Name:        "plugin_oauth2_authorized_request_total",
    66  		Measure:     MOAuth2Authorized,
    67  		Aggregation: view.Count(),
    68  	},
    69  	{
    70  		Name:        "plugin_oauth2_unauthorized_request_total",
    71  		Measure:     MOAuth2Unauthorized,
    72  		Aggregation: view.Count(),
    73  	},
    74  	{
    75  		Name:        "http_server_response_count_by_path_code_and_method",
    76  		TagKeys:     []tag.Key{KeyListenPath, ochttp.StatusCode, ochttp.Method},
    77  		Measure:     ochttp.ServerLatency,
    78  		Aggregation: view.Count(),
    79  	},
    80  	{
    81  		Name:        "http_server_request_latency_by_path_and_method",
    82  		TagKeys:     []tag.Key{KeyListenPath, ochttp.Method},
    83  		Measure:     ochttp.ServerLatency,
    84  		Aggregation: ochttp.DefaultLatencyDistribution,
    85  	},
    86  	{
    87  		Name:        "http_server_request_size",
    88  		Measure:     ochttp.ServerRequestBytes,
    89  		Aggregation: ochttp.DefaultSizeDistribution,
    90  	},
    91  	{
    92  		Name:        "http_proxy_request_count_by_path",
    93  		TagKeys:     []tag.Key{KeyUpstreamPath},
    94  		Measure:     ochttp.ClientRequestCount,
    95  		Aggregation: view.Count(),
    96  	},
    97  	{
    98  		Name:        "http_proxy_request_latency_by_path",
    99  		TagKeys:     []tag.Key{KeyUpstreamPath},
   100  		Measure:     ochttp.ClientLatency,
   101  		Aggregation: ochttp.DefaultLatencyDistribution,
   102  	},
   103  }