forked from jenkinsci/opentelemetry-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrafanaLogsBackendWithJenkinsVisualization.java
More file actions
254 lines (220 loc) · 9.92 KB
/
GrafanaLogsBackendWithJenkinsVisualization.java
File metadata and controls
254 lines (220 loc) · 9.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
* Copyright The Original Author or Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.jenkins.plugins.opentelemetry.backend.grafana;
import com.cloudbees.plugins.credentials.CredentialsMatchers;
import com.cloudbees.plugins.credentials.common.StandardListBoxModel;
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
import com.cloudbees.plugins.credentials.common.StandardUsernameListBoxModel;
import com.google.errorprone.annotations.MustBeClosed;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.model.Item;
import hudson.security.ACL;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import io.jenkins.plugins.opentelemetry.TemplateBindingsProvider;
import io.jenkins.plugins.opentelemetry.backend.GrafanaBackend;
import io.jenkins.plugins.opentelemetry.backend.ObservabilityBackend;
import io.jenkins.plugins.opentelemetry.jenkins.CredentialsNotFoundException;
import io.jenkins.plugins.opentelemetry.jenkins.HttpAuthHeaderFactory;
import io.jenkins.plugins.opentelemetry.job.log.LogStorageRetriever;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collections;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;
import jenkins.model.Jenkins;
import org.apache.commons.lang3.StringUtils;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.interceptor.RequirePOST;
public class GrafanaLogsBackendWithJenkinsVisualization extends GrafanaLogsBackend implements TemplateBindingsProvider {
private static final String MSG_LOKI_URL_IS_BLANK = "Loki URL is blank, logs will not be stored in Elasticsearch";
private String grafanaLokiDatasourceIdentifier = GrafanaBackend.DEFAULT_LOKI_DATA_SOURCE_IDENTIFIER;
private String lokiUrl;
private boolean disableSslVerifications;
private String lokiCredentialsId;
private String lokiTenantId;
@DataBoundConstructor
public GrafanaLogsBackendWithJenkinsVisualization() {}
public String getGrafanaLokiDatasourceIdentifier() {
return grafanaLokiDatasourceIdentifier;
}
@DataBoundSetter
public void setGrafanaLokiDatasourceIdentifier(String grafanaLokiDatasourceIdentifier) {
this.grafanaLokiDatasourceIdentifier = grafanaLokiDatasourceIdentifier;
}
@Override
@MustBeClosed
public LogStorageRetriever newLogStorageRetriever(TemplateBindingsProvider templateBindingsProvider) {
if (StringUtils.isBlank(lokiUrl)) {
throw new IllegalStateException(MSG_LOKI_URL_IS_BLANK);
}
String serviceName = templateBindingsProvider
.getBindings()
.get(ObservabilityBackend.TemplateBindings.SERVICE_NAME)
.toString();
Optional<String> serviceNamespace = Optional.ofNullable(templateBindingsProvider
.getBindings()
.get(ObservabilityBackend.TemplateBindings.SERVICE_NAMESPACE))
.map(Object::toString);
Optional<String> lokiTenantId = Optional.ofNullable(this.lokiTenantId).filter(StringUtils::isNotBlank);
return new LokiLogStorageRetriever(
lokiUrl,
disableSslVerifications,
HttpAuthHeaderFactory.createFactory(lokiCredentialsId),
lokiTenantId,
getBuildLogsVisualizationUrlTemplate(),
TemplateBindingsProvider.compose(templateBindingsProvider, this.getBindings()),
serviceName,
serviceNamespace);
}
public String getLokiUrl() {
return lokiUrl;
}
@DataBoundSetter
public void setLokiUrl(String lokiUrl) {
this.lokiUrl = lokiUrl;
}
public boolean isDisableSslVerifications() {
return disableSslVerifications;
}
@DataBoundSetter
public void setDisableSslVerifications(boolean disableSslVerifications) {
this.disableSslVerifications = disableSslVerifications;
}
public String getLokiTenantId() {
return lokiTenantId;
}
@DataBoundSetter
public void setLokiTenantId(String lokiTenantId) {
this.lokiTenantId = lokiTenantId;
}
public String getLokiCredentialsId() {
return lokiCredentialsId;
}
@DataBoundSetter
public void setLokiCredentialsId(String lokiCredentialsId) {
this.lokiCredentialsId = lokiCredentialsId;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
GrafanaLogsBackendWithJenkinsVisualization that = (GrafanaLogsBackendWithJenkinsVisualization) o;
return Objects.equals(grafanaLokiDatasourceIdentifier, that.grafanaLokiDatasourceIdentifier);
}
@Override
public int hashCode() {
return Objects.hash(grafanaLokiDatasourceIdentifier);
}
@Override
public String toString() {
return "GrafanaLogsBackendWithJenkinsVisualization{" + "grafanaLokiDatasourceIdentifier='"
+ grafanaLokiDatasourceIdentifier + '\'' + ", lokiUrl='"
+ lokiUrl + '\'' + ", disableSslVerifications="
+ disableSslVerifications + ", lokiCredentialsId='"
+ lokiCredentialsId + '\'' + '}';
}
@Override
public Map<String, Object> getBindings() {
return Map.of(
GrafanaBackend.TemplateBindings.GRAFANA_LOKI_DATASOURCE_IDENTIFIER,
getGrafanaLokiDatasourceIdentifier());
}
@Extension(ordinal = 50)
public static class DescriptorImpl extends GrafanaLogsBackend.DescriptorImpl {
@RequirePOST
public FormValidation doCheckLokiUrl(@QueryParameter("lokiUrl") String url) {
if (StringUtils.isEmpty(url)) {
return FormValidation.ok();
}
try {
new URI(url).toURL();
} catch (URISyntaxException | MalformedURLException | IllegalArgumentException e) {
return FormValidation.error("Invalid Loki URL: " + e.getMessage());
}
return FormValidation.ok();
}
@RequirePOST
public ListBoxModel doFillLokiCredentialsIdItems(Item context, @QueryParameter String lokiCredentialsId) {
if (context == null && !Jenkins.get().hasPermission(Jenkins.ADMINISTER)
|| context != null && !context.hasPermission(Item.CONFIGURE)) {
return new StandardListBoxModel();
}
Jenkins jenkins = Jenkins.get();
return new StandardUsernameListBoxModel()
.includeMatchingAs(
ACL.SYSTEM2,
jenkins,
StandardUsernameCredentials.class,
Collections.emptyList(),
CredentialsMatchers.instanceOf(StandardUsernameCredentials.class))
.includeCurrentValue(lokiCredentialsId);
}
@RequirePOST
public FormValidation doCheckLokiCredentialsId(Item context, @QueryParameter String lokiCredentialsId) {
Jenkins.get().checkPermission(Jenkins.ADMINISTER);
if (context == null && !Jenkins.get().hasPermission(Jenkins.ADMINISTER)
|| context != null && !context.hasPermission(Item.CONFIGURE)) {
return FormValidation.ok();
}
if (lokiCredentialsId == null || lokiCredentialsId.isEmpty()) {
return FormValidation.ok(); // support anonymous access
}
try {
new HttpAuthHeaderFactory(lokiCredentialsId).createAuthHeader();
} catch (CredentialsNotFoundException e) {
return FormValidation.error("Loki credentials are not valid: " + e.getMessage());
}
return FormValidation.ok();
}
@RequirePOST
public FormValidation doValidate(
@QueryParameter String lokiUrl,
@QueryParameter boolean disableSslVerifications,
@QueryParameter String lokiCredentialsId,
@QueryParameter String lokiTenantId) {
Jenkins.get().checkPermission(Jenkins.ADMINISTER);
FormValidation lokiUrlValidation = doCheckLokiUrl(lokiUrl);
if (lokiUrlValidation.kind != FormValidation.Kind.OK) {
return lokiUrlValidation;
}
try (LokiLogStorageRetriever lokiLogStorageRetriever = new LokiLogStorageRetriever(
lokiUrl,
disableSslVerifications,
HttpAuthHeaderFactory.createFactory(lokiCredentialsId),
Optional.ofNullable(lokiTenantId).filter(Predicate.not(String::isBlank)),
ObservabilityBackend.ERROR_TEMPLATE,
TemplateBindingsProvider.empty(),
"##not-needed-to-invoke-check-loki-setup##",
Optional.empty())) {
return FormValidation.aggregate(lokiLogStorageRetriever.checkLokiSetup());
} catch (NoSuchElementException e) {
return FormValidation.error("No credentials found for id '" + lokiCredentialsId + "'");
} catch (Exception e) {
return FormValidation.error(e, e.getMessage());
}
}
@NonNull
public String getDefaultLokiDataSourceIdentifier() {
return GrafanaBackend.DEFAULT_LOKI_DATA_SOURCE_IDENTIFIER;
}
@Override
public String getDefaultLokiOTelLogFormat() {
return LokiOTelLogFormat.LOKI_V2_JSON_OTEL_FORMAT.name();
}
@NonNull
@Override
public String getDisplayName() {
return "Store pipeline logs In Loki and visualize logs both in Grafana and through Jenkins ";
}
}
}