Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: enhancements to config parsing and dao #28

Merged
merged 7 commits into from
Mar 1, 2018
Merged

fix: enhancements to config parsing and dao #28

merged 7 commits into from
Mar 1, 2018

Conversation

darahayes
Copy link
Contributor

@darahayes darahayes commented Feb 27, 2018

This PR introduces a number of enhancements:

  • Client ID max length is now enforced at the business logic layer using metric.Validate(). This allows us to respond with a proper message when the max length has been exceeded. Ping @psturc.
  • the dao package now has some small amount of retry logic which will let the server retry connecting to the db for up to 5 seconds before failing. This solves a minor issue particularly in docker-compose where the server sometimes starts before the database is ready to accept connections. Ping @josemigallas.
  • Configuration parsing has been hardened and refactored. All connection options supported by the postgres driver are now supported and the connection string can be dynamically generated based off what config/env vars are present. This means we won't need to do any refactoring of the DB connection in the future.
  • The dao package now calls db.SetMaxOpenConns() with a default of 100 which is apparently the default allowed by Postgres. See docs.

Verification

Delete all containers previously run by docker-compose. You can get the container IDs with the following docker ps -a | grep aerogearappmetrics

Then run docker rm <id> for each id.

Then forcefully recreate your docker-compose environment with the following:

docker-compose up --build --force-recreate

You should notice 2 things:

  1. The metrics server did not crash while trying to connect to the database.

  2. Make the following curl request:

curl -X POST \
  http://localhost:3000/metrics \
  -H 'Cache-Control: no-cache' \
  -H 'Content-Type: application/json' \
  -H 'Postman-Token: 7011091f-b785-2c89-bfe8-55383df253f9' \
  -d '{
  "clientId": "453de743211112345678900987654312345678908776423567890876543678907654356789076543536789765436789076543256789076543256789654321456789654321456789765432567869765432567896543256789765432145678976543214567897654324567896543214567897654321245678976543256789765432145678965432567896543256786543214567865432",
  "data": {
    "app": {
      "id": "com.example.someApp",
      "sdkVersion": "2.4.6",
      "appVersion": "256"
    },
    "device": {
      "platform": "android",
      "platformVersion": "27"
    }
  }
}'

And you should see the following message:

{
    "error": "Bad Request",
    "message": "clientId exceeded maximum length of 128",
    "statusCode": 400
}

@darahayes darahayes changed the title Minor fixes to database implementation fix: enhancements to config parsing and dao implementation Feb 27, 2018
@darahayes darahayes changed the title fix: enhancements to config parsing and dao implementation fix: enhancements to config parsing and dao Feb 27, 2018
@psturc
Copy link
Contributor

psturc commented Feb 28, 2018

@darahayes I couldn't verify according to the steps provided.

I'm getting error when running the docker-compose command:

Step 4/5 : COPY ${BINARY} /opt/aerogear-app-metrics
ERROR: Service 'api' failed to build: COPY failed: stat /var/lib/docker/tmp/docker-builder721829778/dist/linux_amd64/aerogear-app-metrics: no such file or directory

@darahayes
Copy link
Contributor Author

My bad @psturc you must run make build_linux before running docker-compose

@psturc
Copy link
Contributor

psturc commented Feb 28, 2018

I'm not sure what I'm doing wrong - make build or make build_linux does not work for me.
However it's working fine in master branch.

@darahayes
Copy link
Contributor Author

@psturc This is strange. It seems to me by the error message that you do not have the latest code. It's building on my machine and in CircleCI.

Can you try pull this branch again and ensure you have the latest changes? the most recent commit has the message fix: enforce clientId max length at app layer

@psturc
Copy link
Contributor

psturc commented Feb 28, 2018

@darahayes yep, confirming I have the latest changes:

