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

✨ Add customTags in logger middleware Config #2188

Merged
merged 6 commits into from
Nov 18, 2022

Conversation

Skyenought
Copy link
Member

@Skyenought Skyenought commented Nov 1, 2022

Description

Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change.
Explain the details for making this change. What existing problem does the pull request solve?

Fixes #2187

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist:

  • For new functionalities I follow the inspiration of the express js framework and built them similar in usage
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation - https://github.com/gofiber/docs for https://docs.gofiber.io/
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • If new dependencies exist, I have checked that they are really necessary and agreed with the maintainers/community (we want to have as few dependencies as possible)
  • I tried to make my code as fast as possible with as few allocations as possible
  • For new code I have written benchmarks so that they can be analyzed and improved

Commit formatting:

Use emojis on commit messages so it provides an easy way of identifying the purpose or intention of a commit. Check out the emoji cheatsheet here: https://gitmoji.carloscuesta.me/

@efectn
Copy link
Member

efectn commented Nov 1, 2022

Can you send here old and new benchmark comparision

@Skyenought
Copy link
Member Author

Can you send here old and new benchmark comparision

Seems to have slowed down :-(

old

goos: linux
goarch: amd64                                     
pkg: github.com/gofiber/fiber/v2/middleware/logger
cpu: Intel(R) Core(TM) i5-1035G1 CPU @ 1.00GHz
Benchmark_Logger
Benchmark_Logger-2       5582337               219.6 ns/op             0 B/op          0 allocs/op
Benchmark_Logger-2       5000182               237.4 ns/op             0 B/op          0 allocs/op
Benchmark_Logger-2       5331586               215.4 ns/op             0 B/op          0 allocs/op
Benchmark_Logger-2       5741889               209.6 ns/op             0 B/op          0 allocs/op
PASS
ok      github.com/gofiber/fiber/v2/middleware/logger   5.683s

PASS

new

goos: linux  
goarch: amd64
pkg: github.com/gofiber/fiber/v2/middleware/logger
cpu: Intel(R) Core(TM) i5-1035G1 CPU @ 1.00GHz
Benchmark_Logger
Benchmark_Logger-2       1649736               706.7 ns/op           320 B/op          7 allocs/op
Benchmark_Logger-2       1761847               732.1 ns/op           320 B/op          7 allocs/op
Benchmark_Logger-2       1704952               673.5 ns/op           320 B/op          7 allocs/op
Benchmark_Logger-2       1722056               674.2 ns/op           320 B/op          7 allocs/op
PASS
ok      github.com/gofiber/fiber/v2/middleware/logger   7.636s

PASS

@ReneWerner87
Copy link
Member

Ok then just try to improve a little bit

@Skyenought
Copy link
Member Author

Ok then just try to improve a little bit

I will try, even though it is a bit difficult for me

@Skyenought
Copy link
Member Author

Ok then just try to improve a little bit

Ok then just try to improve a little bit

It's hard to optimize this code at my level, so it looks like I'll have to close this PR :-<

@ReneWerner87
Copy link
Member

We can try to support you

@Skyenought
Copy link
Member Author

We can try to support you

Really? Thx !

@Skyenought
Copy link
Member Author

Skyenought commented Nov 2, 2022

Can you send here old and new benchmark comparision

Seems to have slowed down :-(

old

goos: linux
goarch: amd64                                     
pkg: github.com/gofiber/fiber/v2/middleware/logger
cpu: Intel(R) Core(TM) i5-1035G1 CPU @ 1.00GHz
Benchmark_Logger
Benchmark_Logger-2       5582337               219.6 ns/op             0 B/op          0 allocs/op
Benchmark_Logger-2       5000182               237.4 ns/op             0 B/op          0 allocs/op
Benchmark_Logger-2       5331586               215.4 ns/op             0 B/op          0 allocs/op
Benchmark_Logger-2       5741889               209.6 ns/op             0 B/op          0 allocs/op
PASS
ok      github.com/gofiber/fiber/v2/middleware/logger   5.683s

PASS

new

goos: linux  
goarch: amd64
pkg: github.com/gofiber/fiber/v2/middleware/logger
cpu: Intel(R) Core(TM) i5-1035G1 CPU @ 1.00GHz
Benchmark_Logger
Benchmark_Logger-2       1649736               706.7 ns/op           320 B/op          7 allocs/op
Benchmark_Logger-2       1761847               732.1 ns/op           320 B/op          7 allocs/op
Benchmark_Logger-2       1704952               673.5 ns/op           320 B/op          7 allocs/op
Benchmark_Logger-2       1722056               674.2 ns/op           320 B/op          7 allocs/op
PASS
ok      github.com/gofiber/fiber/v2/middleware/logger   7.636s

PASS

better than before:

goos: linux  
goarch: amd64
pkg: github.com/gofiber/fiber/v2/middleware/logger
cpu: Intel(R) Core(TM) i5-1035G1 CPU @ 1.00GHz
Benchmark_Logger
Benchmark_Logger-2       2779996               439.7 ns/op            80 B/op          2 allocs/op
Benchmark_Logger-2       2711770               433.0 ns/op            80 B/op          2 allocs/op
Benchmark_Logger-2       2692728               437.2 ns/op            80 B/op          2 allocs/op
Benchmark_Logger-2       2676159               452.4 ns/op            80 B/op          2 allocs/op
PASS
ok      github.com/gofiber/fiber/v2/middleware/logger   6.884s

@Skyenought Skyenought force-pushed the feat/switch2map branch 2 times, most recently from 78e1d4c to addc718 Compare November 4, 2022 02:53
@Skyenought
Copy link
Member Author

use switch

goos: linux
goarch: amd64                                     
pkg: github.com/gofiber/fiber/v2/middleware/logger
cpu: Intel(R) Core(TM) i5-1035G1 CPU @ 1.00GHz
Benchmark_Logger
Benchmark_Logger-2       5582337               219.6 ns/op             0 B/op          0 allocs/op
Benchmark_Logger-2       5000182               237.4 ns/op             0 B/op          0 allocs/op
Benchmark_Logger-2       5331586               215.4 ns/op             0 B/op          0 allocs/op
Benchmark_Logger-2       5741889               209.6 ns/op             0 B/op          0 allocs/op
PASS
ok      github.com/gofiber/fiber/v2/middleware/logger   5.683s

PASS

use map

goos: linux                                       
goarch: amd64                                     
pkg: github.com/gofiber/fiber/v2/middleware/logger
cpu: Intel(R) Core(TM) i5-1035G1 CPU @ 1.00GHz    
Benchmark_Logger
Benchmark_Logger-2       4919536               244.3 ns/op             0 B/op          0 allocs/op
Benchmark_Logger-2       4852894               256.5 ns/op             0 B/op          0 allocs/op
Benchmark_Logger-2       4817518               243.4 ns/op             0 B/op          0 allocs/op
Benchmark_Logger-2       4551956               249.3 ns/op             0 B/op          0 allocs/op
PASS
ok      github.com/gofiber/fiber/v2/middleware/logger   5.968s

@Skyenought
Copy link
Member Author

I don't think it's a problem with my code that the check didn't pass, right?

@ReneWerner87
Copy link
Member

No, will look at the pull request in the next few days
Currently there is a lot to do

@Skyenought
Copy link
Member Author

No, will look at the pull request in the next few days Currently there is a lot to do

I'm sorry to bother you, but I'm just confused about the PR not passing.

@ReneWerner87
Copy link
Member

its because of the vulnerability check
can you please refresh your branch with the current master

@li-jin-gou
Copy link
Contributor

No, will look at the pull request in the next few days Currently there is a lot to do

I'm sorry to bother you, but I'm just confused about the PR not passing.

This issue has been fixed and you need to update the code & this change requires a documentation update

@ReneWerner87
Copy link
Member

@Skyenought can you check my last comment and refresh the branch
will wait with the release for this feature

@Skyenought Skyenought force-pushed the feat/switch2map branch 2 times, most recently from 4e4fa61 to 02ac453 Compare November 15, 2022 15:53
@Skyenought
Copy link
Member Author

Now logger support custom tags like "reqHeader:x-key"

goos: linux                                       
goarch: amd64                                     
pkg: github.com/gofiber/fiber/v2/middleware/logger
cpu: Intel(R) Core(TM) i5-1035G1 CPU @ 1.00GHz
Benchmark_Logger
Benchmark_Logger-2       4537462               266.2 ns/op             0 B/op          0 allocs/op
Benchmark_Logger-2       4529635               278.0 ns/op             0 B/op          0 allocs/op
Benchmark_Logger-2       4531717               267.3 ns/op             0 B/op          0 allocs/op
Benchmark_Logger-2       4247362               276.5 ns/op             0 B/op          0 allocs/op
PASS
ok      github.com/gofiber/fiber/v2/middleware/logger   5.962s

@ReneWerner87
Copy link
Member

no i mean to the benchmark you shared at the beginning with the map and the switch

in the last one where you compared switch and map there was a gap of 100ns
#2188 (comment)

in the first one there was only a gap of 20ns
#2188 (comment)

@ReneWerner87
Copy link
Member

first bench
image

last bench
image

@Skyenought
Copy link
Member Author

no i mean to the benchmark you shared at the beginning with the map and the switch

in the last one where you compared switch and map there was a gap of 100ns #2188 (comment)

in the first one there was only a gap of 20ns #2188 (comment)

The latest comparison is between the map logic of my code and your map logic:

if index := strings.Index(tag, ":"); index != -1 {
	if logFunc, ok := tagFunctionMap[tag[0:index+1]]; ok {
		return logFunc(buf, c, w, tag)
	}
}
			
if index := strings.Index(tag, ":"); index != -1 {
	result := subStr(tag, 0, index+1)
	logFunc := tagFunctionMap[result]
	return logFunc(buf, c, w, tag)
}

@ReneWerner87
Copy link
Member

yes i know, i also mean the other comments with the benchmarks

@Skyenought
Copy link
Member Author

I find that whenever I use map to match tags, either your changes to the map logic or my changes, it becomes less performant than switch matching

@ReneWerner87
Copy link
Member

will push an optimized version later

@ReneWerner87 ReneWerner87 self-assigned this Nov 17, 2022
@Skyenought
Copy link
Member Author

Thank you for the code review

@ReneWerner87
Copy link
Member

05dabe7
not 100% done but i pushed it
maybe you can check it on your machine -> need more manual testing

@Skyenought
Copy link
Member Author

05dabe7 not 100% done but i pushed it maybe you can check it on your machine -> need more manual testing

ok

@ReneWerner87
Copy link
Member

Is it now better/faster on your machine?

@Skyenought
Copy link
Member Author

Skyenought commented Nov 17, 2022

yes, at now faster than before:

goos: linux  
goarch: amd64
pkg: github.com/gofiber/fiber/v2/middleware/logger
cpu: Intel(R) Core(TM) i5-1035G1 CPU @ 1.00GHz
Benchmark_Logger
Benchmark_Logger-2       3469314               362.5 ns/op            16 B/op          1 allocs/op
Benchmark_Logger-2       3439934               351.2 ns/op            16 B/op          1 allocs/op
Benchmark_Logger-2       3618189               333.2 ns/op            16 B/op          1 allocs/op
Benchmark_Logger-2       3367159               344.4 ns/op            16 B/op          1 allocs/op
PASS
ok      github.com/gofiber/fiber/v2/middleware/logger   6.262s

before

goos: linux
goarch: amd64
pkg: github.com/gofiber/fiber/v2/middleware/logger
cpu: Intel(R) Core(TM) i5-1035G1 CPU @ 1.00GHz
Benchmark_Logger
Benchmark_Logger-2       3122877               386.0 ns/op             0 B/op          0 allocs/op
Benchmark_Logger-2       3019996               385.9 ns/op             0 B/op          0 allocs/op
Benchmark_Logger-2       3146631               382.3 ns/op             0 B/op          0 allocs/op
Benchmark_Logger-2       3159733               388.7 ns/op             0 B/op          0 allocs/op
PASS
ok      github.com/gofiber/fiber/v2/middleware/logger   6.380s

@ReneWerner87
Copy link
Member

But not faster than the master with the benchmark adjustment? Maybe it's because of the stuff we add to the context.

@Skyenought
Copy link
Member Author

if not add something in context, we couldn`t get some message.

It's 1:13 am in my time zone, so I'm off to bed. I wish you all the best

@ReneWerner87
Copy link
Member

benchmark with format "${bytesReceived} ${bytesSent} ${status} ${reqHeader:test}" and local in handler:
master:

Benchmark_Logger-12    	 4105185	       293.7 ns/op	       0 B/op	       0 allocs/op
Benchmark_Logger-12    	 4130638	       290.5 ns/op	       0 B/op	       0 allocs/op
Benchmark_Logger-12    	 4085211	       288.9 ns/op	       0 B/op	       0 allocs/op
Benchmark_Logger-12    	 4149218	       292.5 ns/op	       0 B/op	       0 allocs/op
Benchmark_Logger-12    	 4114450	       290.6 ns/op	       0 B/op	       0 allocs/op

feature branch:

Benchmark_Logger-12    	 4541797	       262.4 ns/op	       0 B/op	       0 allocs/op
Benchmark_Logger-12    	 4541694	       261.9 ns/op	       0 B/op	       0 allocs/op
Benchmark_Logger-12    	 4574413	       261.2 ns/op	       0 B/op	       0 allocs/op
Benchmark_Logger-12    	 4519794	       261.1 ns/op	       0 B/op	       0 allocs/op
Benchmark_Logger-12    	 4617871	       260.0 ns/op	       0 B/op	       0 allocs/op

benchmark with default format "[${time}] ${status} - ${latency} ${method} ${path}\n" and local in handler:
master:

Benchmark_Logger-12    	 1840008	       628.7 ns/op	       8 B/op	       1 allocs/op
Benchmark_Logger-12    	 1882804	       636.4 ns/op	       8 B/op	       1 allocs/op
Benchmark_Logger-12    	 1841458	       684.7 ns/op	       8 B/op	       1 allocs/op
Benchmark_Logger-12    	 1883836	       662.0 ns/op	       8 B/op	       1 allocs/op
Benchmark_Logger-12    	 1851812	       643.3 ns/op	       8 B/op	       1 allocs/op

feature branch:

Benchmark_Logger-12    	 1690204	       808.4 ns/op	      56 B/op	       3 allocs/op
Benchmark_Logger-12    	 1479172	       758.2 ns/op	      56 B/op	       3 allocs/op
Benchmark_Logger-12    	 1578549	       771.2 ns/op	      56 B/op	       3 allocs/op
Benchmark_Logger-12    	 1557202	       763.5 ns/op	      56 B/op	       3 allocs/op
Benchmark_Logger-12    	 1551295	       828.1 ns/op	      56 B/op	       3 allocs/op

for these allocations i created a task in the code
https://github.com/Skyenought/fiber/blob/04eb75ab93784b0cf12b8e569b9496f4adac0f70/middleware/logger/logger.go#L159

reason for the allocations are the storing of the user values
https://github.com/Skyenought/fiber/blob/feat/switch2map/middleware/logger/logger.go#L168

we should try something like this (v3)

data := &LoggerData{
Pid: pid,
ErrPaddingStr: errPaddingStr,
Timestamp: timestamp,
}

data.ChainErr = chainErr
data.Start = start
data.Stop = stop

but with a sync pool

@ReneWerner87
Copy link
Member

After the usage of syncPool and data structs

benchmark with format "${bytesReceived} ${bytesSent} ${status} ${reqHeader:test}" and local in handler:
master:

Benchmark_Logger-12    	 4105185	       293.7 ns/op	       0 B/op	       0 allocs/op
Benchmark_Logger-12    	 4130638	       290.5 ns/op	       0 B/op	       0 allocs/op
Benchmark_Logger-12    	 4085211	       288.9 ns/op	       0 B/op	       0 allocs/op
Benchmark_Logger-12    	 4149218	       292.5 ns/op	       0 B/op	       0 allocs/op
Benchmark_Logger-12    	 4114450	       290.6 ns/op	       0 B/op	       0 allocs/op

feature branch:

Benchmark_Logger-12    	 4293021	       290.3 ns/op	       0 B/op	       0 allocs/op
Benchmark_Logger-12    	 4115019	       287.7 ns/op	       0 B/op	       0 allocs/op
Benchmark_Logger-12    	 4231435	       287.5 ns/op	       0 B/op	       0 allocs/op
Benchmark_Logger-12    	 4059115	       284.3 ns/op	       0 B/op	       0 allocs/op
Benchmark_Logger-12    	 4272568	       281.6 ns/op	       0 B/op	       0 allocs/op

benchmark with default format "[${time}] ${status} - ${latency} ${method} ${path}\n" and local in handler:
master:

Benchmark_Logger-12    	 1840008	       628.7 ns/op	       8 B/op	       1 allocs/op
Benchmark_Logger-12    	 1882804	       636.4 ns/op	       8 B/op	       1 allocs/op
Benchmark_Logger-12    	 1841458	       684.7 ns/op	       8 B/op	       1 allocs/op
Benchmark_Logger-12    	 1883836	       662.0 ns/op	       8 B/op	       1 allocs/op
Benchmark_Logger-12    	 1851812	       643.3 ns/op	       8 B/op	       1 allocs/op

feature branch:

Benchmark_Logger-12    	 1907866	       663.8 ns/op	       8 B/op	       1 allocs/op
Benchmark_Logger-12    	 1835814	       641.3 ns/op	       8 B/op	       1 allocs/op
Benchmark_Logger-12    	 1710475	       633.3 ns/op	       8 B/op	       1 allocs/op
Benchmark_Logger-12    	 1789392	       652.6 ns/op	       8 B/op	       1 allocs/op
Benchmark_Logger-12    	 1888110	       645.6 ns/op	       8 B/op	       1 allocs/op

@ReneWerner87
Copy link
Member

@Skyenought @li-jin-gou @hunterzhang86 iam done now -> performance is the same like before but with the feature
can you recheck the code

@ReneWerner87
Copy link
Member

if that is gemerged I will start the release

@li-jin-gou
Copy link
Contributor

@Skyenought @li-jin-gou @hunterzhang86 iam done now -> performance is the same like before but with the feature can you recheck the code

very cool👍🏻

@Skyenought
Copy link
Member Author

Almost the same performance now 👍🏻

@ReneWerner87 ReneWerner87 merged commit e8f8cb6 into gofiber:master Nov 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🚀 [Feature]: Add custom tags to logger middleware
5 participants