Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
prometheus_exporter
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
prometheus_exporter
Merge requests
!4
Issue
#3205503
: Create a session count metric
Code
Review changes
Check out branch
Download
Patches
Plain diff
Open
Issue
#3205503
: Create a session count metric
issue/prometheus_exporter-3205503:3205503-create-a-session
into
8.x-1.x
Overview
0
Commits
1
Pipelines
0
Changes
1
Open
Marcus Deglos
requested to merge
issue/prometheus_exporter-3205503:3205503-create-a-session
into
8.x-1.x
4 years ago
Overview
0
Commits
1
Pipelines
0
Changes
1
Expand
0
0
Merge request reports
Compare
8.x-1.x
8.x-1.x (HEAD)
and
latest version
latest version
35d2c4e6
1 commit,
4 years ago
1 file
+
83
−
0
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
src/Plugin/MetricsCollector/SessionCountCollector.php
0 → 100644
+
83
−
0
Options
<?php
namespace
Drupal\prometheus_exporter\Plugin\MetricsCollector
;
use
Drupal\Core\Database\Connection
;
use
Drupal\Core\Plugin\ContainerFactoryPluginInterface
;
use
Drupal\prometheus_exporter
\Plugin\BaseMetricsCollector
;
use
PNX\Prometheus\Gauge
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
/**
* Collects metrics for the number of sessions.
*
* @MetricsCollector(
* id = "session_count",
* title = @Translation("Session count"),
* description = @Translation("Provides metrics for the number of sessions.")
* )
*/
class
SessionCountCollector
extends
BaseMetricsCollector
implements
ContainerFactoryPluginInterface
{
/**
* The PDO database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected
$connection
;
/**
* SessionCountCollector constructor.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Database\Connection $connection
* The PDO database connection.
*/
public
function
__construct
(
array
$configuration
,
$plugin_id
,
$plugin_definition
,
Connection
$connection
)
{
parent
::
__construct
(
$configuration
,
$plugin_id
,
$plugin_definition
);
$this
->
connection
=
$connection
;
}
/**
* {@inheritdoc}
*/
public
static
function
create
(
ContainerInterface
$container
,
array
$configuration
,
$plugin_id
,
$plugin_definition
)
{
return
new
static
(
$configuration
,
$plugin_id
,
$plugin_definition
,
$container
->
get
(
'database'
)
);
}
/**
* {@inheritdoc}
*/
public
function
collectMetrics
()
{
$gauge
=
new
Gauge
(
$this
->
getNamespace
(),
'total'
,
$this
->
getDescription
());
foreach
(
$this
->
query
()
as
$userType
=>
$count
)
{
$gauge
->
set
(
$count
,
[
'user'
=>
$userType
]);
}
$metrics
[]
=
$gauge
;
return
$metrics
;
}
/**
* Query the session table to retrieve the number of sessions.
*
* @return array
* Count of sessions for anonymous and authenticated users.
*/
protected
function
query
()
{
return
[
'anonymous'
=>
(
int
)
$this
->
connection
->
query
(
'SELECT COUNT(1) FROM sessions WHERE uid = :anonymous'
,
[
':anonymous'
=>
0
])
->
fetchField
(),
'authenticated'
=>
(
int
)
$this
->
connection
->
query
(
'SELECT COUNT(1) FROM sessions WHERE uid > :anonymous'
,
[
':anonymous'
=>
0
])
->
fetchField
(),
];
}
}
Loading