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 8 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{
"opentelemetry",

"map",
"root",

Expand Down
36 changes: 36 additions & 0 deletions caddytest/integration/caddyfile_adapt/opentelemetry.txt
@@ -0,0 +1,36 @@
:80 {
opentelemetry /myhandler {
span_name my-span
andriikushch marked this conversation as resolved.
Show resolved Hide resolved
}
}
----------
{
"apps": {
"http": {
"servers": {
"srv0": {
"listen": [
":80"
],
"routes": [
{
"match": [
{
"path": [
"/myhandler"
]
}
],
"handle": [
{
"handler": "opentelemetry",
"span_name": "my-span"
}
]
}
]
}
}
}
}
}
6 changes: 6 additions & 0 deletions go.mod
Expand Up @@ -24,11 +24,17 @@ require (
github.com/smallstep/truststore v0.9.6
github.com/yuin/goldmark v1.4.0
github.com/yuin/goldmark-highlighting v0.0.0-20210516132338-9216f9c5aa01
go.opentelemetry.io/otel v1.0.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.0.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.0.0
go.opentelemetry.io/otel/sdk v1.0.0
go.opentelemetry.io/otel/trace v1.0.0
go.uber.org/zap v1.19.0
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e
golang.org/x/net v0.0.0-20210614182718-04defd469f4e
golang.org/x/term v0.0.0-20210503060354-a79de5458b56
google.golang.org/genproto v0.0.0-20210604141403-392c879c8b08
google.golang.org/grpc v1.40.0
google.golang.org/protobuf v1.27.1
gopkg.in/natefinch/lumberjack.v2 v2.0.0
gopkg.in/yaml.v2 v2.4.0
Expand Down
23 changes: 22 additions & 1 deletion go.sum
Expand Up @@ -178,7 +178,10 @@ github.com/caddyserver/certmagic v0.14.5/go.mod h1:/0VQ5og2Jxa5yBQ8eT80wWS7fi/Dg
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 @@ -201,6 +204,7 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
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/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 @@ -271,6 +275,7 @@ github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1m
github.com/envoyproxy/go-control-plane v0.9.7/go.mod h1:cwu0lG7PUMfa9snN8LXBig5ynNVH9qI8YYLbd1fK2po=
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/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 @@ -468,6 +473,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 @@ -973,14 +979,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.0.0 h1:qTTn6x71GVBvoafHK/yaRUmFzI4LcONZD0/kXxl5PHI=
go.opentelemetry.io/otel v1.0.0/go.mod h1:AjRVh9A5/5DE7S+mZtTR6t8vpKKryam+0lREnfmS4cg=
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.0.0 h1:Vv4wbLEjheCTPV07jEav7fyUpJkyftQK7Ss2G7qgdSo=
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.0.0/go.mod h1:3VqVbIbjAycfL1C7sIu/Uh/kACIUPWHztt8ODYwR3oM=
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.0.0 h1:B9VtEB1u41Ohnl8U6rMCh1jjedu8HwFh4D0QeB+1N+0=
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.0.0/go.mod h1:zhEt6O5GGJ3NCAICr4hlCPoDb2GQuh4Obb4gZBgkoQQ=
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.0.0 h1:BNPMYUONPNbLneMttKSjQhOTlFLOD9U22HNG1KrIN2Y=
go.opentelemetry.io/otel/sdk v1.0.0/go.mod h1:PCrDHlSy5x1kjezSdL37PhbFUMjrsLRshJ2zCzeXwbM=
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.0.0 h1:TSBr8GTEtKevYMG/2d21M989r5WJYVimhTHBKVEZuh4=
go.opentelemetry.io/otel/trace v1.0.0/go.mod h1:PXTWqayeFUlJV1YDNhsJYB184+IvAH814St6o6ajzIs=
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.4.1 h1:QztRUhGYjOPM1I2Nmi7V6XejQyVtcESmo+sbegxvX7Q=
go.step.sm/cli-utils v0.4.1/go.mod h1:hWYVOSlw8W9Pd+BwIbs/aftVVMRms3EG7Q2qLRwc0WA=
go.step.sm/crypto v0.9.0 h1:q2AllTSnVj4NRtyEPkGW2ohArLmbGbe6ZAL/VIOKDzA=
Expand Down Expand Up @@ -1247,6 +1266,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 @@ -1486,8 +1506,9 @@ google.golang.org/grpc v1.36.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAG
google.golang.org/grpc v1.36.1/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU=
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 h1:/9BgsAsa5nWe26HqOlvlgJnqBuktYOLCgjCPqsa56W0=
google.golang.org/grpc v1.38.0/go.mod h1:NREThFqKR1f3iQ6oBuvc5LadQuXVGo9rkm5ZGrQdJfM=
google.golang.org/grpc v1.40.0 h1:AGJ0Ih4mHjSeibYkFGh1dD9KJ/eOtZ93I6hoHhukQ5Q=
google.golang.org/grpc v1.40.0/go.mod h1:ogyxbiOoUXAkP+4+xa6PZSE9DZgIHtSpzjDTB9KAK34=
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
60 changes: 60 additions & 0 deletions modules/caddyhttp/opentelemetry/README.md
@@ -0,0 +1,60 @@
# OpenTelemetry module

This module provides integration with OpenTelemetry tracing facilities. It is implemented
as `caddyhttp.MiddlewareHandler` and can be chained into a list of other handlers.
andriikushch marked this conversation as resolved.
Show resolved Hide resolved

When enabled, it propagates an existing tracing context or will init a new one otherwise.

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

## Configuration

### Environment variables

It 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).

IMPORTANT: Please, consider the version
of [https://github.com/open-telemetry/opentelemetry-go](https://github.com/open-telemetry/opentelemetry-go). Some parts
of the specification may be not implemented yet.
andriikushch marked this conversation as resolved.
Show resolved Hide resolved

These environment variables are required by this module:
- `OTEL_EXPORTER_OTLP_PROTOCOL` or `OTEL_EXPORTER_OTLP_TRACES_PROTOCOL`
- `OTEL_PROPAGATORS`

If neither OTEL_EXPORTER_OTLP_INSECURE nor OTEL_EXPORTER_OTLP_SPAN_INSECURE is provided, then:

1. If OTEL_EXPORTER_OTLP_CERTIFICATE or OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE are specified they will be used for TLS.
2. Else if both OTEL_EXPORTER_OTLP_CERTIFICATE and OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE are not specified, then default
TLS with the default `tls.Config` config will be used for an exporter.

For the exporter configuration details, please
see [spec](https://github.com/open-telemetry/opentelemetry-specification/blob/a4440931b522c7351b0485ff4899f786b4ff4459/specification/protocol/exporter.md)
andriikushch marked this conversation as resolved.
Show resolved Hide resolved
.

Example:

```bash
export OTEL_EXPORTER_OTLP_HEADERS="myAuthHeader=myToken"
export OTEL_EXPORTER_OTLP_PROTOCOL=grpc
export OTEL_PROPAGATORS=tracecontext,baggage
export OTEL_EXPORTER_OTLP_SPAN_INSECURE=false
export OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=localhost:12345
andriikushch marked this conversation as resolved.
Show resolved Hide resolved
```

### Caddy file configuration

Here is an example of **Caddyfile**:
andriikushch marked this conversation as resolved.
Show resolved Hide resolved

```
handle /myHandler {
opentelemetry {
span_name my-span
}
reverse_proxy 127.0.0.1:8081
}
andriikushch marked this conversation as resolved.
Show resolved Hide resolved
```

Please check span
naming [guideline](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/api.md#span)
.
andriikushch marked this conversation as resolved.
Show resolved Hide resolved
123 changes: 123 additions & 0 deletions modules/caddyhttp/opentelemetry/module.go
@@ -0,0 +1,123 @@
package opentelemetry

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(OpenTelemetry{})
httpcaddyfile.RegisterHandlerDirective("opentelemetry", parseCaddyfile)
andriikushch marked this conversation as resolved.
Show resolved Hide resolved
}

// OpenTelemetry implements an HTTP handler that adds support for the opentelemetry tracing.
// It is responsible for the injection and propagation of the tracing contexts.
// OpenTelemetry module can be configured via environment variables https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/sdk-environment-variables.md. Some values can be overwritten with values from the configuration file.
type OpenTelemetry struct {
// SpanName is a span name. It SHOULD follow the naming guideline https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/api.md#span
SpanName string `json:"span_name"`
andriikushch marked this conversation as resolved.
Show resolved Hide resolved

// otel implements opentelemetry related logic.
otel openTelemetryWrapper

logger *zap.Logger
}

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

// Provision implements caddy.Provisioner.
func (ot *OpenTelemetry) 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 *OpenTelemetry) 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 *OpenTelemetry) Validate() error {
if ot.otel.tracer == nil {
return errors.New("openTelemetry tracer is nil")
}

return nil
}

// ServeHTTP implements caddyhttp.MiddlewareHandler.
func (ot *OpenTelemetry) 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 *OpenTelemetry) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
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_name": &ot.SpanName,
andriikushch marked this conversation as resolved.
Show resolved Hide resolved
}

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 OpenTelemetry
err := m.UnmarshalCaddyfile(h.Dispenser)
return &m, err
}

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