Skip to content

Commit

Permalink
Update how-to-add-a-new-rule.md
Browse files Browse the repository at this point in the history
  • Loading branch information
123liuziming authored Aug 13, 2024
1 parent ed5f3a2 commit e858676
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions docs/how-to-add-a-new-rule.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ package os
import "github.com/alibaba/opentelemetry-go-auto-instrumentation/api"

func init() {
api.NewRule("os", "Setenv", "", "onEnterSetenv", "").
Register()
api.NewRule("os", "Setenv", "", "onEnterSetenv", "").Register()
}
```
In the `rule.go` file, we define a new rule for the `os` package. The `NewRule` function takes five arguments: the import path, the function name, the receiver type string, the onEnter function name, and the onExit function name.
Expand All @@ -38,12 +37,12 @@ In the `rule.go` file, we define a new rule for the `os` package. The `NewRule`
package os

import (
"fmt"
"os"
"fmt"
"os"
)

func onEnterSetenv(call os.CallContext, key, value string) {
fmt.Printf("Setting environment variable %s to %s", key, value)
fmt.Printf("Setting environment variable %s to %s", key, value)
}
```

Expand All @@ -63,7 +62,7 @@ package main

import (
...
_ "github.com/alibaba/opentelemetry-go-auto-instrumentation/pkg/rules/os"
_ "github.com/alibaba/opentelemetry-go-auto-instrumentation/pkg/rules/os"
)
```
Dont forget recomplie the tool after adding the new rule, because currently `rules` is coded within the tool.
Expand All @@ -85,4 +84,4 @@ Setting environment variable hello to world%
```
The output shows that the rule is working as expected. The `Setting environment variable hello to world` message is printed when the `os.Setenv()` function is called, that is, the probe code is injected successfully.

There are many advanced features that is not covered in this example, such as the `InstStructRule`, `InstFileRule` and APIs of `CallContext`. You can refer to the existing rules in the `pkg/rules` directory for more information.
There are many advanced features that is not covered in this example, such as the `InstStructRule`, `InstFileRule` and APIs of `CallContext`. You can refer to the existing rules in the `pkg/rules` directory for more information.

0 comments on commit e858676

Please sign in to comment.