Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(telemetry): experiments with opentelemetry, traces and clickhous…
…e storage Signed-off-by: Timofey Kirillov <timofey.kirillov@flant.com>
- Loading branch information
1 parent
9c8168e
commit 2e404a9
Showing
9 changed files
with
176 additions
and
55 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
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,35 @@ | ||
package telemetry | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"time" | ||
|
||
"go.opentelemetry.io/otel/attribute" | ||
) | ||
|
||
func MessageEvent(ctx context.Context, msg string) error { | ||
if !IsEnabled() { | ||
return nil | ||
} | ||
|
||
trc := tracerProvider.Tracer("telemetry.werf.io") | ||
|
||
_, span := trc.Start(ctx, "message") | ||
|
||
span.SetAttributes(attribute.Key("ts").Int64(time.Now().UnixMilli())) | ||
span.SetAttributes(attribute.Key("executionID").String(executionID)) | ||
span.SetAttributes(attribute.Key("projectID").String(projectID)) | ||
span.SetAttributes(attribute.Key("command").String(command)) | ||
span.SetAttributes(attribute.Key("eventType").String("message")) | ||
|
||
data, err := json.Marshal(map[string]interface{}{"message": msg}) | ||
if err != nil { | ||
return fmt.Errorf("unable to marshal message data: %w", err) | ||
} | ||
span.SetAttributes(attribute.Key("data").String(string(data))) | ||
span.End() | ||
|
||
return nil | ||
} |
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,59 @@ | ||
package telemetry | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
neturl "net/url" | ||
"time" | ||
|
||
"go.opentelemetry.io/otel/exporters/otlp/otlptrace" | ||
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp" | ||
"go.opentelemetry.io/otel/sdk/trace" | ||
) | ||
|
||
var ( | ||
tracerProvider *trace.TracerProvider | ||
traceExporter *otlptrace.Exporter | ||
) | ||
|
||
func SetupTraceExporter(ctx context.Context, url string) error { | ||
{ | ||
e, err := NewTraceExporter(url) | ||
if err != nil { | ||
return fmt.Errorf("unable to create telemetry trace exporter: %w", err) | ||
} | ||
traceExporter = e | ||
} | ||
|
||
tracerProvider = trace.NewTracerProvider(trace.WithSyncer(traceExporter)) | ||
|
||
if err := traceExporter.Start(ctx); err != nil { | ||
return fmt.Errorf("error starting telemetry trace exporter: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func NewTraceExporter(url string) (*otlptrace.Exporter, error) { | ||
urlObj, err := neturl.Parse(url) | ||
if err != nil { | ||
return nil, fmt.Errorf("bad url: %w", err) | ||
} | ||
|
||
var opts []otlptracehttp.Option | ||
|
||
if urlObj.Scheme == "http" { | ||
opts = append(opts, otlptracehttp.WithInsecure()) | ||
} | ||
|
||
opts = append(opts, | ||
otlptracehttp.WithEndpoint(urlObj.Host), | ||
otlptracehttp.WithURLPath(urlObj.Path), | ||
otlptracehttp.WithRetry(otlptracehttp.RetryConfig{Enabled: false}), | ||
otlptracehttp.WithTimeout(5*time.Second), | ||
) | ||
|
||
client := otlptracehttp.NewClient(opts...) | ||
|
||
return otlptrace.NewUnstarted(client), nil | ||
} |
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,20 +1,29 @@ | ||
version: "2" | ||
version: "3" | ||
|
||
networks: | ||
otel-clickhouse: | ||
|
||
services: | ||
|
||
otel-collector: | ||
image: otel/opentelemetry-collector-contrib-dev:latest | ||
command: [ "--config=/etc/otel-collector-config.yaml" ] | ||
image: ghcr.io/werf/telemetrywerfio-collector:dev | ||
# image: otel/opentelemetry-collector-contrib-dev:latest | ||
command: | ||
- "--config=/etc/otel-collector-config.yaml" | ||
- "--set=service.telemetry.logs.level=DEBUG" | ||
volumes: | ||
- ./otel-collector-config.yaml:/etc/otel-collector-config.yaml | ||
ports: | ||
- "8888:8888" # Prometheus metrics exposed by the collector server itself | ||
- "8889:8889" # Collected metrics for prometheus | ||
- "4318:4318" # OTLP HTTP receiver | ||
- "4318:4318" # OTLP HTTP receiver | ||
depends_on: | ||
- clickhouse | ||
networks: | ||
- otel-clickhouse | ||
|
||
prometheus: | ||
container_name: prometheus | ||
image: prom/prometheus:latest | ||
volumes: | ||
- ./prometheus.yaml:/etc/prometheus/prometheus.yml | ||
clickhouse: | ||
image: clickhouse/clickhouse-server:latest | ||
ports: | ||
- "9090:9090" | ||
- "9000:9000" | ||
- "8123:8123" | ||
networks: | ||
- otel-clickhouse |
Oops, something went wrong.