Skip to main content

Grafana Loki

Reading time: 0 minute(s) (0 words)

Grafana Loki (or Loki) isย a horizontally-scalable, multi-tenant log aggregation system that is extremely easy to operate. Loki does not index the contents of the logs, but rather a set of labels for each log stream. Nobl9 users can leverage Loki to query and build metrics on top of their logs.

Scope of supportโ€‹

Currently, the Grafana Loki integration with Nobl9 does not support the direct connection.

Authenticationโ€‹

Loki does not provide an authentication layer.ย Authentication is up to the customer.ย Users are expected to run an authenticating reverse proxy in front of their services, such as NGINX using basic auth or an OAuth2 proxy.

Nobl9 collects only the URL for the Loki integration definition and calls the GET /loki/api/v1/query_range URL. For details, refer to HTTP API Grafana Loki | Grafana Loki documentation.

Authenticating Grafana Loki agent with the basic_auth proxyโ€‹

Since Loki does not provide an authentication layer, the authentication method is up to the users. Normally, Loki's users are expected to run an authenticating reverse proxy in front of their services, such as NGINX using basic_auth proxy.

If that's the method you use, the Nobl9 agent version equal to or higher than 0.40.0, allows you to send an additional Authorization request header with the basic_auth. Refer to the section below for more details.

Adding Grafana Loki as a data sourceโ€‹

To add Grafana Loki as a data source in Nobl9 using the agent connection method, follow these steps:

  1. Navigate to Integrations > Sources.
  2. Click .
  3. Click the relevant Source icon.
  4. Choose a relevant connection method (Agent or Direct), then configure the source as described below.

Grafana Loki agentโ€‹

Agent configuration in the UIโ€‹

Follow the instructions below to configure your Grafana Loki agent:

  1. Select one of the following Release Channels:
    • The stable channel is fully tested by the Nobl9 team. It represents the final product; however, this channel does not contain all the new features of a beta release. Use it to avoid crashes and other limitations.
    • The beta channel is under active development. Here, you can check out new features and improvements without the risk of affecting any viable SLOs. Remember that features in this channel may be subject to change.
  2. Add the URL (mandatory).
    The url is an entry point to Grafana Loki. It depends on the configuration of your Loki instance, for more details, refer to the Configuration | Grafana Loki documentation section of Grafana Loki technical documentation.

  1. Select a Project.
    Specifying a project is helpful when multiple users are spread across multiple teams or projects. When the Project field is left blank then object is assigned to project default.
  2. Enter a Display Name.
    You can enter a friendly name with spaces in this field.
  3. Enter a Name.
    The name is mandatory and can only contain lowercase, alphanumeric characters and dashes (for example, my-project-name). This field is populated automatically when you enter a display name, but you can edit the result.
  4. Enter a Description.
    Here you can add details such as who is responsible for the integration (team/owner) and the purpose of creating it.
  5. Specify the Query delay to set a customized delay for queries when pulling the data from the data source.
    • The default value in Grafana Loki integration for Query delay is 1 minute.
    info
    Changing the Query delay may affect your SLI data. For more details, check the Query delay documentation.
  6. Click Add Data Source.

Agent using CLI - YAMLโ€‹

The YAML for setting up an agent connection to Grafana Loki looks like this:

apiVersion: n9/v1alpha
kind: Agent
metadata:
name: grafana-loki-agent
displayName: Grafana Loki Agent # optional
project: default
spec:
description: Agent settings for Grafana Loki datasource # optional
sourceOf:
- Metrics
- Services
releaseChannel: beta # string, one of: beta || stable
queryDelay:
unit: Minute # string, one of: Second || Minute
value: 720 # numeric, must be a number less than 1440 minutes (24 hours)
grafanaLoki:
url: http://loki.example.com

Important notes:

  • url is an entry point to Grafana Loki. The url depends on the configuration of your Loki instance, for more details, refer to the Configuration | Grafana Loki documentation section of Grafana Loki technical documentation.
warning

You can deploy only one agent in one YAML file by using the sloctl apply command.

Deploying Grafana Loki agentโ€‹

