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 Collector Demo #711

Merged
merged 40 commits into from
May 20, 2020
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
c6c841f
add simple demo for connecting to collector
stefanprisca May 8, 2020
a27a37e
Update main.go
stefanprisca May 8, 2020
ae195db
Merge branch 'master' of https://github.com/open-telemetry/openteleme…
stefanprisca May 11, 2020
7b5aa0c
Merge branch 'otel-collector-demo' of github.com:stefanprisca/opentel…
stefanprisca May 11, 2020
8921ddf
Add readme to the example
stefanprisca May 11, 2020
74347ef
Mention default config
stefanprisca May 11, 2020
be01af5
small rephrasing
stefanprisca May 11, 2020
af769b8
Otel collector demo
stefanprisca May 11, 2020
97f8856
fix readme
stefanprisca May 11, 2020
105e0df
precommit
stefanprisca May 11, 2020
6ba5818
Update example/otel-collector/README.md
stefanprisca May 12, 2020
4c7262b
Update example/otel-collector/README.md
stefanprisca May 12, 2020
e2e4e77
Update example/otel-collector/README.md
stefanprisca May 12, 2020
7db551e
Update example/otel-collector/README.md
stefanprisca May 12, 2020
9c442a0
Update example/otel-collector/README.md
stefanprisca May 12, 2020
95888a7
Update example/otel-collector/README.md
stefanprisca May 12, 2020
a81fb66
Update example/otel-collector/README.md
stefanprisca May 12, 2020
1e878b9
Update example/otel-collector/README.md
stefanprisca May 12, 2020
4c1109b
Update example/otel-collector/README.md
stefanprisca May 12, 2020
41ca31f
Update example/otel-collector/main.go
stefanprisca May 12, 2020
a09fd99
Update example/otel-collector/main.go
stefanprisca May 12, 2020
bbb7763
add full collector config
stefanprisca May 13, 2020
d69386f
add resource attributes to provider
stefanprisca May 13, 2020
5e717aa
Merge branch 'master' of https://github.com/open-telemetry/openteleme…
stefanprisca May 13, 2020
af27b43
Merge branch 'master' of github.com:stefanprisca/opentelemetry-go int…
stefanprisca May 13, 2020
46477e2
fix precommit
stefanprisca May 13, 2020
e9f185c
add k8s files
stefanprisca May 14, 2020
2c71898
Merge branch 'master' of https://github.com/open-telemetry/openteleme…
stefanprisca May 14, 2020
2e6011d
change collector image
stefanprisca May 14, 2020
612e894
fix otlp receiver endpoint
stefanprisca May 14, 2020
d623815
Update README with latest changes
stefanprisca May 15, 2020
107e926
Merge branch 'master' of https://github.com/open-telemetry/openteleme…
stefanprisca May 15, 2020
e31b093
change api/core package to api/kv
stefanprisca May 15, 2020
2a00038
Update example/otel-collector/k8s/namespace.yaml
stefanprisca May 16, 2020
c80ed29
Update example/otel-collector/k8s/otel-collector.yaml
stefanprisca May 16, 2020
8bcefd4
remove unnecessary jaeger files & add licence header
stefanprisca May 16, 2020
7c9f069
Update Makefile
stefanprisca May 19, 2020
1f16b8a
Merge branch 'master' of https://github.com/open-telemetry/openteleme…
stefanprisca May 20, 2020
e1b144e
Wait for batcher to push changes
stefanprisca May 20, 2020
3f24acf
Merge branch 'master' of github.com:stefanprisca/opentelemetry-go int…
stefanprisca May 20, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ coverage.*
/example/namedtracer/namedtracer
/example/prometheus/prometheus
/example/zipkin/zipkin
/example/otel-collector/otel-collector
34 changes: 34 additions & 0 deletions example/otel-collector/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace-k8s:
k apply -f k8s/namespace.yaml
stefanprisca marked this conversation as resolved.
Show resolved Hide resolved

jaeger-operator-k8s:
# Create the jaeger operator and necessary artifacts in ns observability
kubectl create -n observability -f https://raw.githubusercontent.com/jaegertracing/jaeger-operator/master/deploy/crds/jaegertracing.io_jaegers_crd.yaml
kubectl create -n observability -f https://raw.githubusercontent.com/jaegertracing/jaeger-operator/master/deploy/service_account.yaml
kubectl create -n observability -f https://raw.githubusercontent.com/jaegertracing/jaeger-operator/master/deploy/role.yaml
kubectl create -n observability -f https://raw.githubusercontent.com/jaegertracing/jaeger-operator/master/deploy/role_binding.yaml
kubectl create -n observability -f https://raw.githubusercontent.com/jaegertracing/jaeger-operator/master/deploy/operator.yaml

