summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/telemetry_collect_on_mongos.js
blob: 51546adc0de6ed5e1d5b004afc5c1929c5ad19a5 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
 * Test that mongos is collecting metrics.
 */

(function() {
"use strict";

const st = new ShardingTest({
    mongos: 1,
    shards: 1,
    config: 1,
    rs: {nodes: 1},
    mongosOptions: {
        setParameter: {
            internalQueryConfigureTelemetrySamplingRate: 2147483647,
            featureFlagTelemetry: true,
        }
    },
});

// Redacted literal replacement string. This may change in the future, so it's factored out.
const R = "###";

const mongos = st.s;
const db = mongos.getDB("test");
const coll = db.coll;
coll.insert({v: 1});
coll.insert({v: 4});

// Get the telemetry for a given database, filtering out the actual $telemetry call.
const getTelemetry = (conn) => {
    const result = conn.adminCommand({
        aggregate: 1,
        pipeline: [
            {$telemetry: {}},
            // Sort on telemetry key so entries are in a deterministic order.
            {$sort: {key: 1}},
        ],
        cursor: {}
    });
    return result.cursor.firstBatch;
};

/**
 * Verify that mongos is recording these metrics:
 * - "firstSeenTimestamp"
 * - "lastExecutionMicros"
 * - "execCount"
 * - "queryExecMicros"
 * - "docsReturned"
 */

// This test can't predict exact timings, so just assert these three fields have been set (are
// non-zero).
const assertTelemetryMetricsSet = (metrics) => {
    const {firstSeenTimestamp, lastExecutionMicros, queryExecMicros} = metrics;

    assert.neq(timestampCmp(firstSeenTimestamp, Timestamp(0, 0)), 0);
    assert.neq(lastExecutionMicros, NumberLong(0));

    const distributionFields = ['sum', 'max', 'min', 'sumOfSquares'];
    for (const field of distributionFields) {
        assert.neq(queryExecMicros[field], NumberLong(0));
    }
};

coll.find({v: {$gt: 0, $lt: 5}}).toArray();
coll.find({v: {$gt: 2, $lt: 3}}).toArray();
coll.find({v: {$gt: 0, $lt: 1}}).toArray();
coll.find({v: {$gt: 0, $lt: 2}}).toArray();

{
    const telemetry = getTelemetry(db);
    assert.eq(1, telemetry.length);
    const {key, metrics} = telemetry[0];
    const {docsReturned, execCount} = metrics;
    assert.eq(4, execCount);
    assert.eq(
        {
            find: {
                find: R,
                filter: {v: {$gt: R, $lt: R}},
                readConcern: {level: R, provenance: R},
            }
        },
        key,
    );
    assert.eq({
        sum: NumberLong(3),
        max: NumberLong(2),
        min: NumberLong(0),
        sumOfSquares: NumberLong(5),
    },
              docsReturned);
    assertTelemetryMetricsSet(metrics);
}

coll.aggregate([
    {$match: {v: {$gt: 0, $lt: 5}}},
    {$project: {hello: "$world"}},
]);
coll.aggregate([
    {$match: {v: {$gt: 0, $lt: 5}}},
    {$project: {hello: "$world"}},
]);
coll.aggregate([
    {$match: {v: {$gt: 2, $lt: 3}}},
    {$project: {hello: "$universe"}},
]);
coll.aggregate([
    {$match: {v: {$gt: 0, $lt: 2}}},
    {$project: {hello: "$galaxy"}},
]);

{
    const telemetry = getTelemetry(mongos.getDB("test"));
    assert.eq(3, telemetry.length);  // The $telemetry query for the last test is included in this
                                     // call to $telemetry.
    const {key, metrics} = telemetry[1];
    const {docsReturned, execCount} = metrics;
    assert.eq(4, execCount);
    assert.eq({
        sum: NumberLong(5),
        max: NumberLong(2),
        min: NumberLong(0),
        sumOfSquares: NumberLong(9),
    },
              docsReturned);
    assert.eq({
        pipeline: [
            {$match: {v: {$gt: R, $lt: R}}},
            {$project: {hello: R}},
        ],
        namespace: "test.coll",
        applicationName: "MongoDB Shell"
    },
              key);
    assertTelemetryMetricsSet(metrics);
}

st.stop();
}());