When you add the data source, Nobl9 automatically generates a Kubernetes configuration and a Docker command line for you to use to deploy the agent. Both of these are available in the web UI, under the Agent Configuration section. Be sure to swap in your credentials.

If you use Kubernetes, you can apply the supplied YAML config file to a Kubernetes cluster to deploy the agent. It will look something like this:

# DISCLAIMER: This deployment description contains only the fields necessary for the purpose of this demo.
# It is not a ready-to-apply k8s deployment description, and the client_id and client_secret are only exemplary values.

apiVersion: v1
kind: Secret
metadata:
name: nobl9-agent-nobl9-dev-grafana-loki-name
namespace: default
type: Opaque
stringData:
client_id: "unique_client_id"
client_secret: "unique_client_secret"
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nobl9-agent-nobl9-dev-grafana-loki-lokiagent
namespace: default
spec:
replicas: 1
selector:
matchLabels:
nobl9-agent-name: "lokiagent"
nobl9-agent-project: "grafana-loki"
nobl9-agent-organization: "nobl9-dev"
template:
metadata:
labels:
nobl9-agent-name: "lokiagent"
nobl9-agent-project: "grafana-loki"
nobl9-agent-organization: "nobl9-dev"
spec:
containers:
- name: agent-container
image: nobl9/agent:latest
resources:
requests:
memory: "350Mi"
cpu: "0.1"
env:
- name: N9_CLIENT_ID
valueFrom:
secretKeyRef:
key: client_id
name: nobl9-agent-nobl9-dev-grafana-loki-lokiagent
- name: N9_CLIENT_SECRET
valueFrom:
secretKeyRef:
key: client_secret
name: nobl9-agent-nobl9-dev-grafana-loki-name
# The N9_METRICS_PORT is a variable specifying the port to which the /metrics and /health endpoints are exposed.
# The 9090 is the default value and can be changed.
# If you donโ€™t want the metrics to be exposed, comment out or delete the N9_METRICS_PORT variable.
- name: N9_METRICS_PORT
value: "9090"

Deploying Grafana Loki agent with basic_auth methodโ€‹

To activate basic_auth for the agent, you need to pass optional environmental variables to an agent:

  • AUTH_METHOD: basic_auth - is a fixed value, but it must be passed to let know agent that basic_auth will be used.

    • USERNAME: REDACTED - username for basic_auth.

    • PASSWORD: REDACTED - password for basic_auth.

If you use Kubernetes, the supplied YAML config file to a Kubernetes cluster to deploy the agent using basic_auth method. It will look something like this:

# DISCLAIMER: This deployment description contains only the fields necessary for the purpose of this demo.
# It is not a ready-to-apply k8s deployment description, and the client_id and client_secret are only exemplary values.

apiVersion: v1
kind: Secret
metadata:
name: nobl9-agent-nobl9-dev-stable-grafana-loki
namespace: default
type: Opaque
stringData:
client_id: "REDACTED"
client_secret: "REDACTED"
basic_auth_username: "REDACTED"
basic_auth_password: "REDACTED"
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nobl9-agent-nobl9-dev-stable-grafana-loki
namespace: default
spec:
replicas: 1
selector:
matchLabels:
nobl9-agent-name: "grafana-loki"
nobl9-agent-project: "grafana-loki"
nobl9-agent-organization: "nobl9-dev-stable"
template:
metadata:
labels:
nobl9-agent-name: "grafana-loki"
nobl9-agent-project: "grafana-loki"
nobl9-agent-organization: "nobl9-dev-stable"
spec:
containers:
- name: agent-container
image: nobl9/agent:latest
resources:
requests:
memory: "350Mi"
cpu: "0.1"
env:
- name: N9_CLIENT_ID
valueFrom:
secretKeyRef:
key: client_id
name: nobl9-agent-nobl9-dev-stable-grafana-loki
- name: N9_CLIENT_SECRET
valueFrom:
secretKeyRef:
key: client_secret
name: nobl9-agent-nobl9-dev-stable-grafana-loki
- name: AUTH_METHOD
value: "basic_auth"
- name: USERNAME
valueFrom:
secretKeyRef:
key: basic_auth_username
name: nobl9-agent-nobl9-dev-grafana-loki-with-basic-auth
- name: PASSWORD
valueFrom:
secretKeyRef:
key: basic_auth_password
name: nobl9-agent-nobl9-dev-grafana-loki-with-basic-auth
# The N9_METRICS_PORT is a variable specifying the port to which the /metrics and /health endpoints are exposed.
# The 9090 is the default value and can be changed.
# If you donโ€™t want the metrics to be exposed, comment out or delete the N9_METRICS_PORT variable.
- name: N9_METRICS_PORT
value: "9090"

