Skip to content

Commit e044f74

Browse files
authored
Merge pull request #10136 from magento-gl/AC-15461
AC-15461:Migration New Relic from REST v2 to NerdGraph (GraphQL)
2 parents f6aa682 + ead2c8f commit e044f74

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+7605
-123
lines changed
Lines changed: 115 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
55
*/
66
namespace Magento\NewRelicReporting\Console\Command;
77

8+
use Magento\Framework\Exception\LocalizedException;
89
use Symfony\Component\Console\Command\Command;
910
use Symfony\Component\Console\Input\InputInterface;
1011
use Symfony\Component\Console\Output\OutputInterface;
1112
use Symfony\Component\Console\Input\InputArgument;
13+
use Symfony\Component\Console\Input\InputOption;
14+
use Symfony\Component\Console\Helper\Table;
15+
use Magento\NewRelicReporting\Model\Config;
1216
use Magento\NewRelicReporting\Model\Apm\DeploymentsFactory;
1317
use Magento\NewRelicReporting\Model\ServiceShellUser;
1418

@@ -17,27 +21,34 @@ class DeployMarker extends Command
1721
/**
1822
* @var DeploymentsFactory
1923
*/
20-
private $deploymentsFactory;
24+
private DeploymentsFactory $deploymentsFactory;
2125

2226
/**
2327
* @var ServiceShellUser
2428
*/
25-
private $serviceShellUser;
29+
private ServiceShellUser $serviceShellUser;
30+
/**
31+
* @var Config
32+
*/
33+
private Config $config;
2634

2735
/**
2836
* Initialize dependencies.
2937
*
3038
* @param DeploymentsFactory $deploymentsFactory
3139
* @param ServiceShellUser $serviceShellUser
32-
* @param ?string $name
40+
* @param Config $config
41+
* @param string|null $name
3342
*/
3443
public function __construct(
3544
DeploymentsFactory $deploymentsFactory,
3645
ServiceShellUser $serviceShellUser,
37-
$name = null
46+
Config $config,
47+
?string $name = null
3848
) {
3949
$this->deploymentsFactory = $deploymentsFactory;
4050
$this->serviceShellUser = $serviceShellUser;
51+
$this->config = $config;
4152
parent::__construct($name);
4253
}
4354

