Skip to content

Commit

Permalink
mongoose plugin test
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-pytel committed Mar 26, 2021
1 parent 7c0a0bd commit 13cc5a1
Show file tree
Hide file tree
Showing 8 changed files with 10,793 additions and 41 deletions.
10,448 changes: 10,408 additions & 40 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@
"amqplib": "^0.7.0",
"axios": "^0.21.0",
"express": "^4.17.1",
"grpc-tools": "^1.10.0",
"grpc_tools_node_protoc_ts": "^4.0.0",
"grpc-tools": "^1.10.0",
"jest": "^26.6.3",
"mongodb": "^3.6.4",
"mongoose": "^5.12.2",
"mysql": "^2.18.1",
"pg": "^8.5.1",
"prettier": "^2.0.5",
Expand Down
40 changes: 40 additions & 0 deletions tests/plugins/mongoose/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*!
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

import * as http from 'http';
import agent from '../../../src';

process.env.SW_AGENT_LOGGING_LEVEL = 'ERROR';

agent.start({
serviceName: 'client',
maxBufferSize: 1000,
})

const server = http.createServer((req, res) => {
http
.request(`http://${process.env.SERVER || 'localhost:5000'}${req.url}`, (r) => {
let data = '';
r.on('data', (chunk) => (data += chunk));
r.on('end', () => res.end(data));
})
.end();
});

server.listen(5001, () => console.info('Listening on port 5001...'));
90 changes: 90 additions & 0 deletions tests/plugins/mongoose/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

version: "2.1"

services:
collector:
extends:
file: ../common/base-compose.yml
service: collector
networks:
- traveling-light

mongo:
container_name: mongo
environment:
MONGO_INITDB_ROOT_USERNAME: "root"
MONGO_INITDB_ROOT_PASSWORD: "root"
MONGO_INITDB_DATABASE: "admin"
ports:
- 27017:27017
volumes:
- ./init:/docker-entrypoint-initdb.d
healthcheck:
test: ["CMD", "bash", "-c", "cat < /dev/null > /dev/tcp/127.0.0.1/27017"]
interval: 5s
timeout: 60s
retries: 120
image: "mongo:latest"
networks:
- traveling-light

server:
extends:
file: ../common/base-compose.yml
service: agent
ports:
- 5000:5000
environment:
MONGO_HOST: mongo
volumes:
- .:/app/tests/plugins/pg
healthcheck:
test: ["CMD", "bash", "-c", "cat < /dev/null > /dev/tcp/127.0.0.1/5000"]
interval: 5s
timeout: 60s
retries: 120
entrypoint:
["bash", "-c", "npx ts-node /app/tests/plugins/pg/server.ts"]
depends_on:
collector:
condition: service_healthy
mongo:
condition: service_healthy

client:
extends:
file: ../common/base-compose.yml
service: agent
ports:
- 5001:5001
environment:
SERVER: server:5000
healthcheck:
test: ["CMD", "bash", "-c", "cat < /dev/null > /dev/tcp/127.0.0.1/5001"]
interval: 5s
timeout: 60s
retries: 120
entrypoint:
["bash", "-c", "npx ts-node /app/tests/plugins/pg/client.ts"]
depends_on:
server:
condition: service_healthy

networks:
traveling-light:
129 changes: 129 additions & 0 deletions tests/plugins/mongoose/expected.data.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

segmentItems:
- serviceName: server
segmentSize: 1
segments:
- segmentId: not null
spans:
- operationName: MongoDB/collection
operationId: 0
parentSpanId: 0
spanId: 1
spanLayer: Database
startTime: gt 0
endTime: gt 0
componentId: 9
spanType: Exit
peer: mongo:27017
skipAnalysis: false
tags:
- { key: db.type, value: MongoDB }
- { key: db.instance, value: admin }
- { key: db.statement, value: 'collection("tests")' }
- operationName: Mongoose/ensureIndexes
operationId: 0
parentSpanId: 0
spanId: 2
spanLayer: Database
startTime: gt 0
endTime: gt 0
componentId: 4006
spanType: Exit
peer: mongo:27017
skipAnalysis: false
tags:
- { key: db.type, value: MongoDB }
- { key: db.instance, value: admin }
- operationName: Mongoose/find
operationId: 0
parentSpanId: 0
spanId: 3
spanLayer: Database
startTime: gt 0
endTime: gt 0
componentId: 4006
spanType: Exit
peer: mongo:27017
skipAnalysis: false
tags:
- { key: db.type, value: MongoDB }
- { key: db.instance, value: admin }
- { key: db.statement, value: 'tests.find({})' }
- operationName: /mongoose
operationId: 0
parentSpanId: -1
spanId: 0
spanLayer: Http
startTime: gt 0
endTime: gt 0
componentId: 49
spanType: Entry
peer: not null
skipAnalysis: false
tags:
- { key: http.url, value: 'http://server:5000/mongoose' }
- { key: http.method, value: GET }
- { key: http.status.code, value: '200' }
- { key: http.status.msg, value: OK }
refs:
- parentEndpoint: ""
networkAddress: server:5000
refType: CrossProcess
parentSpanId: 1
parentTraceSegmentId: not null
parentServiceInstance: not null
parentService: client
traceId: not null
- serviceName: client
segmentSize: 1
segments:
- segmentId: not null
spans:
- operationName: /mongoose
operationId: 0
parentSpanId: -1
spanId: 0
spanLayer: Http
startTime: gt 0
endTime: gt 0
componentId: 49
spanType: Entry
peer: not null
skipAnalysis: false
tags:
- { key: http.url, value: 'http://localhost:5001/mongoose' }
- { key: http.method, value: GET }
- { key: http.status.code, value: '200' }
- { key: http.status.msg, value: OK }
- operationName: /mongoose
operationId: 0
parentSpanId: 0
spanId: 1
spanLayer: Http
startTime: gt 0
endTime: gt 0
componentId: 2
spanType: Exit
peer: server:5000
skipAnalysis: false
tags:
- { key: http.url, value: 'http://server:5000/mongoose' }
- { key: http.method, value: GET }
- { key: http.status.code, value: '200' }
- { key: http.status.msg, value: OK }
1 change: 1 addition & 0 deletions tests/plugins/mongoose/init/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
db.createCollection('docs');
66 changes: 66 additions & 0 deletions tests/plugins/mongoose/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*!
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

import * as http from 'http';
import mongoose from 'mongoose';
import agent from '../../../src';

process.env.SW_AGENT_LOGGING_LEVEL = 'ERROR';

agent.start({
serviceName: 'server',
maxBufferSize: 1000,
});

const server = http.createServer(async (req, res) => {
await new Promise((resolve, reject) => {
mongoose.connect(`mongodb://root:root@${process.env.MONGO_HOST}:27017/admin`, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,

}).then(() => {
const Test = new mongoose.Schema({
title: String
});

const modelTest = mongoose.model('Test', Test);

modelTest.find().then(
(result: any) => {
res.end(`${result}`);
resolve(null);
mongoose.connection.close();
},

(err: Error) => {
res.end(`${err}`);
resolve(null);
mongoose.connection.close();
},
);

}).catch((err: Error) => {
res.end(`${err}`);
resolve(null);
});
});
});

server.listen(5000, () => console.info('Listening on port 5000...'));
Loading

0 comments on commit 13cc5a1

Please sign in to comment.