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

opentelemetry: create a new module #4361

Merged
merged 76 commits into from Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from 58 commits
Commits
Show all changes
76 commits
Select commit Hold shift + click to select a range
81567df
opentelemetry: create a new module
Sep 27, 2021
a7fe341
fix imports
Sep 27, 2021
8323fbd
fix test
Sep 27, 2021
f3092b4
Update modules/caddyhttp/opentelemetry/README.md
Sep 27, 2021
01577b5
Update modules/caddyhttp/opentelemetry/README.md
Sep 27, 2021
b9df83e
Update modules/caddyhttp/opentelemetry/README.md
Sep 27, 2021
9d0c1e8
Update modules/caddyhttp/opentelemetry/tracer.go
Sep 27, 2021
9a461e1
rename error ErrUnsupportedTracesProtocol
Sep 27, 2021
cd9cfb2
replace spaces with tabs in the test data
Oct 11, 2021
8c8ef68
Update modules/caddyhttp/opentelemetry/README.md
Oct 11, 2021
155d0c5
Update modules/caddyhttp/opentelemetry/README.md
Oct 11, 2021
a13ac12
replace spaces with tabs in the README.md
Oct 11, 2021
3fce97e
use default values for a propagation and exporter protocol
Oct 11, 2021
e1b4c0c
Merge branch 'master' into add-opentelemetry-module
andriikushch Oct 11, 2021
5a7d40b
set http attributes with helper
Oct 11, 2021
5fcb494
simplify code
Oct 11, 2021
1a39834
Cleanup modules/caddyhttp/opentelemetry/README.md
Nov 3, 2021
3c5277f
Update link in README.md
Nov 3, 2021
0f6602c
Update documentation in README.md
Nov 3, 2021
e380d17
Update link to naming spec in README.md
Nov 3, 2021
7abe411
Rename module from opentelemetry to tracing
Nov 3, 2021
e239a53
Rename span_name to span
Nov 3, 2021
70fe0d2
Rename span_name to span
Nov 3, 2021
8d75f56
Simplify otel resource creation
Nov 3, 2021
c21b417
handle extra attributes
Nov 3, 2021
2e147e3
update go.opentelemetry.io/otel/semconv to 1.7.0
Nov 3, 2021
3453c14
update go.opentelemetry.io/otel version
Nov 3, 2021
a8041eb
remove environment variable handling
Nov 3, 2021
9102435
always use tracecontext,baggage as propagators
Nov 3, 2021
c8977fd
extract tracer name into variable
Nov 3, 2021
23d3500
rename OpenTelemetry to Tracing
Nov 3, 2021
4a12e48
simplify resource creation
Nov 3, 2021
c70f0e9
update go.mod
Nov 3, 2021
61fbe68
rename package from opentelemetry to tracing
Nov 3, 2021
52d4400
cleanup tests
Nov 3, 2021
31b6147
update Caddyfile example in README.md
Nov 3, 2021
13b9338
update README.md
Nov 3, 2021
fa73883
fix test
Nov 3, 2021
320c498
fix module name in README.md
Nov 3, 2021
8c556df
fix module name in README.md
Nov 3, 2021
50bf921
change names in README.md and tests
Nov 3, 2021
c53b79a
order imports
Nov 3, 2021
da4633e
remove redundant tests
Nov 3, 2021
971a888
Update documentation README.md
Nov 30, 2021
9afdd89
Fix grammar
Nov 30, 2021
773a83e
Update comments
Nov 30, 2021
a463325
Update comments
Nov 30, 2021
3bd9493
Merge branch 'master' into add-opentelemetry-module
andriikushch Nov 30, 2021
ca2a2e9
update go.sum
Nov 30, 2021
39ba396
Merge branch 'master' into add-opentelemetry-module
andriikushch Dec 7, 2021
84bdd57
Merge branch 'master' into add-opentelemetry-module
andriikushch Dec 8, 2021
c175eef
Merge branch 'master' into add-opentelemetry-module
andriikushch Dec 10, 2021
70eef8a
Merge branch 'master' into add-opentelemetry-module
andriikushch Dec 12, 2021
067eb5e
update go.sum
Dec 13, 2021
e679df7
Merge branch 'master' into add-opentelemetry-module
andriikushch Dec 14, 2021
a556c13
Merge branch 'master' into add-opentelemetry-module
andriikushch Dec 15, 2021
6b4eb14
Merge branch 'master' into add-opentelemetry-module
andriikushch Dec 28, 2021
ed9845f
Merge branch 'master' into add-opentelemetry-module
andriikushch Jan 3, 2022
3048954
Add otelhttp instrumentation, update OpenTelemetry libraries.
vibhavp Jan 16, 2022
595f9de
Use otelhttp instrumentation for instrumenting HTTP requests.
vibhavp Jan 16, 2022
eb5abda
Merge branch 'master' into add-opentelemetry-module
andriikushch Jan 18, 2022
5bd20b7
use zap.Error() for the error log
Jan 18, 2022
0f35b4c
remove README.md
Jan 19, 2022
15ff9ce
Merge branch 'master' into add-opentelemetry-module
andriikushch Jan 19, 2022
2a0806a
Merge pull request #1 from edjx/propagate-tracing-values
Jan 19, 2022
2200fb3
update dependencies
Jan 19, 2022
c3e17df
clean up the code
Jan 19, 2022
e3a28d4
change comment
Jan 19, 2022
7a5766d
move serveHTTP method from separate file
Jan 20, 2022
c8025ea
add syntax to the UnmarshalCaddyfile comment
Jan 20, 2022
90233ec
Merge branch 'master' into add-opentelemetry-module
andriikushch Jan 20, 2022
13d82ee
go import the file
andriikushch Jan 20, 2022
07c7dc6
admin: Write proper status on invalid requests (#4569) (fix #4561)
mintbomb27 Feb 11, 2022
4d42077
Merge remote-tracking branch 'origin/master' into add-opentelemetry-m…
Feb 15, 2022
faa786c
update dependencies
Feb 15, 2022
b92df37
Merge branch 'master' into add-opentelemetry-module
cedricziel Mar 7, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions caddyconfig/httpcaddyfile/directives.go
Expand Up @@ -37,6 +37,8 @@ import (
// The header directive goes second so that headers
// can be manipulated before doing redirects.
var directiveOrder = []string{
"tracing",

"map",
"root",

Expand Down
36 changes: 36 additions & 0 deletions caddytest/integration/caddyfile_adapt/tracing.txt
@@ -0,0 +1,36 @@
:80 {
tracing /myhandler {
span my-span
}
}
----------
{
"apps": {
"http": {
"servers": {
"srv0": {
"listen": [
":80"
],
"routes": [
{
"match": [
{
"path": [
"/myhandler"
]
}
],
"handle": [
{
"handler": "tracing",
"span": "my-span"
}
]
}
]
}
}
}
}
}
4 changes: 4 additions & 0 deletions go.mod
Expand Up @@ -24,6 +24,10 @@ require (
github.com/smallstep/truststore v0.10.1
github.com/yuin/goldmark v1.4.1
github.com/yuin/goldmark-highlighting v0.0.0-20210516132338-9216f9c5aa01
go.opentelemetry.io/otel v1.1.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.1.0
go.opentelemetry.io/otel/sdk v1.1.0
go.opentelemetry.io/otel/trace v1.1.0
go.uber.org/zap v1.19.0
golang.org/x/crypto v0.0.0-20210915214749-c084706c2272
golang.org/x/net v0.0.0-20210913180222-943fd674d43e
Expand Down
23 changes: 22 additions & 1 deletion go.sum
Expand Up @@ -189,7 +189,10 @@ github.com/caddyserver/certmagic v0.15.2/go.mod h1:qhkAOthf72ufAcp3Y5jF2RaGE96oi
github.com/campoy/unique v0.0.0-20180121183637-88950e537e7e/go.mod h1:9IOqJGCPMSc6E5ydlp5NIonxObaeu/Iub/X03EKPVYo=
github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ=
github.com/cavaliercoder/go-cpio v0.0.0-20180626203310-925f9528c45e/go.mod h1:oDpT4efm8tSYHXV5tHSdRvBet/b/QzxZ+XyyPehvm3A=
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
github.com/cenkalti/backoff/v4 v4.1.1 h1:G2HAfAmvm/GcKan2oOQpBXOd2tT2G57ZnZGWa1PxPBQ=
github.com/cenkalti/backoff/v4 v4.1.1/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
github.com/census-instrumentation/opencensus-proto v0.2.0/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/census-instrumentation/opencensus-proto v0.3.0/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
Expand All @@ -213,6 +216,7 @@ github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGX
github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
github.com/cncf/xds/go v0.0.0-20210312221358-fbca930ec8ed/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/cncf/xds/go v0.0.0-20210805033703-aa0b78936158/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/cockroachdb/datadriven v0.0.0-20190809214429-80d97fb3cbaa/go.mod h1:zn76sxSg3SzpJ0PPJaLDCu+Bu0Lg3sKTORVIj19EIF8=
github.com/cockroachdb/datadriven v0.0.0-20200714090401-bf6692d28da5/go.mod h1:h6jFvWxBdQXxjopDMZyH2UVceIRfR84bdzbkoKrsWNo=
github.com/cockroachdb/errors v1.2.4/go.mod h1:rQD95gz6FARkaKkQXUksEje/d9a6wBJoCr5oaCLELYA=
Expand Down Expand Up @@ -284,6 +288,7 @@ github.com/envoyproxy/go-control-plane v0.9.7/go.mod h1:cwu0lG7PUMfa9snN8LXBig5y
github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
github.com/envoyproxy/go-control-plane v0.9.9-0.20210217033140-668b12f5399d/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
github.com/envoyproxy/go-control-plane v0.9.9-0.20210512163311-63b5d3c536b0/go.mod h1:hliV/p42l8fGbc6Y9bQ70uLwIvmJyVE5k4iMKlh8wCQ=
github.com/envoyproxy/go-control-plane v0.9.10-0.20210907150352-cf90f659a021/go.mod h1:AFq3mo9L8Lqqiid3OhADV3RfLJnjiw63cSpi+fDTRC0=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/envoyproxy/protoc-gen-validate v0.3.0-java/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/etcd-io/gofail v0.0.0-20190801230047-ad7f989257ca/go.mod h1:49H/RkXP8pKaZy4h0d+NW16rSLhyVBt4o6VLJbmOqDE=
Expand Down Expand Up @@ -479,6 +484,7 @@ github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t
github.com/grpc-ecosystem/grpc-gateway v1.9.2/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/grpc-ecosystem/grpc-gateway v1.9.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/grpc-ecosystem/grpc-gateway v1.14.6/go.mod h1:zdiPV4Yse/1gnckTHtghG4GkDEdKCRJduHpTxT3/jcw=
github.com/grpc-ecosystem/grpc-gateway v1.16.0 h1:gmcG1KaJ57LophUzW0Hy8NmPhnMZb4M0+kPpLofRdBo=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/hashicorp/consul/api v1.1.0/go.mod h1:VmuI/Lkw1nC05EYQWNKwWGbkg+FbDBtguAZLlVdkD9Q=
github.com/hashicorp/consul/api v1.3.0/go.mod h1:MmDNSzIMUjNpY/mQ398R4bk2FnqQLoPndWW5VkKPlCE=
Expand Down Expand Up @@ -985,14 +991,27 @@ go.opencensus.io v0.23.0/go.mod h1:XItmlyltB5F7CS4xOC1DcqMoFqwtC6OG2xF7mCv7P7E=
go.opentelemetry.io/contrib v0.20.0/go.mod h1:G/EtFaa6qaN7+LxqfIAT3GiZa7Wv5DTBUzl5H4LY0Kc=
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.20.0/go.mod h1:oVGt1LRbBOBq1A5BQLlUg9UaU/54aiHw8cgjV3aWZ/E=
go.opentelemetry.io/otel v0.20.0/go.mod h1:Y3ugLH2oa81t5QO+Lty+zXf8zC9L26ax4Nzoxm/dooo=
go.opentelemetry.io/otel v1.1.0 h1:8p0uMLcyyIx0KHNTgO8o3CW8A1aA+dJZJW6PvnMz0Wc=
go.opentelemetry.io/otel v1.1.0/go.mod h1:7cww0OW51jQ8IaZChIEdqLwgh+44+7uiTdWsAL0wQpA=
go.opentelemetry.io/otel/exporters/otlp v0.20.0 h1:PTNgq9MRmQqqJY0REVbZFvwkYOA85vbdQU/nVfxDyqg=
go.opentelemetry.io/otel/exporters/otlp v0.20.0/go.mod h1:YIieizyaN77rtLJra0buKiNBOm9XQfkPEKBeuhoMwAM=
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.1.0 h1:PxBRMkrJnY4HRgToPzoLrTdQDHQf9MeFg5oGzTqtzco=
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.1.0/go.mod h1:/E4iniSqAEvqbq6KM5qThKZR2sd42kDvD+SrYt00vRw=
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.1.0 h1:4UC7muAl2UqSoTV0RqgmpTz/cRLH6R9cHt9BvVcq5Bo=
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.1.0/go.mod h1:Gyc0evUosTBVNRqTFGuu0xqebkEWLkLwv42qggTCwro=
go.opentelemetry.io/otel/metric v0.20.0/go.mod h1:598I5tYlH1vzBjn+BTuhzTCSb/9debfNp6R3s7Pr1eU=
go.opentelemetry.io/otel/oteltest v0.20.0/go.mod h1:L7bgKf9ZB7qCwT9Up7i9/pn0PWIa9FqQ2IQ8LoxiGnw=
go.opentelemetry.io/otel/sdk v0.20.0/go.mod h1:g/IcepuwNsoiX5Byy2nNV0ySUF1em498m7hBWC279Yc=
go.opentelemetry.io/otel/sdk v1.1.0 h1:j/1PngUJIDOddkCILQYTevrTIbWd494djgGkSsMit+U=
go.opentelemetry.io/otel/sdk v1.1.0/go.mod h1:3aQvM6uLm6C4wJpHtT8Od3vNzeZ34Pqc6bps8MywWzo=
go.opentelemetry.io/otel/sdk/export/metric v0.20.0/go.mod h1:h7RBNMsDJ5pmI1zExLi+bJK+Dr8NQCh0qGhm1KDnNlE=
go.opentelemetry.io/otel/sdk/metric v0.20.0/go.mod h1:knxiS8Xd4E/N+ZqKmUPf3gTTZ4/0TjTXukfxjzSTpHE=
go.opentelemetry.io/otel/trace v0.20.0/go.mod h1:6GjCW8zgDjwGHGa6GkyeB8+/5vjT16gUEi0Nf1iBdgw=
go.opentelemetry.io/otel/trace v1.1.0 h1:N25T9qCL0+7IpOT8RrRy0WYlL7y6U0WiUJzXcVdXY/o=
go.opentelemetry.io/otel/trace v1.1.0/go.mod h1:i47XtdcBQiktu5IsrPqOHe8w+sBmnLwwHt8wiUsWGTI=
go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
go.opentelemetry.io/proto/otlp v0.9.0 h1:C0g6TWmQYvjKRnljRULLWUVJGy8Uvu0NEL/5frY2/t4=
go.opentelemetry.io/proto/otlp v0.9.0/go.mod h1:1vKfU9rv61e9EVGthD1zNvUbiwPcimSsOPU9brfSHJg=
go.step.sm/cli-utils v0.7.0 h1:2GvY5Muid1yzp7YQbfCCS+gK3q7zlHjjLL5Z0DXz8ds=
go.step.sm/cli-utils v0.7.0/go.mod h1:Ur6bqA/yl636kCUJbp30J7Unv5JJ226eW2KqXPDwF/E=
go.step.sm/crypto v0.9.0/go.mod h1:+CYG05Mek1YDqi5WK0ERc6cOpKly2i/a5aZmU1sfGj0=
Expand Down Expand Up @@ -1258,6 +1277,7 @@ golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210412220455-f1c623a9e750/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210503080704-8803ae5d1324/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down Expand Up @@ -1502,8 +1522,9 @@ google.golang.org/grpc v1.36.1/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAG
google.golang.org/grpc v1.37.0/go.mod h1:NREThFqKR1f3iQ6oBuvc5LadQuXVGo9rkm5ZGrQdJfM=
google.golang.org/grpc v1.37.1/go.mod h1:NREThFqKR1f3iQ6oBuvc5LadQuXVGo9rkm5ZGrQdJfM=
google.golang.org/grpc v1.38.0/go.mod h1:NREThFqKR1f3iQ6oBuvc5LadQuXVGo9rkm5ZGrQdJfM=
google.golang.org/grpc v1.39.0 h1:Klz8I9kdtkIN6EpHHUOMLCYhTn/2WAe5a0s1hcBkdTI=
google.golang.org/grpc v1.39.0/go.mod h1:PImNr+rS9TWYb2O4/emRugxiyHZ5JyHW5F+RPnDzfrE=
google.golang.org/grpc v1.41.0 h1:f+PlOh7QV4iIJkPrx5NQ7qaNGFQ3OTse67yaDHfju4E=
google.golang.org/grpc v1.41.0/go.mod h1:U3l9uK9J0sini8mHphKoXyaqDA/8VyGnDee1zzIUK6k=
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.1.0/go.mod h1:6Kw0yEErY5E/yWrBtf03jp27GLLJujG4z/JK95pnjjw=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
Expand Down
1 change: 1 addition & 0 deletions modules/caddyhttp/standard/imports.go
Expand Up @@ -17,4 +17,5 @@ import (
_ "github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy/fastcgi"
_ "github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite"
_ "github.com/caddyserver/caddy/v2/modules/caddyhttp/templates"
_ "github.com/caddyserver/caddy/v2/modules/caddyhttp/tracing"
)
43 changes: 43 additions & 0 deletions modules/caddyhttp/tracing/README.md
@@ -0,0 +1,43 @@
# Tracing module
francislavoie marked this conversation as resolved.
Show resolved Hide resolved

This module provides integration with OpenTelemetry tracing facilities. It is implemented
as a `caddyhttp.MiddlewareHandler` and can be chained into a list of other handlers.

When enabled, it will propagate an existing trace context or initialize a new one.

It is based on `https://github.com/open-telemetry/opentelemetry-go`.

This module uses `gRPC` as an exporter protocol and W3C `tracecontext` and `baggage` as propagators.

## Configuration

### Environment variables

This module can be configured using the environment variables defined
by the [OpenTelemetry Environment Variable Specification](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/sdk-environment-variables.md).

For the exporter configuration details, please
see [spec](https://github.com/open-telemetry/opentelemetry-specification/blob/v1.7.0/specification/protocol/exporter.md)
.

Example:

```bash
export OTEL_EXPORTER_OTLP_HEADERS="myAuthHeader=myToken,anotherHeader=value"
export OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=https://my-otlp-endpoint:55680
```

### Caddyfile configuration

Here is a **Caddyfile** example:

```
handle /myHandler {
tracing {
span my-span
}
reverse_proxy 127.0.0.1:8081
}
```

Please see span naming [guidelines](https://github.com/open-telemetry/opentelemetry-specification/blob/v1.7.0/specification/trace/api.md).
127 changes: 127 additions & 0 deletions modules/caddyhttp/tracing/module.go
@@ -0,0 +1,127 @@
package tracing

import (
"errors"
"fmt"
"net/http"

"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"go.uber.org/zap"
)

func init() {
caddy.RegisterModule(Tracing{})
httpcaddyfile.RegisterHandlerDirective("tracing", parseCaddyfile)
}

// Tracing implements an HTTP handler that adds support for distributed tracing,
// using OpenTelemetry. This module is responsible for the injection and
// propagation of the trace context. Configure this module via environment
// variables (see https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/sdk-environment-variables.md).
// Some values can be overwritten in the configuration file.
type Tracing struct {
// SpanName is a span name. It should follow the naming guidelines here:
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/api.md#span
SpanName string `json:"span"`

// otel implements opentelemetry related logic.
otel openTelemetryWrapper

logger *zap.Logger
}

// CaddyModule returns the Caddy module information.
func (Tracing) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "http.handlers.tracing",
New: func() caddy.Module { return new(Tracing) },
}
}

// Provision implements caddy.Provisioner.
func (ot *Tracing) Provision(ctx caddy.Context) error {
ot.logger = ctx.Logger(ot)

var err error
ot.otel, err = newOpenTelemetryWrapper(ctx, ot.SpanName)

return err
}

// Cleanup implements caddy.CleanerUpper and closes any idle connections. It
// calls Shutdown method for a trace provider https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/sdk.md#shutdown.
func (ot *Tracing) Cleanup() error {
if err := ot.otel.cleanup(ot.logger); err != nil {
return fmt.Errorf("tracerProvider shutdown: %w", err)
}
return nil
}

// Validate implements caddy.Validator.
func (ot *Tracing) Validate() error {
if ot.otel.tracer == nil {
return errors.New("tracer is nil")
}

return nil
}

// ServeHTTP implements caddyhttp.MiddlewareHandler.
func (ot *Tracing) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
return ot.otel.ServeHTTP(w, r, next)
}

// UnmarshalCaddyfile sets up the module from Caddyfile tokens.
func (ot *Tracing) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
mholt marked this conversation as resolved.
Show resolved Hide resolved
setParameter := func(d *caddyfile.Dispenser, val *string) error {
if d.NextArg() {
*val = d.Val()
} else {
return d.ArgErr()
}
if d.NextArg() {
return d.ArgErr()
}
return nil
}

// paramsMap is a mapping between "string" parameter from the Caddyfile and its destination within the module
paramsMap := map[string]*string{
"span": &ot.SpanName,
}

for d.Next() {
args := d.RemainingArgs()
if len(args) > 0 {
return d.ArgErr()
}

for d.NextBlock(0) {
if dst, ok := paramsMap[d.Val()]; ok {
if err := setParameter(d, dst); err != nil {
return err
}
} else {
return d.ArgErr()
}
}
}
return nil
}

func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
var m Tracing
err := m.UnmarshalCaddyfile(h.Dispenser)
return &m, err
}

// Interface guards
var (
_ caddy.Provisioner = (*Tracing)(nil)
_ caddy.Validator = (*Tracing)(nil)
_ caddyhttp.MiddlewareHandler = (*Tracing)(nil)
_ caddyfile.Unmarshaler = (*Tracing)(nil)
)