-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from dentech-floss/b3-propagator
B3 propagator
- Loading branch information
Showing
5 changed files
with
115 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,19 @@ | ||
# telemetry | ||
|
||
Setup [Opentelemetry](https://github.com/open-telemetry/opentelemetry-go) support for collecting and exporting traces to a backend using a batching strategy. The spans can be exported to either an [Opentelemetry Collector](https://github.com/open-telemetry/opentelemetry-collector) or just to stdout for local development usage. | ||
Setup [Opentelemetry](https://github.com/open-telemetry/opentelemetry-go) support, only tracing is supported atm (metrics and logging is to come in the future). | ||
|
||
Note that both of these can't be enabled at the same time, first we check if the "OtlpExporterEnabled" flag is true and if that is not the case then we check if the "StdoutExporterEnabled" flag is true. | ||
## Tracing | ||
|
||
Tracing is set up for collecting and exporting traces to a backend using a batching strategy. The spans can be exported to either an [Opentelemetry Collector](https://github.com/open-telemetry/opentelemetry-collector) or just to stdout for local development usage. Note that both of these can't be enabled at the same time, first we check if the "OtlpExporterEnabled" flag is true and if that is not the case then we check if the "StdoutExporterEnabled" flag is true. | ||
|
||
### Propagation | ||
|
||
The default propagator is the standard W3C trace context/baggage, but this is possible to change by providing a "Propagator" in the config (see the example below). One reason for not using W3C is to provide a setup that the GCP infrastructure "does not understand" and thus does not tamper with (in order to opt out from Cloud Trace). This is explained in more detail in this blog post: [opting-out-of-tracing-on-gcp](https://lynn.zone/blog/opting-out-of-tracing-on-gcp/) (the update in that post about B3 propagation is based on correspondance between the author of this lib and the author of the blog). | ||
|
||
## Install | ||
|
||
``` | ||
go get github.com/dentech-floss/[email protected].1 | ||
go get github.com/dentech-floss/[email protected].2 | ||
``` | ||
|
||
## Usage | ||
|
@@ -36,9 +42,10 @@ func main() { | |
ServiceVersion: revision.ServiceVersion, | ||
DeploymentEnvironment: metadata.ProjectID, | ||
OtlpExporterEnabled: metadata.OnGCP, | ||
// OtlpCollectorHttpEndpoint: ..., // defaults to "opentelemetry-collector:80" if not set | ||
// OtlpCollectorTimeoutSecs: ..., // default to 30 if not set | ||
// StdoutExporterEnabled: ..., // if OtlpExporterEnabled is false, then you can enable this for stdout exporting | ||
// OtlpCollectorHttpEndpoint: ..., // defaults to "opentelemetry-collector:80" if not set | ||
// OtlpCollectorTimeoutSecs: ..., // default to 30 if not set | ||
// StdoutExporterEnabled: ..., // if OtlpExporterEnabled is false, then you can enable this for stdout exporting | ||
// Propagator: telemetry.B3_PROPAGATOR, // defaults to W3C trace context/baggage if not set, set it to switch propagator | ||
}, | ||
) | ||
defer shutdownTracing() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package telemetry | ||
|
||
import ( | ||
"context" | ||
"reflect" | ||
"testing" | ||
|
||
"go.opentelemetry.io/otel" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_SetupTracing_Propagator(t *testing.T) { | ||
|
||
require := require.New(t) | ||
|
||
// We default to using W3C trace context/baggage here, so if we don't specify a propagator | ||
// then W3C shall be the default | ||
|
||
_, shutdownTracing := SetupTracing( | ||
context.Background(), | ||
&TracingConfig{ | ||
ServiceName: "serviceName", | ||
ServiceVersion: "serviceVersion", | ||
DeploymentEnvironment: "projectID", | ||
OtlpExporterEnabled: false, | ||
//Propagator: B3_PROPAGATOR, // rely on the default which shall be W3C | ||
}, | ||
) | ||
|
||
require.NotNil(otel.GetTextMapPropagator()) | ||
|
||
propagatorType := reflect.TypeOf(otel.GetTextMapPropagator()) | ||
require.Equal("go.opentelemetry.io/otel/propagation", propagatorType.PkgPath(), "unexpected propagator") | ||
require.Equal("propagation.compositeTextMapPropagator", propagatorType.String(), "unexpected propagator") | ||
|
||
shutdownTracing() | ||
|
||
// If one don't want to use W3C then it shall be possible to provide one. Here we switch to B3, | ||
// which the GCP infrastructure (gRPC API Gateway for example) does not understand and thus | ||
// will not tamper with (fudging up tracing where we do not want to use Cloud Trace) | ||
|
||
_, shutdownTracing = SetupTracing( | ||
context.Background(), | ||
&TracingConfig{ | ||
ServiceName: "serviceName", | ||
ServiceVersion: "serviceVersion", | ||
DeploymentEnvironment: "projectID", | ||
OtlpExporterEnabled: false, | ||
Propagator: B3_PROPAGATOR, // this alternative is provided by this lib | ||
}, | ||
) | ||
|
||
require.NotNil(otel.GetTextMapPropagator()) | ||
|
||
propagatorType = reflect.TypeOf(otel.GetTextMapPropagator()) | ||
require.Equal("go.opentelemetry.io/contrib/propagators/b3", propagatorType.PkgPath(), "unexpected propagator") | ||
require.Equal("b3.propagator", propagatorType.String(), "unexpected propagator") | ||
|
||
shutdownTracing() | ||
} |