# Create the cluster role & bindings
kubectl create -f https://raw.githubusercontent.com/jaegertracing/jaeger-operator/master/deploy/cluster_role.yaml
kubectl create -f https://raw.githubusercontent.com/jaegertracing/jaeger-operator/master/deploy/cluster_role_binding.yaml

jaeger-k8s:
kubectl apply -f k8s/jaeger.yaml

otel-collector-k8s:
kubectl apply -f k8s/otel-collector.yaml

clean-k8s:
- kubectl delete -f k8s/otel-collector.yaml

- kubectl delete -f k8s/jaeger.yaml

- kubectl delete -f https://raw.githubusercontent.com/jaegertracing/jaeger-operator/master/deploy/cluster_role.yaml
- kubectl delete -f https://raw.githubusercontent.com/jaegertracing/jaeger-operator/master/deploy/cluster_role_binding.yaml

- kubectl delete -n observability -f https://raw.githubusercontent.com/jaegertracing/jaeger-operator/master/deploy/crds/jaegertracing.io_jaegers_crd.yaml
- kubectl delete -n observability -f https://raw.githubusercontent.com/jaegertracing/jaeger-operator/master/deploy/service_account.yaml
- kubectl delete -n observability -f https://raw.githubusercontent.com/jaegertracing/jaeger-operator/master/deploy/role.yaml
- kubectl delete -n observability -f https://raw.githubusercontent.com/jaegertracing/jaeger-operator/master/deploy/role_binding.yaml
- kubectl delete -n observability -f https://raw.githubusercontent.com/jaegertracing/jaeger-operator/master/deploy/operator.yaml
183 changes: 183 additions & 0 deletions example/otel-collector/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
# OpenTelemetry Collector Traces Example

This example illustrates how to export traces from the OpenTelemetry-Go SDK to the OpenTelemetry Collector, and from there to a Jaeger instance.
The complete flow is:

`Demo App -> OpenTelemetry Collector -> Jaeger`

# Prerequisites