commit a948c9820c2993bbce6c2eecebdb0ee6b400625e (HEAD -> db-fix, upstream/db-fix)
Author: Dara Hayes <dara.hayes@redhat.com>
Date:   Tue Feb 27 23:15:29 2018 +0000

    fix: enforce clientId max length at app layer

This will be probably some stupid think I'm overlooking.

btw I'm using

Docker version 17.09.0-ce, build afdb6d4
go version go1.10 darwin/amd64

@coveralls
Copy link

coveralls commented Feb 28, 2018

Pull Request Test Coverage Report for Build #7

  • 54 of 56 (96.43%) changed or added relevant lines in 3 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+2.3%) to 88.732%

Changes Missing Coverage Covered Lines Changed/Added Lines %
pkg/dao/db.go 10 12 83.33%
Totals Coverage Status
Change from base Build #5: 2.3%
Covered Lines: 189
Relevant Lines: 213

💛 - Coveralls

@darahayes
Copy link
Contributor Author

@psturc I've merged master back into this branch can you try pulling again? If that doesn't work can you try deleting and fetching the branch again? Or perhaps cloning the repo again? I'm out of ideas beyond this 😅

@psturc
Copy link
Contributor

psturc commented Feb 28, 2018

@darahayes code lgtm, the clientId validation is fine, I think limit for 128 chars should be sufficient.

However I cannot verify this locally, I tried everything you've suggested above, but the build is still failing with the same error.

Could anybody else please try that too?

@darahayes
Copy link
Contributor Author

darahayes commented Feb 28, 2018

@psturc I think I figured out your issue: in the logs you sent me I saw the path ~/gocode/src/github.com/aerogear/aerogear-metrics-api

Please ensure you have the latest code cloned to the directory aerogear-app-metrics. I think your compiler is pulling in old code from a different directory

We changed the name of the service recently

@psturc
Copy link
Contributor

psturc commented Feb 28, 2018

@darahayes lol of course, nice catch Dara :) I will verify it locally soon then

@@ -28,11 +32,19 @@ type DeviceMetric struct {
PlatformVersion string `json:"platformVersion"`
}

const clientIdMaxLength = 128

var clientIdLengthError = fmt.Sprintf("clientId exceeded maximum length of %v", clientIdMaxLength)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@darahayes I'm missing context but is the clientId not a fixed length across the SDKs? Since you've pinged @psturc on the PR description probably you two are aware of why this is needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@paolobueno I have no idea what the clientId length is coming from Android/iOS. I just chose 128 as a sane default. If I can get more specific info that would be great!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's 36 on iOS side

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@psturc
Copy link
Contributor

psturc commented Feb 28, 2018

@darahayes I just noticed one thing, unfortunately unrelated to this PR.

There's a difference between API and client side in one property (appId vs id):
Android & iOS vs. metrics-api

I think appId property is more clear - so maybe we would need a change on metrics-api side, wdyt?

@darahayes
Copy link
Contributor Author

@psturc Great catch. Let's update the server and the original proposal.

@darahayes
Copy link
Contributor Author

@psturc I've updated the id property as suggested.

@psturc
Copy link
Contributor

psturc commented Feb 28, 2018

Ok, verified that appId chage

@darahayes
Copy link
Contributor Author

@psturc Did you manage to verify the other steps?

@psturc
Copy link
Contributor

psturc commented Feb 28, 2018

@darahayes yes, sorry I forgot to mention that it was all verified with iOS client

Copy link

@StevenTobin StevenTobin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified locally using verification instructions 👍

@darahayes darahayes merged commit 1478ca7 into master Mar 1, 2018
@darahayes darahayes deleted the db-fix branch March 1, 2018 10:25
david-martin pushed a commit to david-martin/aerogear-metrics-api that referenced this pull request Mar 12, 2018
* fix: use varchar(128) for clientId

* fix: basic connection retry logic

* fix(config): major improvements to config parser

* fix(dao) use new config and set max db connections

* fix: enforce clientId max length at app layer

* fix: rename data.app.id label to data.app.appId
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants