Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
section_purge
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
section_purge
Commits
7c96a58d
Commit
7c96a58d
authored
1 year ago
by
Ming Quah
Committed by
Ming Quah
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3362303
by FleetAdmiralButter: Add monitoring sensor for Section
parent
1f76cf75
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/Plugin/monitoring/SensorPlugin/SectionPurgerSensorPlugin.php
+145
-0
145 additions, 0 deletions
...gin/monitoring/SensorPlugin/SectionPurgerSensorPlugin.php
with
145 additions
and
0 deletions
src/Plugin/monitoring/SensorPlugin/SectionPurgerSensorPlugin.php
0 → 100644
+
145
−
0
View file @
7c96a58d
<?php
namespace
Drupal\section_purger\Plugin\monitoring\SensorPlugin
;
use
Drupal\key\KeyRepositoryInterface
;
use
Drupal\monitoring\Entity\SensorConfig
;
use
Drupal\monitoring\Result\SensorResultInterface
;
use
Drupal\monitoring\SensorPlugin\SensorPluginBase
;
use
Drupal\section_purger
\Entity\SectionPurgerSettings
;
use
GuzzleHttp\ClientInterface
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
use
Drupal\monitoring\SensorPlugin\SensorPluginInterface
;
/**
* Monitors the Section API connection.
*
* @SensorPlugin(
* id = "section_purger",
* label = @Translation("Section Purger Sensor"),
* description = @Translation("Monitors connectivity to the Section API"),
* )
*/
class
SectionPurgerSensorPlugin
extends
SensorPluginBase
implements
SensorPluginInterface
{
/**
* The client interface.
*
* @var \GuzzleHttp\ClientInterface
*/
protected
$client
;
/**
* The key repository.
*
* @var \Drupal\key\KeyRepository
*/
protected
$keyRepository
;
/**
* {@inheritdoc}
*/
public
function
__construct
(
SensorConfig
$sensor_config
,
$plugin_id
,
$plugin_definition
,
ClientInterface
$http_client
,
KeyRepositoryInterface
$key_repository
)
{
parent
::
__construct
(
$sensor_config
,
$plugin_id
,
$plugin_definition
);
$this
->
client
=
$http_client
;
$this
->
keyRepository
=
$key_repository
;
}
/**
* {@inheritdoc}
*/
public
static
function
create
(
ContainerInterface
$container
,
SensorConfig
$sensor_config
,
$plugin_id
,
$plugin_definition
)
{
return
new
static
(
$sensor_config
,
$plugin_id
,
$plugin_definition
,
$container
->
get
(
'http_client'
),
$container
->
get
(
'key.repository'
)
);
}
/**
* {@inheritdoc}
*/
public
function
getDefaultConfiguration
()
{
return
[
'caching_time'
=>
60
*
5
,
'value_type'
=>
'bool'
,
'category'
=>
'Tide'
,
'settings'
=>
[],
];
}
/**
* {@inheritdoc}
*/
public
function
calculateDependencies
()
{
return
[
'module'
=>
[
'section_purger'
],
];
}
/**
* Get the Section URI.
*
* @return string
* The section URI.
*/
protected
function
getUri
(
SectionPurgerSettings
$settings
)
{
return
sprintf
(
'%s://%s:%s/api/v1/account/%s/application/%s/environment/%s'
,
$settings
->
scheme
,
$settings
->
hostname
,
$settings
->
port
,
$settings
->
account
,
$settings
->
application
,
$settings
->
environmentname
);
}
/**
* Get request options.
*
* @param \Drupal\section_purger\Entity\SectionPurgerSettings $settings
* The purger settings.
*
* @param
*/
protected
function
getOptions
(
SectionPurgerSettings
$settings
)
{
$opt
=
[
'auth'
=>
[
$settings
->
username
,
$this
->
keyRepository
->
getKey
(
$settings
->
password
)
->
getKeyValue
()],
'connect_timeout'
=>
$settings
->
connect_timeout
,
'timeout'
=>
$settings
->
timeout
,
];
return
$opt
;
}
/**
* {@inheritdoc}
*/
public
function
runSensor
(
SensorResultInterface
$sensor_result
)
{
$purgers
=
SectionPurgerSettings
::
loadMultiple
();
$purger
=
reset
(
$purgers
);
if
(
empty
(
$purger
))
{
$sensor_result
->
setStatus
(
SensorResultInterface
::
STATUS_WARNING
);
$sensor_result
->
setMessage
(
'Section purger is not configured.'
);
return
;
}
$uri
=
$this
->
getUri
(
$purger
);
$opt
=
$this
->
getOptions
(
$purger
);
// Sensor interface catches raised exceptions - Guzzle will throw
// an exception when HTTP >= 400.
$this
->
client
->
get
(
$uri
,
$opt
);
$sensor_result
->
setStatus
(
SensorResultInterface
::
STATUS_OK
);
$sensor_result
->
setMessage
(
'Successfully connected to Section.io.'
);
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment