summaryrefslogtreecommitdiff
path: root/jstests/libs/telemetry_utils.js
blob: 11e2d236827e198eac1cf48a0fbc32ea2d689e92 (plain)
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
/**
 * Utility for checking that the aggregated telemetry metrics are logical (follows sum >= max >=
 * min, and sum = max = min if only one execution).
 */
function verifyMetrics(batch) {
    batch.forEach(element => {
        if (element.metrics.execCount === 1) {
            for (const [metricName, summaryValues] of Object.entries(element.metrics)) {
                // Skip over fields that aren't aggregated metrics with sum/min/max (execCount,
                // lastExecutionMicros).
                if (summaryValues.sum === undefined) {
                    continue;
                }
                const debugInfo = {[metricName]: summaryValues};
                // If there has only been one execution, all metrics should have min, max, and sum
                // equal to each other.
                assert.eq(summaryValues.sum, summaryValues.min, debugInfo);
                assert.eq(summaryValues.sum, summaryValues.max, debugInfo);
                assert.eq(summaryValues.min, summaryValues.max, debugInfo);
            }
        } else {
            for (const [metricName, summaryValues] of Object.entries(element.metrics)) {
                // Skip over fields that aren't aggregated metrics with sum/min/max (execCount,
                // lastExecutionMicros).
                if (summaryValues.sum === undefined) {
                    continue;
                }
                const debugInfo = {[metricName]: summaryValues};
                assert.gte(summaryValues.sum, summaryValues.min, debugInfo);
                assert.gte(summaryValues.sum, summaryValues.max, debugInfo);
                assert.lte(summaryValues.min, summaryValues.max, debugInfo);
            }
        }
    });
}

/**
 *
 * Collect telemetry from a given collection. Only include query shapes generated by the shell that
 * is running tests.
 *
 */
function getTelemetry(conn) {
    const kApplicationName = "MongoDB Shell";
    const result = conn.adminCommand({
        aggregate: 1,
        pipeline: [
            {$telemetry: {}},
            // Sort on telemetry key so entries are in a deterministic order.
            {$sort: {key: 1}},
            {$match: {"key.applicationName": kApplicationName}}
        ],
        cursor: {}
    });
    assert.commandWorked(result);
    return result.cursor.firstBatch;
}

function getTelemetryRedacted(
    conn,
    applyHmacToIdentifiers = true,
    hmacKey = BinData(0, "MjM0NTY3ODkxMDExMTIxMzE0MTUxNjE3MTgxOTIwMjE=")) {
    // Hashed application name is generated using the default hmacKey argument.
    const kApplicationName = "MongoDB Shell";
    // Filter out agg queries, including $telemetry.
    const match = {
        $match: {"key.queryShape.command": "find", "key.applicationName": kApplicationName}
    };

    const result = conn.adminCommand({
        aggregate: 1,
        pipeline: [
            {$telemetry: {applyHmacToIdentifiers: applyHmacToIdentifiers, hmacKey: hmacKey}},
            match,
            // Sort on telemetry key so entries are in a deterministic order.
            {$sort: {key: 1}},
        ],
        cursor: {}
    });
    assert.commandWorked(result);
    return result.cursor.firstBatch;
}