@@ -47,11 +58,11 @@ public function __construct(
4758
protected function configure()
4859
{
4960
$this->setName("newrelic:create:deploy-marker");
50-
$this->setDescription("Check the deploy queue for entries and create an appropriate deploy marker.")
61+
$this->setDescription("Create a deployment marker in New Relic (supports both v2 REST and NerdGraph)")
5162
->addArgument(
5263
'message',
5364
InputArgument::REQUIRED,
54-
'Deploy Message?'
65+
'Deploy Message / Description'
5566
)
5667
->addArgument(
5768
'change_log',
@@ -65,24 +76,110 @@ protected function configure()
6576
)->addArgument(
6677
'revision',
6778
InputArgument::OPTIONAL,
68-
'Revision'
79+
'Revision / Version'
80+
)
81+
->addOption(
82+
'commit',
83+
'c',
84+
InputOption::VALUE_OPTIONAL,
85+
'Git commit hash for this deployment (NerdGraph only)'
86+
)
87+
->addOption(
88+
'deep-link',
89+
'd',
90+
InputOption::VALUE_OPTIONAL,
91+
'Deep link to deployment details (NerdGraph only)'
92+
)
93+
->addOption(
94+
'group-id',
95+
'g',
96+
InputOption::VALUE_OPTIONAL,
97+
'Group ID for organizing deployments (NerdGraph only)'
6998
);
7099
parent::configure();
71100
}
72101

73102
/**
74103
* @inheritdoc
75104
*/
76-
protected function execute(InputInterface $input, OutputInterface $output)
105+
protected function execute(InputInterface $input, OutputInterface $output): int
106+
{
107+
$isEnabled = $this->config->isNewRelicEnabled();
108+
if (!$isEnabled) {
109+
$output->writeln('<error>✗ New Relic is not enabled. Please check your configuration.</error>');
110+
return Command::FAILURE;
111+
}
112+
try {
113+
$result = $this->deploymentsFactory->create()->setDeployment(
114+
$input->getArgument('message'),
115+
$input->getArgument('change_log') ?: false,
116+
$this->serviceShellUser->get($input->getArgument('user')) ?: false,
117+
$input->getArgument('revision'),
118+
$input->getOption('commit'),
119+
$input->getOption('deep-link'),
120+
$input->getOption('group-id')
121+
);
122+
123+
if ($result !== false) {
124+
$output->writeln('<info>✓ NewRelic deployment marker created successfully!</info>');
125+
126+
// Display enhanced details if available (from NerdGraph)
127+
if (is_array($result) && isset($result['deploymentId'])) {
128+
$this->displayDeploymentDetails($output, $result);
129+
}
130+
131+
return Command::SUCCESS;
132+
} else {
133+
$output->writeln('<error>✗ Failed to create deployment marker</error>');
134+
return Command::FAILURE;
135+
}
136+
} catch (\Exception $e) {
137+
$output->writeln('<error>✗ Error: ' . $e->getMessage() . '</error>');
138+
return Command::FAILURE;
139+
}
140+
}
141+
142+
/**
143+
* Display deployment details from NerdGraph response
144+
*
145+
* @param OutputInterface $output
146+
* @param array $deployment
147+
*/
148+
private function displayDeploymentDetails(OutputInterface $output, array $deployment): void
77149
{
78-
$this->deploymentsFactory->create()->setDeployment(
79-
$input->getArgument('message'),
80-
$input->getArgument('change_log'),
81-
$this->serviceShellUser->get($input->getArgument('user')),
82-
$input->getArgument('revision')
83-
);
84-
$output->writeln('<info>NewRelic deployment information sent</info>');
150+
$output->writeln('');
151+
$output->writeln('<comment>Deployment Details:</comment>');
152+
153+
$table = new Table($output);
154+
$table->setHeaders(['Field', 'Value']);
155+
156+
$rows = [
157+
['Deployment ID', $deployment['deploymentId'] ?? 'N/A'],
158+
['Entity GUID', $deployment['entityGuid'] ?? 'N/A'],
159+
['Version', $deployment['version'] ?? 'N/A'],
160+
['Description', $deployment['description'] ?? 'N/A'],
161+
['User', $deployment['user'] ?? 'N/A'],
162+
['Timestamp', $deployment['timestamp'] ?
163+
date(
164+
'Y-m-d H:i:s',
165+
(int)($deployment['timestamp'] / 1000)
166+
) : 'N/A']
167+
];
168+
169+
if (!empty($deployment['changelog'])) {
170+
$rows[] = ['Change log', $deployment['changelog']];
171+
}
172+
if (!empty($deployment['commit'])) {
173+
$rows[] = ['Commit', $deployment['commit']];
174+
}
175+
if (!empty($deployment['deepLink'])) {
176+
$rows[] = ['Deep Link', $deployment['deepLink']];
177+
}
178+
if (!empty($deployment['groupId'])) {
179+
$rows[] = ['Group ID', $deployment['groupId']];
180+
}
85181

86-
return 0;
182+
$table->setRows($rows);
183+
$table->render();
87184
}
88185
}

app/code/Magento/NewRelicReporting/Model/Apm/Deployments.php

Lines changed: 97 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
55
*/
66
namespace Magento\NewRelicReporting\Model\Apm;
77

88
use Laminas\Http\Exception\RuntimeException;
99
use Laminas\Http\Request;
10-
use Magento\Framework\HTTP\LaminasClient;
1110
use Magento\Framework\HTTP\LaminasClientFactory;
1211
use Magento\Framework\Serialize\SerializerInterface;
1312
use Magento\NewRelicReporting\Model\Config;
13+
use Magento\NewRelicReporting\Model\Config\Source\ApiMode;
14+
use Magento\NewRelicReporting\Model\NerdGraph\DeploymentTracker;
1415
use Psr\Log\LoggerInterface;
1516

1617
/**
@@ -36,12 +37,17 @@ class Deployments
3637
/**
3738
* @var LaminasClientFactory $clientFactory
3839
*/
39-
protected $clientFactory;
40+
protected LaminasClientFactory $clientFactory;
4041

4142
/**
4243
* @var SerializerInterface
4344
*/
44-
private $serializer;
45+
private SerializerInterface $serializer;
46+
47+
/**
48+
* @var DeploymentTracker
49+
*/
50+
private DeploymentTracker $deploymentTracker;
4551

4652
/**
4753
* Constructor
@@ -50,30 +56,75 @@ class Deployments
5056
* @param LoggerInterface $logger
5157
* @param LaminasClientFactory $clientFactory
5258
* @param SerializerInterface $serializer
59+
* @param DeploymentTracker $deploymentTracker
5360
*/
5461
public function __construct(
5562
Config $config,
5663
LoggerInterface $logger,
5764
LaminasClientFactory $clientFactory,
58-
SerializerInterface $serializer
65+
SerializerInterface $serializer,
66+
DeploymentTracker $deploymentTracker
5967
) {
6068
$this->config = $config;
6169
$this->logger = $logger;
6270
$this->clientFactory = $clientFactory;
6371
$this->serializer = $serializer;
72+
$this->deploymentTracker = $deploymentTracker;
6473
}
6574

