Skip to content

Commit

Permalink
Comment out Nginx from os repo (to test)
Browse files Browse the repository at this point in the history
  • Loading branch information
yodigos committed Sep 8, 2024
1 parent 6121156 commit e79e00a
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 1 deletion.
98 changes: 98 additions & 0 deletions odiglet/pkg/ebpf/director.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package ebpf

import (
"bytes"
"context"
"fmt"
"github.com/odigos-io/odigos/common/consts"
"github.com/odigos-io/odigos/odiglet/pkg/env"
"os"
"strconv"
"sync"
"syscall"

odigosv1 "github.com/odigos-io/odigos/api/odigos/v1alpha1"
"github.com/odigos-io/odigos/common"
Expand Down Expand Up @@ -193,6 +199,26 @@ func (d *EbpfDirector[T]) observeInstrumentations(ctx context.Context, scheme *r

func (d *EbpfDirector[T]) Instrument(ctx context.Context, pid int, pod types.NamespacedName, podWorkload *workload.PodWorkload, appName string, containerName string) error {
log.Logger.V(0).Info("Instrumenting process", "pid", pid, "workload", podWorkload)
if d.language == common.NginxProgrammingLanguage {

err := modifyNginxConfFile(pid)
if err != nil {
return err
}

err = addOtelNginxAttributeConfFile(pid, podWorkload.Name)
if err != nil {
return err
}

err = reloadNginx(pid)
if err != nil {
return err
}

return nil
}

d.mux.Lock()
defer d.mux.Unlock()
if _, exists := d.pidsAttemptedInstrumentation[pid]; exists {
Expand Down Expand Up @@ -288,6 +314,78 @@ func (d *EbpfDirector[T]) Instrument(ctx context.Context, pid int, pod types.Nam
return nil
}

func addOtelNginxAttributeConfFile(pid int, deploymentName string) error {
nginx_path := "/proc/" + strconv.Itoa(pid) + "/root/etc/nginx/conf.d"
nginx_conf_file_name := "opentelemetry_module.conf"
opentelemetry_module_conf := nginx_path + "/" + nginx_conf_file_name

otlpEndpoint := fmt.Sprintf("http://%s:%d", env.Current.NodeIP, consts.OTLPPort)

content := "NginxModuleEnabled ON;\n" +
"NginxModuleOtelSpanExporter otlp;\n" +
"NginxModuleOtelExporterEndpoint " + otlpEndpoint + ";\n" +
"NginxModuleServiceName " + deploymentName + ";\n" +
"NginxModuleServiceNamespace NginxServiceNamespace;\n" +
"NginxModuleServiceInstanceId NginxInstanceId;\n" +
"NginxModuleResolveBackends ON;\n"

file, err := os.Create(opentelemetry_module_conf)
if err != nil {
return err
}
defer file.Close()

// Write content to the file
_, err = file.WriteString(content)
if err != nil {
return err
}

// Sync to ensure the content is written to disk
err = file.Sync()
if err != nil {
return err
}

return nil
}

func reloadNginx(pid int) error {

err := syscall.Kill(pid, syscall.SIGHUP)
if err != nil {
return fmt.Errorf("failed to send SIGHUP signal to nginx: %v", err)
}

fmt.Println("NGINX reloaded successfully")
return nil
}

func modifyNginxConfFile(pid int) error {
nginx_path := "/proc/" + strconv.Itoa(pid) + "/root/etc/nginx"
nginx_conf_file_name := "nginx.conf"
nginx_conf_full_path := nginx_path + "/" + nginx_conf_file_name

originalContent, err := os.ReadFile(nginx_conf_full_path)
if err != nil {
return err
}
file, err := os.OpenFile(nginx_conf_full_path, os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
return err
}

otelLoadConf := "load_module /var/odigos/nginx/opentelemetry-webserver-sdk/WebServerModule/Nginx/1.25.5/ngx_http_opentelemetry_module.so;"
if !bytes.Contains(originalContent, []byte(otelLoadConf)) {
_, err = file.WriteString(otelLoadConf + string(originalContent))
if err != nil {
return err
}
}

return nil
}

func (d *EbpfDirector[T]) Language() common.ProgrammingLanguage {
return d.language
}
Expand Down
30 changes: 30 additions & 0 deletions odiglet/pkg/kube/instrumentation_ebpf/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package instrumentation_ebpf
import (
"context"
"errors"
"github.com/odigos-io/odigos/common"
process2 "github.com/odigos-io/odigos/procdiscovery/pkg/process"
"math"

odigosv1 "github.com/odigos-io/odigos/api/odigos/v1alpha1"
odgiosK8s "github.com/odigos-io/odigos/k8sutils/pkg/container"
Expand Down Expand Up @@ -53,8 +56,15 @@ func instrumentPodWithEbpf(ctx context.Context, pod *corev1.Pod, directors ebpf.
return err, instrumentedEbpf
}

minProcessId := getMinimumProcessID(details, language)

var errs []error
for _, d := range details {
// Hack - if NginxInstrumentation - only instrument the process with the minimum PID (it's root)
if minProcessId != -1 && d.ProcessID != minProcessId {
continue
}

podDetails := types.NamespacedName{
Namespace: pod.Namespace,
Name: pod.Name,
Expand All @@ -76,3 +86,23 @@ func instrumentPodWithEbpf(ctx context.Context, pod *corev1.Pod, directors ebpf.
}
return nil, instrumentedEbpf
}

func getMinimumProcessID(details []process2.Details, language common.ProgrammingLanguage) int {
if language != common.NginxProgrammingLanguage {
return -1
}

return findMinProcessID(details)
}

func findMinProcessID(details []process2.Details) int {
minPID := math.MaxInt // Start with the maximum possible integer value

for _, detail := range details {
if detail.ProcessID < minPID {
minPID = detail.ProcessID
}
}

return minPID
}
1 change: 0 additions & 1 deletion tests/e2e/cli-upgrade/odigos-ui.pid

This file was deleted.

0 comments on commit e79e00a

Please sign in to comment.