Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add number of projects and times spent per project metrics. #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import com.atlassian.instrumentation.Instrument;
import com.atlassian.instrumentation.InstrumentRegistry;
import com.atlassian.jira.cluster.ClusterManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.license.LicenseCountService;
import com.atlassian.jira.project.ProjectManager;
import com.atlassian.jira.user.util.UserManager;
import com.atlassian.jira.web.session.currentusers.JiraUserSession;
import com.atlassian.jira.web.session.currentusers.JiraUserSessionTracker;
Expand All @@ -16,6 +18,7 @@
import io.prometheus.client.Gauge;
import io.prometheus.client.Histogram;
import io.prometheus.client.hotspot.DefaultExports;
import org.ofbiz.core.entity.GenericEntityException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
Expand All @@ -28,8 +31,8 @@
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static com.atlassian.jira.instrumentation.InstrumentationName.CONCURRENT_REQUESTS;
import static com.atlassian.jira.instrumentation.InstrumentationName.DBCP_ACTIVE;
import static com.atlassian.jira.instrumentation.InstrumentationName.DBCP_IDLE;
Expand Down Expand Up @@ -60,6 +63,7 @@ public class MetricCollectorImpl extends Collector implements MetricCollector, D
private final ScheduledMetricEvaluator scheduledMetricEvaluator;
private final CollectorRegistry registry;
private final InstrumentRegistry instrumentRegistry;
private final ProjectManager projectManager;

public MetricCollectorImpl(
IssueManager issueManager,
Expand All @@ -68,7 +72,8 @@ public MetricCollectorImpl(
LicenseCountService licenseCountService,
ApplicationManager jiraApplicationManager,
ScheduledMetricEvaluator scheduledMetricEvaluator,
InstrumentRegistry instrumentRegistry) {
InstrumentRegistry instrumentRegistry,
ProjectManager projectManager) {
this.issueManager = issueManager;
this.jiraUserSessionTracker = JiraUserSessionTracker.getInstance();
this.clusterManager = clusterManager;
Expand All @@ -78,6 +83,7 @@ public MetricCollectorImpl(
this.scheduledMetricEvaluator = scheduledMetricEvaluator;
this.registry = CollectorRegistry.defaultRegistry;
this.instrumentRegistry = instrumentRegistry;
this.projectManager = projectManager;
}

private final Gauge maintenanceExpiryDaysGauge = Gauge.build()
Expand Down Expand Up @@ -226,6 +232,17 @@ public MetricCollectorImpl(
.help("JVM Uptime Gauge")
.create();

private final Gauge projectsGauge = Gauge.build()
.name("jira_projects_gauge")
.help("Number Of Projects Gauge")
.create();

private final Gauge timeSpentPerProjectGauge = Gauge.build()
.name("time_spent_per_project_seconds_gauge")
.help("Time Spent Per Project In Seconds")
.labelNames("project")
.create();

@Override
public void requestDuration(String path, ExceptionRunnable runnable) throws IOException, ServletException {
Histogram.Timer level1Timer = isNotBlank(path) ? requestDurationOnPath.labels(path).startTimer() : null;
Expand Down Expand Up @@ -317,6 +334,25 @@ private List<MetricFamilySamples> collectInternal() {
// jvm uptime
jvmUptimeGauge.set(ManagementFactory.getRuntimeMXBean().getUptime());

// number of projects
projectsGauge.set(projectManager.getProjectCount());

// calculate time spent per project
projectManager.getProjects().forEach(p -> {
try {
// get all issues for the current project and sum the times spent per issue
Collection<Long> issueIdsForProject = issueManager.getIssueIdsForProject(p.getId());
long totalTimeSpentPerProject = issueManager.getIssueObjects(issueIdsForProject)
.stream()
.filter(i -> i.getTimeSpent() != null)
.mapToLong(Issue::getTimeSpent)
.sum();
timeSpentPerProjectGauge.labels(p.getName()).set(totalTimeSpentPerProject);
} catch (GenericEntityException e) {
log.error("GenericEntityException caught. Detailed message: ", e);
}
});

// collect all metrics
List<MetricFamilySamples> result = new ArrayList<>();
result.addAll(issueUpdateCounter.collect());
Expand Down Expand Up @@ -347,6 +383,8 @@ private List<MetricFamilySamples> collectInternal() {
result.addAll(concurrentRequestsGauge.collect());
result.addAll(httpSessionObjectsGauge.collect());
result.addAll(jvmUptimeGauge.collect());
result.addAll(projectsGauge.collect());
result.addAll(timeSpentPerProjectGauge.collect());

return result;
}
Expand Down