Creating SLOs with Grafana Lokiโ€‹

Creating SLOs in the UIโ€‹

Follow the instructions below to create your SLOs with Grafana Loki in the UI:

  1. Navigate to Service Level Objectives.

  2. Click .
  3. In step 1 of the SLO wizard, select the Service the SLO will be associated with.

  4. In step 2, select Grafana Loki as the data source for your SLO.

  5. Specify the Metric. You can choose either a Threshold Metric, where a single time series is evaluated against a threshold or a Ratio Metric, which allows you to enter two time series to compare (for example, a count of good requests and total requests).

    1. Choose the Data Count Method for your ratio metric:
    • Non-incremental: counts incoming metric values one-by-one. So the resulting SLO graph is pike-shaped.
    • Incremental: counts the incoming metric values incrementally, adding every next value to previous values. It results in a constantly increasing SLO graph.
  6. Enter a Query, or Good Query and Total Query for the metric you selected.
    Refer to the Query Examples section below for more details.

  7. In step 3, define a Time Window for the SLO.

  8. In step 4, specify the Error Budget Calculation Method and your Objective(s).

  9. In step 5, add a Name, Description, and other details about your SLO. You can also select Alert policies and Labels on this screen.

  10. When youโ€™re done, click Create SLO.

SLOs using Grafana Loki - YAML samplesโ€‹

Hereโ€™s an example of Grafana Loki using a rawMetric (threshold metric):

apiVersion: n9/v1alpha
kind: SLO
metadata:
name: n9-kafka-main-cluster-alerts-error-budgets-out-lag-threshold
project: grafana-loki
spec:
description: Example of Loki Metric query
service: grafana-loki-service
indicator:
metricSource:
name: grafana-loki
timeWindows:
- unit: Day
count: 1
isRolling: true
budgetingMethod: Occurrences
objectives:
- displayName: Good
op: lte
rawMetric:
query:
grafanaLoki:
logql: sum(sum_over_time({topic="error-budgets-out", consumergroup="alerts", cluster="main"} |= "kafka_consumergroup_lag" | logfmt | line_format "{{.kafka_consumergroup_lag}}" | unwrap kafka_consumergroup_lag [1m]))
value: 5
target: 0.50
- displayName: Moderate
op: lte
rawMetric:
query:
grafanaLoki:
logql: sum(sum_over_time({topic="error-budgets-out", consumergroup="alerts", cluster="main"} |= "kafka_consumergroup_lag" | logfmt | line_format "{{.kafka_consumergroup_lag}}" | unwrap kafka_consumergroup_lag [1m]))
value: 10
target: 0.75

Metrics for Grafana Loki have one mandatory field:

Query examplesโ€‹

  • Ratio metric for Grafana Loki:
    Good Query:
    count(count_over_time(({app="nobl9", component="ingest", container="ingest container"} | json | line_format "{{.log}}" | json | http_useragent != "ELB-HealthChecker/2.0" | http_status_code >= 200 and http_status_code < 300)[1m]))
    Total Query:
    count(count_over_time(({app="nobl9", component="ingest", container="ingest-container"} | json | line_format "{{.log}}" | json | http_useragent != "ELB-HealthChecker/2.0" | http_status_code > 0)[1m]))

Querying the Grafana Loki serverโ€‹

Nobl9 calls Loki API every minute to retrieve the log data from the previous minute. Nobl9 aggregates the total number of points to 4 per minute.

caution

Users should refrain from adding duration and Nobl9 will append [1m] to the query.

Grafana HTTP API | Grafana Loki documentation

Introduction to PromQL | Grafana documentation

Agent metrics

Creating SLOs via Terraform

Creating agents via Terraform