6675
/**
6776
* Performs the request to make the deployment
6877
*
78+
* Supports both v2 REST and NerdGraph APIs based on configuration
79+
*
6980
* @param string $description
70-
* @param bool $change
71-
* @param bool $user
72-
* @param ?string $revision
81+
* @param string|null $changelog
82+
* @param string|null $user
83+
* @param string|null $revision
84+
* @param string|null $commit Git commit hash (NerdGraph only)
85+
* @param string|null $deepLink Deep link URL (NerdGraph only)
86+
* @param string|null $groupId Group ID (NerdGraph only)
87+
*
88+
* @return bool|string|array
89+
*/
90+
public function setDeployment(
91+
string $description,
92+
?string $changelog = null,
93+
?string $user = null,
94+
?string $revision = null,
95+
?string $commit = null,
96+
?string $deepLink = null,
97+
?string $groupId = null
98+
): bool|array|string {
99+
// Check API mode configuration
100+
$apiMode = $this->config->getApiMode();
101+
102+
if ($apiMode === ApiMode::MODE_NERDGRAPH) {
103+
return $this->setNerdGraphDeployment(
104+
$description,
105+
$changelog,
106+
$user,
107+
$revision,
108+
$commit,
109+
$deepLink,
110+
$groupId
111+
);
112+
} else {
113+
return $this->setV2RestDeployment($description, $changelog, $user, $revision);
114+
}
115+
}
116+
117+
/**
118+
* Create deployment using v2 REST API (legacy)
73119
*
120+
* @param string $description
121+
* @param bool|string $changelog
122+
* @param bool|string $user
123+
* @param string|null $revision
74124
* @return bool|string
75125
*/
76-
public function setDeployment($description, $change = false, $user = false, $revision = null)
126+
private function setV2RestDeployment(string $description, bool|string $changelog, bool|string $user, ?string
127+
$revision): bool|string
77128
{
78129
$apiUrl = $this->config->getNewRelicApiUrl();
79130
if (empty($apiUrl)) {
@@ -83,7 +134,6 @@ public function setDeployment($description, $change = false, $user = false, $rev
83134

84135
$apiUrl = sprintf($apiUrl, $this->config->getNewRelicAppId());
85136

86-
/** @var LaminasClient $client */
87137
$client = $this->clientFactory->create();
88138
$client->setUri($apiUrl);
89139
$client->setMethod(Request::METHOD_POST);
@@ -101,7 +151,7 @@ public function setDeployment($description, $change = false, $user = false, $rev
101151
$params = [
102152
'deployment' => [
103153
'description' => $description,
104-
'changelog' => $change,
154+
'changelog' => $changelog,
105155
'user' => $user,
106156
'revision' => $revision
107157
]
@@ -116,10 +166,44 @@ public function setDeployment($description, $change = false, $user = false, $rev
116166
}
117167

118168
if ($response->getStatusCode() < 200 || $response->getStatusCode() > 210) {
119-
$this->logger->warning('Deployment marker request did not send a 200 status code.');
169+
$this->logger->warning(
170+
'Deployment marker request did not send a 200 status code.',
171+
[
172+
'status_code' => $response->getStatusCode(),
173+
'response_body' => $response->getBody(),
174+
'request_url' => $apiUrl,
175+
'request_params' => $params
176+
]
177+
);
120178
return false;
121179
}
122180

123181
return $response->getBody();
124182
}
183+
184+
/**
185+
* Create deployment using NerdGraph (GraphQL) API
186+
*
187+
* @param string $description
188+
* @param string|null $changelog
189+
* @param string|null $user
190+
* @param string|null $revision
191+
* @param string|null $commit
192+
* @param string|null $deepLink
193+
* @param string|null $groupId
194+
* @return array|false
195+
*/
196+
private function setNerdGraphDeployment(string $description, ?string $changelog, ?string $user, ?string
197+
$revision, ?string $commit, ?string $deepLink, ?string $groupId): false|array
198+
{
199+
return $this->deploymentTracker->setDeployment(
200+
$description,
201+
$changelog ? (string)$changelog : null,
202+
$user ? (string)$user : null,
203+
$revision,
204+
$commit,
205+
$deepLink,
206+
$groupId
207+
);
208+
}
125209
}

0 commit comments

Comments
 (0)