Ejemplo n.º 1
0
    def test_simple_gaugehistogram(self):
        families = text_string_to_metric_families("""# TYPE a gaugehistogram
# HELP a help
a_bucket{le="1.0"} 0
a_bucket{le="+Inf"} 3
a_gcount 3
a_gsum 2
# EOF
""")
        self.assertEqual([
            GaugeHistogramMetricFamily("a",
                                       "help",
                                       gsum_value=2,
                                       buckets=[("1.0", 0.0), ("+Inf", 3.0)])
        ], list(families))
Ejemplo n.º 2
0
    def test_gaugehistogram(self):
        self.custom_collector(
            GaugeHistogramMetricFamily('gh',
                                       'help',
                                       buckets=[('1.0', 4), ('+Inf', (5))],
                                       gsum_value=7))
        self.assertEqual(
            b"""# HELP gh help
# TYPE gh gaugehistogram
gh_bucket{le="1.0"} 4.0
gh_bucket{le="+Inf"} 5.0
gh_gcount 5.0
gh_gsum 7.0
# EOF
""", generate_latest(self.registry))
Ejemplo n.º 3
0
    def test_negative_bucket_gaugehistogram(self):
        families = text_string_to_metric_families("""# TYPE a gaugehistogram
# HELP a help
a_bucket{le="-1.0"} 1
a_bucket{le="1.0"} 2
a_bucket{le="+Inf"} 3
a_gcount 3
a_gsum -5
# EOF
""")
        self.assertEqual([
            GaugeHistogramMetricFamily("a",
                                       "help",
                                       gsum_value=-5,
                                       buckets=[("-1.0", 1.0), ("1.0", 2.0),
                                                ("+Inf", 3.0)])
        ], list(families))
Ejemplo n.º 4
0
 def test_gaugehistogram(self):
     self.custom_collector(GaugeHistogramMetricFamily('h', 'help', buckets=[('0', 1), ('+Inf', 2)]))
     self.assertEqual(1, self.registry.get_sample_value('h_bucket', {'le': '0'}))
     self.assertEqual(2, self.registry.get_sample_value('h_bucket', {'le': '+Inf'}))
    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