Skip to content

Commit 8398405

Browse files
authored
Fix functional options, add OpenTelemetry example (#221)
* fix(api): propagate options for non-standard API endpoints * docs(otel): add OpenTelemetry example
1 parent 149554b commit 8398405

File tree

19 files changed

+414
-15
lines changed

19 files changed

+414
-15
lines changed

.github/dependabot.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ updates:
1414
assignees:
1515
- "pandatix"
1616

17-
# Functional tests Go module
17+
# Specific examples
1818
- package-ecosystem: "gomod"
19-
directory: "/deploy"
19+
directories:
20+
- "examples/opentelemetry"
2021
schedule:
2122
interval: "weekly"
2223
assignees:

api/export.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func (client *Client) ExportRaw(params *ExportRawParams, opts ...Option) ([]byte
3434
// Build request and execute it manunally
3535
req, _ := http.NewRequest(http.MethodPost, "/api/v1/exports/raw", bytes.NewBuffer(i))
3636
req.Header.Set("Content-Type", "application/json")
37+
req, client.sub.Transport = applyOpts(req, opts...)
3738
res, err := client.Do(req)
3839
if err != nil {
3940
return nil, err

api/login.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func (client *Client) Login(params *LoginParams, opts ...Option) error {
2727

2828
req, _ := http.NewRequest(http.MethodPost, "/login", bytes.NewBufferString(val.Encode()))
2929
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
30+
req, client.sub.Transport = applyOpts(req, opts...)
3031
res, err := client.Do(req)
3132
if err != nil {
3233
return err

api/logout.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
func (client *Client) Logout(opts ...Option) error {
1010
req, _ := http.NewRequest(http.MethodGet, "/logout", nil)
11+
req, client.sub.Transport = applyOpts(req, opts...)
1112
res, err := client.Do(req)
1213
if err != nil {
1314
return err

api/notifications.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package api
22

33
import (
4-
"context"
54
"fmt"
65
"net/http"
76
"net/url"
@@ -68,16 +67,8 @@ func (client *Client) HeadNotifications(params *HeadNotificationsParams, opts ..
6867
req.URL.RawQuery = val.Encode()
6968
}
7069

71-
// Compile options
72-
reqopts := &options{
73-
Ctx: context.Background(),
74-
}
75-
for _, opt := range opts {
76-
opt.apply(reqopts)
77-
}
78-
req = req.WithContext(reqopts.Ctx)
79-
8070
// Issue HTTP request
71+
req, client.sub.Transport = applyOpts(req, opts...)
8172
res, err := client.Do(req)
8273
if err != nil {
8374
return 0, err

api/register.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func (client *Client) Register(params *RegisterParams, opts ...Option) error {
2323

2424
req, _ := http.NewRequest(http.MethodPost, "/register", bytes.NewBufferString(val.Encode()))
2525
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
26+
req, client.sub.Transport = applyOpts(req, opts...)
2627
res, err := client.Do(req)
2728
if err != nil {
2829
return err
@@ -37,6 +38,7 @@ func (client *Client) Register(params *RegisterParams, opts ...Option) error {
3738

3839
// Update session to track user then fetch nonce for later API calls
3940
req, _ = http.NewRequest(http.MethodGet, "/", nil)
41+
req, client.sub.Transport = applyOpts(req, opts...)
4042
res, err = client.Do(req)
4143
if err != nil {
4244
return err

api/reset.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (client *Client) Reset(params *ResetParams, opts ...Option) error {
3838
}
3939
req, _ := http.NewRequest(http.MethodPost, "/admin/reset", strings.NewReader(str))
4040
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
41-
41+
req, client.sub.Transport = applyOpts(req, opts...)
4242
res, err := client.Do(req)
4343
if err != nil {
4444
return errors.Wrap(err, "CTFd responded with error")

api/setup.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ func (client *Client) Setup(params *SetupParams, opts ...Option) error {
7070
client.sub.CheckRedirect = oldCR
7171
}()
7272

73+
req, client.sub.Transport = applyOpts(req, opts...)
7374
res, err := client.Do(req)
7475
if err != nil {
7576
return err

api/utils.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ var nonceRegex = regexp.MustCompile(`([0-9a-f]{64})`)
1414
// It uses the default HTTP client under the hood.
1515
func GetNonceAndSession(url string, opts ...Option) (nonce string, session string, err error) {
1616
req, _ := http.NewRequest(http.MethodGet, url+"/setup", nil)
17-
res, err := http.DefaultClient.Do(req)
17+
client := http.DefaultClient
18+
req, client.Transport = applyOpts(req, opts...)
19+
res, err := client.Do(req)
1820
if err != nil {
1921
return "", "", err
2022
}

examples/opentelemetry/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Example - OpenTelemetry
2+
3+
At scale, understanding what led your CTFd to its current state can help you understand an incident, or how many interactions are happening under the hood in your distributed system.
4+
5+
This is something we want to provide, first for our own usage, but secondly as a proof-of-concept for the community to see how it can be technically achieved.
6+
7+
## Try it out!
8+
9+
- Run an OpenTelemetry Collector to capture signals (here we focus on traces), Jaeger for distributed traces visualization, and our [instrumented and repackaged CTFd](https://github.com/ctfer-io/ctfd-packaged) for OpenTelemetry support.
10+
```bash
11+
docker compose up
12+
```
13+
14+
- Run the example, with OpenTelemetry exporters settings.
15+
```bash
16+
OTEL_EXPORTER_OTLP_ENDPOINT=dns://localhost:4317 OTEL_EXPORTER_OTLP_INSECURE=true go run main.go
17+
```
18+
19+
- Open Jaeger and visualize traces: [`http://localhost:16686`](http://localhost:16686)
20+
<div align="center">
21+
<img src="jaeger.png" width="1000px">
22+
</div>
23+
24+
- You can delete the infra :wink:
25+
```bash
26+
docker compose down -v
27+
```

0 commit comments

Comments
 (0)