Exemple #1
0
 def test_gaugehistogram_labels(self):
     cmf = GaugeHistogramMetricFamily('h', 'help', labels=['a'])
     cmf.add_metric(['b'], buckets=[('0', 1), ('+Inf', 2)], gsum_value=3)
     self.custom_collector(cmf)
     self.assertEqual(1, self.registry.get_sample_value('h_bucket', {'a': 'b', 'le': '0'}))
     self.assertEqual(2, self.registry.get_sample_value('h_bucket', {'a': 'b', 'le': '+Inf'}))
     self.assertEqual(2, self.registry.get_sample_value('h_gcount', {'a': 'b'}))
     self.assertEqual(3, self.registry.get_sample_value('h_gsum', {'a': 'b'}))
    def collect(self):
        """Yields metrics from the collectors in the registry."""

        __data = self.__build_sentry_data()
        __metadata = __data.get("metadata")
        __projects_data = __data.get("projects_data")

        self.org = __metadata.get("org")
        self.projects_data = {}

        if self.issue_metrics == "True":
            issues_histogram_metrics = GaugeHistogramMetricFamily(
                "sentry_issues",
                "Number of open issues (aka is:unresolved) per project",
                buckets=None,
                gsum_value=None,
                labels=[
                    "project_slug",
                    "environment",
                ],
                unit="",
            )

            log.info("collector: loading projects issues")
            for project in __metadata.get("projects"):
                envs = __metadata.get("projects_envs").get(project.get("slug"))
                project_issues = __projects_data.get(project.get("slug"))
                for env in envs:
                    log.debug(
                        "collector: loading issues - project: {proj} env: {env}".format(
                            proj=project.get("slug"), env=env
                        )
                    )

                    project_issues_1h = project_issues.get(env).get("1h")
                    project_issues_24h = project_issues.get(env).get("24h")
                    project_issues_14d = project_issues.get(env).get("14d")

                    events_1h = 0
                    events_24h = 0
                    events_14d = 0

                    if project_issues_1h:
                        for issue in project_issues_1h:
                            events_1h += int(issue.get("count") or 0)

                    if project_issues_24h:
                        for issue in project_issues_24h:
                            events_24h += int(issue.get("count") or 0)

                    if project_issues_14d:
                        for issue in project_issues_14d:
                            events_14d += int(issue.get("count") or 0)

                    sum_events = events_1h + events_24h + events_14d
                    histo_buckets = []
                    if self.get_1h_metrics == "True":
                        histo_buckets.append(("1h", float(events_1h)))
                    if self.get_24h_metrics == "True":
                        histo_buckets.append(("24h", float(events_24h)))
                    if self.get_14d_metrics == "True":
                        histo_buckets.append(("+Inf", float(events_14d)))
                    issues_histogram_metrics.add_metric(
                        labels=[
                            str(project.get("slug")),
                            str(env),
                        ],
                        buckets=histo_buckets,
                        gsum_value=int(sum_events),
                    )

            yield issues_histogram_metrics

            issues_metrics = GaugeMetricFamily(
                "sentry_open_issue_events",
                "Number of open issues (aka is:unresolved) per project",
                labels=[
                    "issue_id",
                    "logger",
                    "level",
                    "status",
                    "platform",
                    "project_slug",
                    "environment",
                    "release",
                    "isUnhandled",
                    "firstSeen",
                    "lastSeen",
                ],
            )

            for project in __metadata.get("projects"):
                envs = __metadata.get("projects_envs").get(project.get("slug"))
                project_issues = __projects_data.get(project.get("slug"))
                for env in envs:
                    project_issues_1h = project_issues.get(env).get("1h")
                    for issue in project_issues_1h:
                        release = self.__sentry_api.issue_release(issue.get("id"), env)
                        issues_metrics.add_metric(
                            [
                                str(issue.get("id")),
                                str(issue.get("logger")) or "None",
                                str(issue.get("level")),
                                str(issue.get("status")),
                                str(issue.get("platform")),
                                str(issue.get("project").get("slug")),
                                str(env),
                                str(release),
                                str(issue.get("isUnhandled")),
                                str(
                                    datetime.strftime(
                                        datetime.strptime(
                                            str(
                                                issue.get("firstSeen")
                                                # if the issue age is recent, firstSeen returns None
                                                # and we'll return datetime.now() as default
                                                or datetime.strftime(
                                                    datetime.now(), "%Y-%m-%dT%H:%M:%SZ"
                                                )
                                            ),
                                            "%Y-%m-%dT%H:%M:%SZ",
                                        ),
                                        "%Y-%m-%d",
                                    )
                                ),
                                str(
                                    datetime.strftime(
                                        datetime.strptime(
                                            str(
                                                issue.get("lastSeen")
                                                # if the issue age is recent, lastSeen returns None
                                                # and we'll return datetime.now() as default
                                                or datetime.strftime(
                                                    datetime.now(), "%Y-%m-%dT%H:%M:%SZ"
                                                )
                                            ),
                                            "%Y-%m-%dT%H:%M:%SZ",
                                        ),
                                        "%Y-%m-%d",
                                    )
                                ),
                            ],
                            int(issue.get("count")),
                        )
            yield issues_metrics

        if self.events_metrics == "True":
            project_events_metrics = CounterMetricFamily(
                "sentry_events",
                "Total events counts per project",
                labels=[
                    "project_slug",
                    "stat",
                ],
            )

            for project in __metadata.get("projects"):
                events = self.__sentry_api.project_stats(self.org.get("slug"), project.get("slug"))
                for stat, value in events.items():
                    project_events_metrics.add_metric(
                        [
                            str(project.get("slug")),
                            str(stat),
                        ],
                        int(value),
                    )

            yield project_events_metrics