Ejemplo n.º 1
0
def test_get_internal_team_id_returns_a_team_id_and_memoizes(
        db, django_assert_num_queries):
    team_id = get_internal_metrics_team_id()
    assert isinstance(team_id, int)

    with django_assert_num_queries(0):
        assert get_internal_metrics_team_id() == team_id

    team = Team.objects.get(pk=team_id)
    assert team.name == NAME
    assert team.organization.name == NAME
    assert team.organization.for_internal_metrics
Ejemplo n.º 2
0
def test_methods_capture_enabled(db, mock_capture_internal):
    timing("foo_metric", 100, tags={"team_id": 15})
    gauge("bar_metric", 20, tags={"team_id": 15})
    incr("zeta_metric")

    mock_capture_internal.assert_any_call(
        {
            "event": "$$foo_metric",
            "properties": {
                "value": 100,
                "team_id": 15
            }
        },
        "machine_id",
        None,
        None,
        mock.ANY,
        mock.ANY,
        get_internal_metrics_team_id(),
    )

    mock_capture_internal.assert_any_call(
        {
            "event": "$$bar_metric",
            "properties": {
                "value": 20,
                "team_id": 15
            }
        },
        "machine_id",
        None,
        None,
        mock.ANY,
        mock.ANY,
        get_internal_metrics_team_id(),
    )

    mock_capture_internal.assert_any_call(
        {
            "event": "$$zeta_metric",
            "properties": {
                "value": 1
            }
        },
        "machine_id",
        None,
        None,
        mock.ANY,
        mock.ANY,
        get_internal_metrics_team_id(),
    )
Ejemplo n.º 3
0
def _capture(metric_name: str, value: Any, tags: Tags):
    from posthog.api.capture import capture_internal

    team_id = get_internal_metrics_team_id()
    if team_id is not None:
        now = timezone.now()
        distinct_id = utils.get_machine_id()
        event = {"event": f"$${metric_name}", "properties": {"value": value, **(tags or {})}}
        capture_internal(event, distinct_id, None, None, now, now, team_id)
def test_get_internal_metrics_dashboards(db):
    info = get_internal_metrics_dashboards()

    team = Team.objects.get(pk=get_internal_metrics_team_id())
    dashboard = Dashboard.objects.get(pk=info["clickhouse"]["id"])

    assert Dashboard.objects.count() == 1
    assert dashboard.team_id == team.pk
    assert dashboard.name == CLICKHOUSE_DASHBOARD["name"]
    assert dashboard.items.count() == len(CLICKHOUSE_DASHBOARD["items"])

    assert get_internal_metrics_dashboards() == info
Ejemplo n.º 5
0
def _capture(metric_name: str, value: Any, tags: Tags):
    from posthog.api.capture import capture_internal

    try:
        team_id = get_internal_metrics_team_id()
        if team_id is not None:
            now = timezone.now()
            distinct_id = utils.get_machine_id()
            event = {
                "event": f"$${metric_name}",
                "properties": {
                    "value": value,
                    **(tags or {})
                }
            }
            capture_internal(event, distinct_id, None, None, now, now, team_id)
    except Exception as err:
        # Ignore errors, this is not important enough to fail API on
        capture_exception(err)
Ejemplo n.º 6
0
def test_get_internal_metrics_team_id_with_capture_disabled(
        db, django_assert_num_queries, mocker: MockerFixture):
    mocker.patch.object(settings, "CAPTURE_INTERNAL_METRICS", False)

    with django_assert_num_queries(0):
        assert get_internal_metrics_team_id() is None