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

Adapts the open-telemetry example for using agents #506

Merged
merged 5 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ suspend fun main() {
// To run the example with OpenTelemetry, you can execute the following commands:
// - # docker compose-up server/docker/opentelemetry

// OpenAI.conversation(LocalVectorStore(OpenAI().DEFAULT_EMBEDDING), OpenTelemetryMetric())
// OpenAI.conversation(
// com.xebia.functional.xef.store.LocalVectorStore(OpenAI().DEFAULT_EMBEDDING),
// com.xebia.functional.xef.opentelemetry.OpenTelemetryMetric()) {

val configNoneFromConversation = PromptConfiguration {
messagePolicy { addMessagesFromConversation = MessagesFromHistory.NONE }
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ suspendApp = "0.4.0"
flyway = "9.22.3"
resources-kmp = "0.4.0"
detekt = "1.23.1"
opentelemetry="1.30.1"
opentelemetry="1.31.0"
opentelemetry-alpha="1.30.1-alpha"

[libraries]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@ package com.xebia.functional.xef.opentelemetry

import io.opentelemetry.api.OpenTelemetry
import io.opentelemetry.api.common.AttributeKey
import io.opentelemetry.api.common.Attributes
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator
import io.opentelemetry.context.propagation.ContextPropagators
import io.opentelemetry.exporter.otlp.logs.OtlpGrpcLogRecordExporter
import io.opentelemetry.exporter.otlp.metrics.OtlpGrpcMetricExporter
import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter
import io.opentelemetry.sdk.OpenTelemetrySdk
import io.opentelemetry.sdk.logs.SdkLoggerProvider
import io.opentelemetry.sdk.logs.export.BatchLogRecordProcessor
import io.opentelemetry.sdk.metrics.SdkMeterProvider
import io.opentelemetry.sdk.metrics.export.PeriodicMetricReader
import io.opentelemetry.sdk.resources.Resource
import io.opentelemetry.sdk.trace.SdkTracerProvider
import io.opentelemetry.sdk.trace.export.BatchSpanProcessor
Expand All @@ -17,24 +24,49 @@ data class OpenTelemetryConfig(
) {

fun newInstance(): OpenTelemetry {
val jaegerOtlpExporter: OtlpGrpcSpanExporter =
val otlpGrpcSpanExporter: OtlpGrpcSpanExporter =
OtlpGrpcSpanExporter.builder()
.setEndpoint(endpointConfig)
.setTimeout(30, TimeUnit.SECONDS)
.build()

val serviceNameResource: Resource =
Resource.create(Attributes.of(AttributeKey.stringKey("service.name"), serviceName))
val resource =
Resource.getDefault()
.toBuilder()
.put(AttributeKey.stringKey("service.name"), serviceName)
.build()

val tracerProvider =
SdkTracerProvider.builder()
.addSpanProcessor(BatchSpanProcessor.builder(jaegerOtlpExporter).build())
.setResource(Resource.getDefault().merge(serviceNameResource))
.addSpanProcessor(BatchSpanProcessor.builder(otlpGrpcSpanExporter).build())
.setResource(resource)
.build()

val meterProvider =
SdkMeterProvider.builder()
.registerMetricReader(
PeriodicMetricReader.builder(OtlpGrpcMetricExporter.builder().build()).build()
)
.setResource(resource)
.build()

val loggerProvider =
SdkLoggerProvider.builder()
.addLogRecordProcessor(
BatchLogRecordProcessor.builder(OtlpGrpcLogRecordExporter.builder().build()).build()
)
.setResource(resource)
.build()

val openTelemetry = OpenTelemetrySdk.builder().setTracerProvider(tracerProvider).build()
val openTelemetry =
OpenTelemetrySdk.builder()
.setTracerProvider(tracerProvider)
.setMeterProvider(meterProvider)
.setLoggerProvider(loggerProvider)
.setPropagators(ContextPropagators.create(W3CTraceContextPropagator.getInstance()))
.buildAndRegisterGlobal()

Runtime.getRuntime().addShutdownHook(Thread { tracerProvider.close() })
Runtime.getRuntime().addShutdownHook(Thread { openTelemetry.close() })
return openTelemetry
}

Expand Down
47 changes: 41 additions & 6 deletions server/docker/opentelemetry/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,44 @@
version: "3"
version: "4"
services:
jaeger:

# Jaeger
jaeger-all-in-one:
image: jaegertracing/all-in-one:latest
environment:
COLLECTOR_OTLP_ENABLED: true
restart: always
ports:
- 4317:4317
- 16686:16686
- "16686:16686"
- "14268"
- "14250"

# Zipkin
zipkin-all-in-one:
image: openzipkin/zipkin:latest
restart: always
ports:
- "9411:9411"

# Collector
otel-collector:
image: otel/opentelemetry-collector:0.85.0
restart: always
command: ["--config=/etc/otel-collector-config.yaml", "${OTELCOL_ARGS}"]
volumes:
- ./otel-collector-config.yaml:/etc/otel-collector-config.yaml
ports:
- "1888:1888" # pprof extension
- "8888:8888" # Prometheus metrics exposed by the collector
- "8889:8889" # Prometheus exporter metrics
- "13133:13133" # health_check extension
- "4317:4317" # OTLP gRPC receiver
- "55679:55679" # zpages extension
depends_on:
- jaeger-all-in-one
- zipkin-all-in-one

prometheus:
image: prom/prometheus:latest
restart: always
volumes:
- ./prometheus.yaml:/etc/prometheus/prometheus.yml
ports:
- "9090:9090"
41 changes: 41 additions & 0 deletions server/docker/opentelemetry/otel-collector-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
receivers:
otlp:
protocols:
grpc:

exporters:
prometheus:
endpoint: "0.0.0.0:8889"
const_labels:
label1: value1

zipkin:
endpoint: "http://zipkin-all-in-one:9411/api/v2/spans"
format: proto

otlp:
endpoint: jaeger-all-in-one:4317
tls:
insecure: true

processors:
batch:

extensions:
health_check:
pprof:
endpoint: :1888
zpages:
endpoint: :55679

service:
extensions: [pprof, zpages, health_check]
pipelines:
traces:
receivers: [otlp]
processors: [batch]
exporters: [zipkin, otlp]
metrics:
receivers: [otlp]
processors: [batch]
exporters: [prometheus]
fedefernandez marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 6 additions & 0 deletions server/docker/opentelemetry/prometheus.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
scrape_configs:
- job_name: 'otel-collector'
scrape_interval: 10s
static_configs:
- targets: ['otel-collector:8889']
- targets: ['otel-collector:8888']