48
Compile Time Code Weaving with Go https://github.com/deferpanic/goweave

Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

  • Upload
    ontico

  • View
    443

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

Compile Time Code Weaving with Go

https://github.com/deferpanic/goweave

Page 2: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

I like your software but…

Page 3: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

What sucks about Go?

Page 4: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

Do you know about AspectJ?

Page 5: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

Really?

Page 6: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

I’m not a code purist

Page 7: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

“Which is our full time job. Write a program to write

a program”- rob pike / gopherfest 2015

Page 8: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)
Page 9: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

Go is Actually a Decent Fit

Page 10: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

Prior Artgo fmtgo fix

go generate

Page 11: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

go fix

Page 12: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

No WrappingNo code residue

Non-trivial amount of work

Page 13: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

Regex/Sed Doesn’t Work

scopemany

packagesmany refs

Page 14: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

Aspect Oriented Programming

Page 15: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

Cross-Cutting Concern

Contains behavior that is prominent in many places but don’t really have anything to do with your business logic.

Page 16: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

Logging is a canonical example

logging.Error.Println(“got here”)

logging.Error.Println(“got here”)

logging.Error.Println(“got here”)

Page 17: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

Pointcut

an expression that details where to apply your behavior

Page 18: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

like a regex but on the ast

Page 19: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

Advice

fmt.Println("Hello, 世界 ")

Page 20: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

Aspect

the combination of a pointcut and advice

Page 21: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

Sample PointCuts

Page 22: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

Call

Before, after or wrap around calling a function.

Page 23: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

func blah() { some.stuff()}

func blah() { fmt.Println(“before”) some.stuff()}

before

after

Page 24: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

Execute

Before or after inside executing a method.

Page 25: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

func blah() { stuff()}

func stuff() {}

before

afterfunc blah() { stuff()}

func stuff() { fmt.Println(“stuff”)}

Page 26: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

Within

Every single call within a method.

Page 27: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

func blah() { beforeEach() slowCall() beforeEach() fastcall() }

before

after

func blah() { slowCall() fastcall() }

Page 28: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

get

Before or After Every Get to a Variable

Page 29: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

before

after

func blah() { x := “stuff” fmt.Println(x)}

func blah() { x := “stuff” fmt.Println(“before getting x”) fmt.Println(x)}

Page 30: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

set

Before or After Every Set to a Variable

Page 31: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

before

after

func blah() { x := “stuff” fmt.Println(x)}

func blah() { fmt.Println(“before setting x”) x := “stuff” fmt.Println(x)}

Page 32: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

declaration

Before or After Every Variable Declaration

Page 33: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

before

after

func blah() { ch := make(chan int, 2) ch <- 1}

func blah() { fmt.Println(“before make”) ch := make(chan int, 2) ch <- 1}

Page 34: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

Logging

Page 35: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

Monitoring

Page 36: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

Performance Analysis

Page 37: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

Debugging & Tracing

Page 38: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

Undo Functionality

Page 39: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)
Page 40: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

Behavior Mutation

Page 41: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

Security

Page 42: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

Transactions

Page 43: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

Log every call to Itoa

Page 44: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

Validation

Page 45: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

Time Database Query Latencies

Page 46: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

Ensure we log every panic in a goroutine

Page 47: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

the loom

central repository for storing .weave files

Page 48: Compile Time Code Weaving in Go. Large Systems Debugging, Profiling / Ian Eyberg (DeferPanic)

https://github.com/deferpanic/goweave

https://github.com/deferpanic/loom