The demo is built on Kubernetes, and uses a local instance of [microk8s](https://microk8s.io/). You will need access to a cluster in order to deploy the OpenTelemetry Collector and Jaeger components from this demo.

For simplicity, the demo application is not part of the k8s cluster, and will access the OpenTelemetry Collector through a NodePort on the cluster. Note that the NodePort opened by this demo is not secured.

Ideally you'd want to either have your application running as part of the kubernetes cluster, or use a secured connection (NodePort/LoadBalancer with TLS or an ingress extension).

# Deploying Jaeger and OpenTelemetry Collector
The first step of this demo is to deploy a Jaeger instance and a Collector to your cluster. All the necessary Kubernetes deployment files are available in this demo, in the [k8s](./k8s) folder.
There are two ways to create the necessary deployments for this demo: using the [makefile](./Makefile) or manually applying the k8s files.

## Using the makefile

For using the [makefile](./Makefile), run the following commands in order:
```bash
# Create the namespace
make namespace-k8s

# Deploy Jaeger operator
make jaeger-operator-k8s

# After the operator is deployed, create the Jaeger instance
make jaeger-k8s

# Finally, deploy the OpenTelemetry Collector
make otel-collector-k8s
```

If you want to clean up after this, you can use the `make clean-k8s` to delete all the resources created above. Note that this will not remove the namespace. Because Kubernetes sometimes gets stuck when removing namespaces, please remove this namespace manually after all the resources inside have been deleted.

## Manual deployment

For manual deployments, follow the same steps as above, but instead run the `kubectl apply` yourself.

First, the namespace needs to be created:
```bash
k apply -f k8s/namespace.yaml
```

Jaeger is then deployed via the operator, and the demo follows [these steps](https://github.com/jaegertracing/jaeger-operator#getting-started) to create it:
```bash
# Create the jaeger operator and necessary artifacts in ns observability
kubectl create -n observability -f https://raw.githubusercontent.com/jaegertracing/jaeger-operator/master/deploy/crds/jaegertracing.io_jaegers_crd.yaml
kubectl create -n observability -f https://raw.githubusercontent.com/jaegertracing/jaeger-operator/master/deploy/service_account.yaml
kubectl create -n observability -f https://raw.githubusercontent.com/jaegertracing/jaeger-operator/master/deploy/role.yaml
kubectl create -n observability -f https://raw.githubusercontent.com/jaegertracing/jaeger-operator/master/deploy/role_binding.yaml
kubectl create -n observability -f https://raw.githubusercontent.com/jaegertracing/jaeger-operator/master/deploy/operator.yaml

# Create the cluster role & bindings
kubectl create -f https://raw.githubusercontent.com/jaegertracing/jaeger-operator/master/deploy/cluster_role.yaml
kubectl create -f https://raw.githubusercontent.com/jaegertracing/jaeger-operator/master/deploy/cluster_role_binding.yaml

# Create the Jaeger instance itself:
kubectl apply -f k8s/jaeger/jaeger.yaml
```

The OpenTelemetry Collector is contained in a single k8s file, which can be deployed with one command:
```bash
k8s apply -f k8s/otel-collector.yaml
```


# Configuring the OpenTelemetry Collector

Although the above steps should deploy and configure both Jaeger and the OpenTelemetry Collector, it might be worth spending some time on the [configuration](./k8s/otel-collector.yaml) of the Collector.

One important part here is that, in order to enable our application to send traces to the OpenTelemetry Collector, we need to first configure the otlp receiver:

```yml
...
otel-collector-config: |
receivers:
# Make sure to add the otlp receiver.
# This will open up the receiver on port 55680.
otlp:
endpoint: 0.0.0.0:55680
processors:
...
```

This will create the receiver on the Collector side, and open up port `55680` for receiving traces.

The rest of the configuration is quite standard, with the only mention that we need to create the Jaeger exporter:
stefanprisca marked this conversation as resolved.
Show resolved Hide resolved

```yml
...
exporters:
jaeger_grpc:
endpoint: "jaeger-collector.observability.svc.cluster.local:14250"
...
```

## OpenTelemetry Collector service

One more aspect in the OpenTelemetry Collector [configuration](./k8s/otel-collector.yaml) worth looking at is the NodePort service used for accessing it:
```yaml
apiVersion: v1
kind: Service
metadata:
...
spec:
ports:
- name: otlp # Default endpoint for otlp receiver.
port: 55680
protocol: TCP
targetPort: 55680
nodePort: 30080
- name: metrics # Default endpoint for metrics.
port: 8888
protocol: TCP
targetPort: 8888
selector:
component: otel-collector
type:
NodePort
```

This service will bind the `55680` port used to access the otlp receiver to port `30080` on your cluster's node. By doing so, it makes it possible for us to access the Collector by using the static address `<node-ip>:30080`. In case you are running a local cluster, this will be `localhost:30080`. Note that you can also change this to a LoadBalancer or have an ingress extension for accessing the service.


# Writing the demo

Having the OpenTelemetry Collector started with the otlp port open for traces, and connected to Jaeger, let's look at the go app that will send traces to the Collector.

First, we need to create an exporter using the [otlp](https://pkg.go.dev/go.opentelemetry.io/otel/exporters/otlp?tab=doc) package:
```go
exp, err := otlp.NewExporter(otlp.WithInsecure(),
// use the address of the NodePort service created above
// <node-ip>:30080
otlp.WithAddress("localhost:30080"),
otlp.WithGRPCDialOption(grpc.WithBlock()))
if err != nil {
log.Fatalf("Failed to create the collector exporter: %v", err)
}
defer func() {
err := exp.Stop()
if err != nil {
log.Fatalf("Failed to stop the exporter: %v", err)
}
}()
```
This will initialize the exporter and connect to the otlp receiver at the address that we set for the [NodePort](#opentelemetry-collector-service): `localhost:30080`.

Feel free to remove the blocking operation, but it might come in handy when testing the connection.
Also, make sure to close the exporter before the app exits.

The next step is to create the TraceProvider:
```go
tp, err := sdktrace.NewProvider(
sdktrace.WithConfig(sdktrace.Config{DefaultSampler: sdktrace.AlwaysSample()}),
sdktrace.WithResourceAttributes(
// the service name used to display traces in Jaeger
core.Key(conventions.AttributeServiceName).String("test-service"),
),
sdktrace.WithBatcher(exp, // add following two options to ensure flush
sdktrace.WithScheduleDelayMillis(5),
sdktrace.WithMaxExportBatchSize(2),
))
if err != nil {
log.Fatalf("error creating trace provider: %v\n", err)
}
```

It is important here to set the [AttributeServiceName](https://github.com/open-telemetry/opentelemetry-collector/blob/master/translator/conventions/opentelemetry.go#L20) from the `github.com/open-telemetry/opentelemetry-collector/translator/conventions` package on the resource level. This will be passed to the OpenTelemetry Collector, and used as ServiceName when exporting the traces to Jaeger.

After this, you can simply start sending traces:
```go
tracer := tp.Tracer("test-tracer")
ctx, span := tracer.Start(context.Background(), "CollectorExporter-Example")
defer span.End()
```

The traces should now be visible from the Jaeger UI (if you have it installed), or thorough the jaeger-query service, under the name `test-service`.

You can find the complete code for this example in the [main.go](./main.go) file.
10 changes: 10 additions & 0 deletions example/otel-collector/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module go.opentelemetry.io/otel/example/otel-collector

go 1.14

require (
github.com/open-telemetry/opentelemetry-collector v0.3.0
go.opentelemetry.io/otel v0.5.0
go.opentelemetry.io/otel/exporters/otlp v0.5.0
google.golang.org/grpc v1.29.1
)
Loading